This post contains a list of basic Vim keys/commands. A more complete list can be found at this site.

Change mode

One of Vim's distinguishing features is that it has different modes. To switch between modes:

<Esc> Normal/Command mode (all commands are executed in this mode)
i Insert mode (mode for typing in text)
v Visual mode (text is selected when cursor moves around)

Different ways to enter Insert mode

From Normal/Command mode:

i Insert mode (current cursor position)
a Insert mode (next cursor position)
o Insert mode (next line)

Saving files and quitting Vim

:w save file (and overwrite)
:sav X save to file called X (i.e. 'Save As')
:wq save and quit
:q! force quit (without saving)

Moving around

Use these keys instead of using the arrow keys. The vimrc file discussed in this post disables the arrow keys to force the use of these keys.

j down
k up
h left
l right

Moving around Part 2

0 beginning of line
^ first non-whitespace character in line
$ end of line
% jump to matching (closing) parenthesis
gg first line of file
G last line of file
{ skip paragraph up
} skip paragraph down
w skip forward one word
e skip to end of word
b skip backwards one word

Moving around Part 3

Sometimes if you get the keystrokes wrong, your cursor ends up jumping to another part of the file, and you just want to move back to the last cursor location. These commands may help.

g; last cursor location
g, next cursor location
<C-O> last location of an edit (Ctrl+O)
<C-I> next location of an edit (Ctrl+I)
:27 jump to line 27

Undo/redo

u undo
<C-R> redo (Ctrl+R)

Searching

/ (cursor will drop to bottom of screen)
/abc + Enter search downwards for string 'abc'
n search for next occurrence (after first search string is found)
? + Enter switch search direction (then press n)
f + 'A' search forwards for character 'A' in current line
* search downwards for next occurrence of word under cursor
# search upwards for next occurrence of word under cursor

Copy/paste text

y copy selected text (visual mode)
yy copy current line (can also use Y)
3yy copy three lines
p paste text after cursor (or on next line)
P paste text before cursor (or on current line)
3p paste three copies of text after cursor

Copy/paste text in OSX

Suppose one wants to copy/paste text between iTerm windows. !pbcopy and !pbpaste allow one to cut/paste text. Tip: to copy, use !pbcopy and u. (Note that Command+v in Insert mode will paste text cut from another window).

:. !pbcopy cut current line in iTerm
:25,28 !pbcopy cut lines 25 to 28 in iTerm
:r !pbpaste paste the cut text in iTerm

Delete/replace text

Note that all 'delete' commands are like 'cut', and save the deleted text to a register.

x delete character under cursor (<Del> key)
X delete character before cursor (<Backspace> key)
D delete from cursor to end of line
dd delete current line
3dd delete three lines
daw delete word around cursor
r + 'A' replace character under cursor with character 'A'

Replacing d with c in the commands above has the same effect, but the mode is switched to Insert mode (i.e. 'delete' become 'change').

Find/replace text

:%s/abc/xyz/g find/replace abc with xyz in whole document
:5,23s/abc/xyz/g find/replace abc with xyz in lines 5 to 23
:.s/abc/xyz/g find/replace abc with xyz in current line
:.s/abc/xyz/gc same as above, and ask permission for each change

Indenting text

>> shift to the right
<< shift to the left

z commands

Vertically aligning text. The .vimrc file contains code which, when z/ is pressed, toggles the highlighting for the word under the cursor.

zz move current line to centre of the screen
zt move current line to the top of the screen
zb move current line to the bottom of the screen

Changing to uppercase/lowercase

u make line lowercase (in Visual mode)
U make line uppercase (in Visual mode)
guw make word lowercase (in Command mode)
gUw make word uppercase (in Command mode)
~ change the case of a single letter

These might come in handy when you select lines (in Visual mode) and try to y yank a few lines, but miss y and hit u (and then everything becomes lowercase). In such situations, undo u is your best friend. But, you are probably not as unkeyboardinated as me.

Using registers

There are times when you may wish to keep several items in the clipboard, but yanking only with y will overwrite the clipboard every time it is used. Registers are, effectively, separate clipboards to which you can save text. Registers a, b, ..., z are free to store text.

"ayy save the current line to register a
"ap print contents of register a
"by yank selection into register b (Visual mode)
:reg + Enter see the contents of all the registers

The registers 1, 2, ..., 9 store the most recent deletes with d, while register 0 holds the most recent yank only using y.

Recording keystrokes

Similarly to using registers to save text, you can also record keystrokes to registers a, b, ..., z. This will allow you to repeat edits you make on one line (to other lines). Note that you will need to use i and <Esc> to change to Insert/Command mode during the recording.

qa start recording keystrokes to register a
q stop recording
@a execute keystrokes from register a on current line
2@b execute keystrokes from register b on the next two lines

Note: All the commands above are executed in Command mode.

General

. repeat last command
<C-space> autocomplete (in Insert mode) (Ctrl-space)
<C-N> same as Ctrl-space

.vimrc

Recall that your vimrc file is located here:

vim ~/.vimrc