10 cool Vim editor tips users should know

1. Opened a file with vim but does not have permission to write, forgot to do sudo? No worries, you can still write to the file if user has sudo rights.

:w !sudo tee %

2. Revert document to 5min back. Opposite of “:earlier” is “:later”.

:earlier 5m

3. Execute external command and dump output in current window.

:.! [command]

Add current date in the fileopened.

:.! date

4. Delete commands

diw to delete the current word.

di( to delete within the current parenthesis.

di” to delete the text between the quotes.

More can be found at

:help text-objects

5. Turn vim into hex editor. Open file with “-b” to avoid any damage.



:%!xxd -r

6. Searching

/pattern       - search forward for pattern
?pattern       - search backward
n              - repeat forward search
N              - repeat backward
*              - search for word currently under cursor
g*             - search for partial word under curso

Some variables available to set:

:set ignorecase - case insensitive
:set smartcase  - use case if any caps used 
:set incsearch  - show match as search proceeds
:set hlsearch   - search highlighting

7. Variables

:set            - shows vars different from defaults
:set all        - shows all values
:set foo?       - shows the value of foo
:set foo+=opt   - add opt to the value w/o changing others
:set foo-=opt   - remove opt from value
:set foo&       - reset foo to default value
:setlocal foo   - only the current buffer

:verbose set foo?   - tells you where it was last set

8. Replace

Replace occurrences of 'foo' with 'bar' in the whole file


Replace occurrences of ‘foo’ with ‘bar’ in the current line


Replace everything with confirmation first, note the ‘c’ below


Replace words exactly matching ‘foo’ with confirmation first


Replace everything with case insensitive and confirmation first, note the ‘c’ and ‘I’ below


9. Create horizontal and vertical windows. ‘-o’ for horizontal and ‘-O’ for vertical windows.

vim -o file1 file2

vim -O file1 file2

10. Get help

:help ‘incsearch’

:help :substitute