Developer Tools: Vim keyboard shortcuts

January 5th, 2013

Warning: This post is 11 years old. Some of this information may be out of date.

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.

Vim Keyboard Shortcuts

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