This page is the continuation of my blog post on Vim commands.

Help

:help  -  vim help
:help commandname - help on a particular command
CTRL+] - jump to a highlighted topic
CTRL+T - jump backwards

HTML version of the Vim help pages kept up-to-date automatically from the Vim source repository.

Editing

Undo redo

u: undo last change (can be repeated to undo preceding commands)
Ctrl-R: Redo changes which were undone (undo the undos). 
Compare to '.' to repeat a previous change, at the current cursor position. Ctrl-R will redo a previously undone change, wherever the change occurred. 

Switch between navigation and editing mode

A - move to the end of the line and switch to editing mode 
I - switch to editing mode at the current place
Escape - switch to navigation mode
alt+h alt+j alt+k alt+l - switch to navigation mode and move
alt+: - switch to navigation mode and send a command

Replace characters

Vim wiki on search and replace Find each occurrence of ‘red’ (in the current line only), and replace it with ‘green’:

:s/red/green/g

The scommand means “substitute”. The g means “greedy”, it will replace all occurences of the word on a line (not just the first occurence).

When text is visually selected, press : to enter a command. The command line will automatically enter the range:

:'<,'>

Then you can enter a replacement command such as s/red/green/g to replace each occurence of “red” with “green” in all lines of the visual selection. The command will appear as:

:'<,'>s/red/green/g

Find each occurrence of ‘red’ in all lines (%s), and replace it with ‘green’.

:%s/red/green/g 

Find each occurence of foo but don’t perform the replacement, only count the number of occurences

:%s/foo/bar/n

To replace by a new line character, use instead of . For example to insert a new line after each comma in a selection:

:'<,'>s/,/,\r/g

You can use replacement to remove text. For example to remove the last 2 characters of each line in a selection:

:'<,'>s/..$//

Remove all beginnings of line. :%s//, /g replace all end of line by comma + space. This cleans an html list of species for inclusion in a text.

%s/option value=".*"//g 

Editing a whole line

dd to delete a whole line
yy to copy a whole line
p to paste the copied or deleted text after the current line or 
P to paste the copied or deleted text before the current line 

Editing multiple lines

You can edit multiple lines simultaneously by inserting text inside a multi-line selection in Vim :

Use Ctrl+V to enter visual block mode
Move down with jj to select the columns of text in the lines you want to comment.
Then hit Shift+I and type the text you want to insert.
Then press Escape, the inserted text will appear on all lines.

Similarly to enter text at the end of multiple lines (vim.wikia)

Use Ctrl+V to enter visual block mode
Move down with jj to select the columns of text in the lines you want to comment.
Then hit $ to select all lines until the end of the line.
Then press Shift+A and type the text you want to insert.
The press Escape, the inserted text will appear at the end of all lines.

Copy, cut and paste

Position the cursor where you want to begin cutting.
Press v (or upper case V if you want to cut whole lines).
Move the cursor to the end of what you want to cut.
Press d to cut or y to copy.
Move to where you would like to paste.
Press P to paste before the cursor, or p to paste after. 

Paste without autoindent

:r! cat 

And then paste CTRL + SHIFT + V the content, and CTRL+D.

Also works over SSH.

Indentation

Indentation replaced by spaces, add this to the ~/.vimrc file

set tabstop=4
set expandtab
set softtabstop=4
set shiftwidth=4
filetype indent on 

More details on vim indentation in the python wiki.

Multiple files and windows

:e filename - edit another file 
:ls         - show current buffers
:b 2        - open buffer #2 in this window
:b filename - open buffer #filename in this window
:bd         - close the current buffer (! to forget changes)
:bd filename -close a buffer by name 

Window split

:sp[lit] filename  - split window and load another file
:10sp - specify the split height with a number
:vs[plit] - same but split vertically  
ctrl-w up arrow - move cursor up a window
ctrl-w ctrl-w   - move cursor to another window (cycle)
ctrl-w_         - maximize current window
ctrl-w=         - make all equal size
CTRL+z - suspend the process and get back to the shell
fg - get back to vim

Easier split navigations

nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H> 

Natural split opening

set splitbelow
set splitright

Tools

Shell

Working with external commands explains how to start a shell:

:shell

Then exit the shell normaly with exit.

Use a Bang! to run a command directly, for example to see how many words are in the file, run:

:! wc %

You can copy (y $) and paste a command in the vim command line with CTRL + R + ".

Insert text from a specified file into the current buffer:

:r textfile

You can also read in the output of shell applications. For example, if you wanted to include a list of files from a specific directory, you could include them using this read command:

:r ! ls -1 /home/user/directory

Vim diff

View differences between file1 and file2 (vim documentation)

vimdiff file1 file2

spell check

How to configure vim spellcheck to use two languages To use the spellcheck for two languages, add this to your ~/.vimrc:

set spell spelllang=en_gb,fr

On the first use, vim may give a warning that some word lists are missing. How to make vim download missing word lists To solve this issue, turn spell check off

:set nospell 

Turn spell check on

:set spell

Then vim will offer to download the missing dictionaries.

Set spell check only in the local buffer:

:setlocal spell spelllang=en_gb   

Find alternative for highlighted word:

z=

Mark word as correct, this creates a spell file under /home/user/.vim/spell:

zg 

Mark word as incorrect

zw

Plugins for programming languages

Vim R
Vim on Python wiki (set softtabstop=4) or your  python development environment (set tabstop=4 and set softtabstop=4)

.vimrc

See also my vimrc file stored in setup/.vimrc.

Text colour. Add syntax highlight to your .vimrc

syntax enable

How to add a file extension to vim syntax highlight

au BufNewFile,BufRead *.dump set filetype=sql

I used it to display markdown files as text files:

au BufNewFile,BufRead *.md set filetype=txt