I regularly use Vim but have to confess that I'm not a 'power' user. To help me learn Vim keyboard shortcuts I have noted them down here.
File Operations
|
Vim command
|
<td>
<em>Action</em>
</td>
:e filename
|
<td>
Open a new file. Use the tab key for file name completion.
</td>
:w filename
|
<td>
Save changes to a file. If you don't specify a file name, Vim saves as the file name you were editing. For saving the file under a different name, enter the file name.
</td>
:q
|
<td>
Quit Vim. If you have unsaved changes, Vim refuses to exit.
</td>
:q!
|
<td>
Exit Vim without saving changes.
</td>
:wq
|
<td>
Write the file and exit.
</td>
:x
|
<td>
Almost the same as :wq, write the file and exit if you've made changes to the file. If you haven't made any changes to the file, Vim exits without writing the file.
</td>
Moving around the file
|
These work in both command and visual modes.
|
Vim command
|
<td>
<em>Action</em>
</td>
j or Up Arrow
|
<td>
Move the cursor up one line.
</td>
k or Down Arrow
|
<td>
Down one line.
</td>
h or Left Arrow
|
<td>
Left one character.
</td>
l or Right Arrow
|
<td>
Right one character.
</td>
e
|
<td>
To the end of a word.
</td>
E
|
<td>
To the end of a whitespace-delimited word.
</td>
b
|
<td>
To the beginning of a word.
</td>
B
|
<td>
To the beginning of a whitespace-delimited word.
</td>
|
<td>
To the beginning of a line.
</td>
^
|
<td>
To the first non-whitespace character of a line.
</td>
$
|
<td>
To the end of a line.
</td>
H
|
<td>
To the first line of the screen.
</td>
M
|
<td>
To the middle line of the screen.
</td>
L
|
<td>
To the the last line of the screen.
</td>
:n
|
<td>
Jump to line number <em>n</em>. E.g, to jump to line 10, you'd type :10
</td>
Inserting and overwriting text
|
Vim command
|
<td>
<em>Action</em>
</td>
i
|
<td>
Insert before cursor.
</td>
I
|
<td>
Insert to the start of the current line.
</td>
a
|
<td>
Append after cursor.
</td>
A
|
<td>
Append to the end of the current line.
</td>
o
|
<td>
Open a new line below and insert.
</td>
O
|
<td>
Open a new line above and insert.
</td>
C
|
<td>
Change the rest of the current line.
</td>
r
|
<td>
Overwrite one character. After overwriting the single character, go back to command mode.
</td>
R
|
<td>
Enter insert mode but replace characters rather than inserting.
</td>
The ESC key
|
<td>
Exit insert/overwrite mode and go back to command mode.
</td>
Deleting text
|
Vim command
|
<td>
<em>Action</em>
</td>
x
|
<td>
Delete characters under the cursor.
</td>
X
|
<td>
Delete characters before the cursor.
</td>
dd or :d
|
<td>
Delete the current line.
</td>
Entering visual mode
|
Vim command
|
<td>
<em>Action</em>
</td>
v
|
<td>
Start highlighting characters. Use the normalmovement keys and commands to select text for highlighting.
</td>
V
|
<td>
Start highlighting lines.
</td>
The ESC key
|
<td>
Exit visual mode and return to command mode.
</td>
Editing blocks of text
|
Note: the Vim commands marked with (V) work in visual mode, when you've selected some text. The other commands work in the command mode, when you haven't selected any text.
|
Vim command
|
<td>
<em>Action</em>
</td>
~
|
<td>
Change the case of characters. This works both in visual and command mode. In visual mode, change the case of highlighted characters. In command mode, change the case of the character uder cursor.
</td>
> (V)
|
<td>
Shift right (indent).
</td>
< (V)
|
<td>
Shift left (de-indent).
</td>
c (V)
|
<td>
Change the highlighted text.
</td>
y (V)
|
<td>
Yank the highlighted text. In Windows terms, “copy the selected text to clipboard.”
</td>
d (V)
|
<td>
Delete the highlighted text. In Windows terms, “cut the selected text to clipboard.”
</td>
yy or :y or Y
|
<td>
Yank the current line. You don't need to highlight it first.
</td>
dd or :d
|
<td>
Delete the current line. Again, you don't need to highlight it first.
</td>
p
|
<td>
Put the text you yanked or deleted. In Windows terms, “paste the contents of the clipboard”. Put characters after the cursor. Put lines below the current line.
</td>
P
|
<td>
Put characters before the cursor. Put lines above the current line.
</td>
Undo and redo
|
Vim command
|
<td>
<em>Action</em>
</td>
u
|
<td>
Undo the last action.
</td>
U
|
<td>
Undo all the latest changes that were made to the current line.
</td>
Ctrl + r
|
<td>
Redo.
</td>
Search
|
Vim command
|
<td>
<em>Action</em>
</td>
/pattern
|
<td>
Search the file for <em>pattern</em>.
</td>
n
|
<td>
Scan for next search match in the same direction.
</td>
N
|
<td>
Scan for next search match but opposite direction.
</td>
Replace
|
Vim command
|
<td>
<em>Action</em>
</td>
:rs/foo/bar/a
|
<td>
Substitute <em>foo</em> with <em>bar</em>. <em>r</em> determines the range and <em>a</em> determines the arguments.
</td>
The range (r) can be
|
nothing
|
<td>
Work on current line only.
</td>
number
|
<td>
Work on the line whose number you give.
</td>
%
|
<td>
The whole file.
</td>
Arguments (a) can be
|
g
|
<td>
Replace all occurrences in the line. Without this, Vim replaces only the first occurrences in each line.
</td>
i
|
<td>
Ignore case for the search pattern.
</td>
I
|
<td>
Don't ignore case.
</td>
c
|
<td>
Confirm each substitution. You can type y to substitute this match, n to skip this match, a to substitute this and all the remaining matches (“Yes to all”), and q to quit substitution.
</td>
Examples
|
:452s/foo/bar/
|
<td>
Replace the first occurrence of the word <em>foo</em> with <em>bar</em> on line number 452.
</td>
:s/foo/bar/g
|
<td>
Replace every occurrence of the word <em>foo</em> with <em>bar</em> on current line.
</td>
:%s/foo/bar/g
|
<td>
Replace every occurrence of the word <em>foo</em> with <em>bar</em> in the whole file.
</td>
:%s/foo/bar/gi
|
<td>
The same as above, but ignore the case of the pattern you want to substitute. This replaces <em>foo</em>, <em>FOO</em>, <em>Foo</em>, and so on.
</td>
:%s/foo/bar/gc
|
<td>
Confirm every substitution.
</td>
:%s/foo/bar/c
|
<td>
For each line on the file, replace the first occurrence of <em>foo</em> with <em>bar</em> and confirm every substitution.
</td>