Vim Cheatsheet
Reference:
Navigation
| Keys | Action |
|---|---|
h,j,k,l |
Equivalent to left, down, up and right arrow key |
C-f |
Forward one screen |
C-b |
Backward one screen |
gg |
To first line |
G |
To last line |
:<n> <n>gg |
Jump to line <n> |
w |
To beginning of next word |
b |
To beginning of previous word |
W |
To beginning of next space-delimited word |
B |
To beginning of previous space_delimited word |
e |
To end of current/next word |
E |
To end of current/next space-delimited word |
0 |
To beginning of current line |
$ |
To end of current line |
( |
To beginning of current/previous sentence |
) |
To beginning of next sentence |
f<c> |
To next character <c> on current line |
F<c> |
To previous character <c> on current line |
% |
To matching parenthesis (){} |
Basic Edit Shortcuts
| Keys | Action |
|---|---|
a |
Append after current character position |
cw |
Change current word |
C |
Change to end of current line |
cc |
Change whole of current line |
dd |
Delete current line |
dw |
Delete current word |
D |
Delete to end of current line |
:<x>,<y>d |
Delete lines <x> through <y> |
J |
Join line |
o |
Add a new line after current line and start edit mode |
O |
Add a new line before current line and start edit mode |
r<c> |
Replace current character with <c> |
R |
Replace many characters (overwrite mode) |
x |
Delete current character |
. |
Repeat last edit operation |
u |
Undo last change |
U |
Undo all changes on current line |
Selection, Copy and Paste
Visual mode
| Keys | Action |
|---|---|
v |
Enter visual mode to select by character |
V |
Enter visual mode to select by line |
y |
Yank selected text into copy buffer |
yy |
Yank current line contents into copy buffer |
p |
Paste text from copy buffer after current character position |
P |
Paste text from copy buffer at current character position |
d |
Delete selected text and insert into copy buffer |
Visual Block mode
| Keys | Action |
|---|---|
C-v |
Enter Visual Block mode to select by characters |
o |
Select opposite corner of text selection block to move |
c |
Change (replace) text in selected block |
d |
Delete text in selected block |
P |
Paste selected block at current position |
p |
Paste selected block after current position |
S-i |
Insert at position of selected block; Hit Esc to finalise edit |
S-a |
Append after position of selected block; Hit Esc to finalise edit |
Searching
| Keys | Action |
|---|---|
/<string> |
Search forward for <string> |
?<string> |
Search backward for <string> |
n |
Go to next match |
N |
Go to previous match |
* |
Search for next instance of current word |
# |
Search for previous instance of current word |
Search and Edit
| Keys | Action |
|---|---|
:s/<pattern>/<string>/<flag> |
Replace <pattern> with <string> on current line |
:%s/<pattern>/<string>/<flag> |
Replace <pattern> with <string> throughout the file |
:g/<pattern>/d |
Search for all lines matching <pattern> and delete these lines |
:v/<pattern>/d |
Search for all lines not matching <pattern> and delete these lines |
Flags
| Flag | Action |
|---|---|
g |
Replace globally (all occurrences of <patter>) |
c |
Confirm before replacing |
Working with Windows
| Keys | Action |
|---|---|
C-w v |
Split window vertically |
C-w s |
Split window horizontally |
C-w w |
Go to next window |
C-w <arrow key> |
Go to window in direction of arrow key |
:close |
Close current window |
C-w | |
Maximise current window horizontally |
C-w _ |
Maximise current window vertically |
C-w = |
Make all windows equal in size |
C-w {n} > |
Increase window width by {n} characters (default {n}=1) |
C-w {n} < |
Decrease window width by {n} characters (default {n}=1) |
C-w {n} + |
Increase window height by {n} characters (default {n}=1) |
C-w {n} - |
Decrease window height by {n} characters (default {n}=1) |
Working with Tabs
| Keys | Action |
|---|---|
:tabnew <file> |
Create <file> in new tab |
:tabedit <file> |
Open <file> in new tab |
gt |
Go to next tab |
gT |
Go to previous tab |
<n>gt |
Go to tab number <n> |
:tabfirst |
Go to first tab |
:tablast |
Go to last tab |
:tabs |
List all tabs |
:tabclose |
Close current tab |
:tabonly |
Close all tabs except current tab |
Shell Commands
| Keys | Action |
|---|---|
:r ! <command> |
Read in output of <command> into new lines after current line |
:<n> ! <command> |
Replace line <n> with output from <command> |
:<m>,<n> ! <command> |
Replace contents of lines <m> through <n> with output of <command> |
:% ! <command> |
Replace contents of buffer with output of <command> |
:sh |
Temporary exit from Vim into a shell; Exit from shell to return to Vim |
Working with Macros
| Keys | Action |
|---|---|
q<a> |
Start recording commands and keystrokes into register <a> |
<Esc>q |
Stop recording |
@<a> |
Playback macro in register <a> |
@@ |
Playback last played macro |