- Devhints (devhints.io)
- Vim cheatsheet (vim.rotrr.com)
- Vim documentation (vimdoc.sourceforge.net)
- Interactive Vim tutorial (openvim.com)
Also see
Miscellaneous
Tricks
Remove duplicate lines
:sort | %!uniq -u
To number the lines in the file
:%!cat -n
Copy whole doc to clipboard
:%w !pbcopy # Mac OS X
:%w !xclip -i -sel c # GNU/Linux
:%w !xsel -i -b # GNU/Linux
Command line
Shortcut | Description |
---|---|
<C-r><C-w> |
Insert current word into the command line |
<C-r>" |
Paste from " register |
<C-x><C-f> |
Auto-completion of path in insert mode |
Shell
- | - |
---|---|
:!<shell> |
Interpret Shell Command |
:r!<shell> |
Read in output of shell |
:r!date |
Insert date |
:!!date |
Replace current line with date |
Calculator
Shortcut | Description |
---|---|
<C-r> = 7*7 |
Shows the result |
<C-r> = 10/2 |
Shows the result |
Do this in INSERT mode |
Marks
Shortcut | Description |
---|---|
`^ |
Last position of cursor in insert mode |
`. |
Last change in current buffer |
`" |
Last exited current buffer |
`0 |
In last file edited |
'' |
Back to line in current buffer where jumped from |
```` | Back to position in current buffer where jumped from |
`[ |
To beginning of previously changed or yanked text |
`] |
To end of previously changed or yanked text |
`< |
To beginning of last visual selection |
`> |
To end of last visual selection |
ma |
Mark this cursor position as a |
`a |
Jump to the cursor position a |
'a |
Jump to the beginning of the line with position a |
d'a |
Delete from current line to line of mark a |
d a| Delete from current position to position of mark a` |
|
c'a |
Change text from current line to line of a |
y a| Yank text from current position to position of a` |
|
:marks |
List all current marks |
:delm a |
Delete mark a |
:delm a-d |
Delete marks a , b , c , d |
:delm abc |
Delete marks a , b , c |
Formatting
- | - |
---|---|
:ce 8 |
Center lines between 8 columns |
:ri 4 |
Right-align lines at 4 columns |
:le |
Left-align lines |
See :help formatting |
Tags
Shortcut | Description |
---|---|
:tag Classname |
Jump to first definition of Classname |
<C-]> |
Jump to definition |
g] |
See all definitions |
<C-t> |
Go back to last tag |
<C-o> <C-i> |
Back/forward |
:tselect Classname |
Find definitions of Classname |
:tjump Classname |
Find definitions of Classname (auto-select 1st) |
Counters
Shortcut | Description |
---|---|
<C-a> |
Increase number |
<C-x> |
Decrease number |
Navigating
Shortcut | Description |
---|---|
% |
Nearest/matching {[()]} |
[( * |
* [{ |
]) * |
* ]{ |
[m |
Previous method start |
[M |
Previous method end |
Misc command-lines
- | - |
---|---|
:h |
Help open help view |
:edit! |
Reload current file |
:2,8m0 |
Move lines 2 -8 to 0 |
:noh |
Clear search highlights |
:sort |
Sort lines |
:ter |
Open a terminal window |
:set paste |
Enable Insert Paste sub-mode |
:set nopaste |
disable Insert Paste sub-mode |
:cq |
Exiting with an error(aborting Git) |
Case
Shortcut | Description |
---|---|
vU |
Uppercase character |
vu |
Lowercase character |
~ |
Toggle case character |
viw U |
Uppercase word |
viw u |
Lowercase word |
viw ~ |
Toggle case word |
VU / gUU |
Uppercase line |
Vu / guu |
Lowercase line |
V~ / g~~ |
Toggle case line |
gggUG |
Uppercase all text |
ggguG |
Lowercase all text |
ggg~G |
Toggle case all text |
Vim Search and Replace
Substitute expression (magic)
- | - |
---|---|
& * |
* \0 |
\1 …\9 |
Replace with the group 0-9 |
\u |
Uppercase next letter |
\U |
Uppercase following characters |
\l |
Lowercase next letter |
\L |
Lowercase following characters |
\e |
End of \u , \U , \l and \L |
\E |
End of \u , \U , \l and \L |
Flags
- | - |
---|---|
g |
Replace all occurrences |
i |
Ignore case |
I |
Don't ignore case |
c |
Confirm each substitution |
Inverse :g
:[range]v/{pattern}/[command]
:v/foo/d |
Delete lines not containing foo (also :g!/foo/d ) |
Global command
:[range]g/{pattern}/[command]
:g/foo/d |
Delete lines containing foo |
:g!/foo/d |
Delete lines not containing foo |
:g/^\s*$/d |
Delete all blank lines |
:g/foo/t$ |
Copy lines containing foo to EOF |
:g/foo/m$ |
Move lines containing foo to EOF |
:g/^/m0 |
Reverse a file |
:g/^/t. |
Duplicate every line |
Ranges
- | - |
---|---|
% |
Entire file |
â<,â> |
Current selection |
5 |
Line 5 |
5,10 |
Lines 5 to 10 |
$ |
Last line |
2,$ |
Lines 2 to Last |
. |
Current line |
,3 |
Next 3 lines |
-3, |
Forward 3 lines |
Replace FILE
:%s/{pattern}/{str}/[flags]
:%s/old/new |
Replace first |
:%s/old/new/g |
Replace all |
:%s/old/new/gc |
Replace all (Confirm) |
:%s/old/new/gi |
Replace all (ignore case) |
:%s/\vold/new/g |
Replace all with regex |
Replace LINE
:[range]s/{pattern}/{str}/[flags]
:s/old/new |
Replace first |
:s/old/new/g |
Replace all |
:s/\vold/new/g |
Replace all with regex |
:s/old/new/gc |
replace all (Confirm) |
:s/old/new/i |
Ignore case replace first |
:2,6s/old/new/g |
Replace between lines 2 -6 |
Vim Multiple files
Tabs
Shortcut | Description |
---|---|
:tabe [file] |
Edit file in a new tab |
:tabf [file] |
Open if exists in new tab |
:tabc |
Close current tab |
:tabo |
Close other tabs |
:tabs |
List all tabs |
:tabr |
Go to first tab |
:tabl |
Go to last tab |
:tabm 0 |
Move to position 0 |
:tabn |
Go to next tab |
:tabp |
Go to previous tab |
#Normal mode
Shortcut | Description |
---|---|
gt |
Go to next tab |
gT |
Go to previous tab |
2gt |
Go to tab number 2 |
Windows
- | - |
---|---|
<C-w> s |
Split window |
<C-w> v |
Split window vertically |
<C-w> w |
Switch windows |
<C-w> q |
Quit a window |
<C-w> T |
Break out into a new tab |
<C-w> x |
Swap current with next |
<C-w> - / + |
Decrease/Increase height |
<C-w> < / > |
Decrease/Increase width |
<C-w> | |
Max out the width |
<C-w> = |
Equally high and wide |
<C-w> h / l |
Go to the left/right window |
<C-w> j / k |
Go to the up/down window |
Buffers
- | - |
---|---|
:e file |
Edit a file in a new buffer |
:bn |
Go to the next buffer |
:bp |
Go to the previous buffer |
:bd |
Remove file from buffer list |
:b 5 |
Open buffer #5 |
:b file |
Go to a buffer by file |
:ls |
List all open buffers |
:sp file |
Open and split window |
:vs file |
Open and vertically split window |
:hid |
Hide this buffer |
:wn |
Write file and move to next |
:tab ba |
Edit all buffers as tabs |
Vim Text objects
Misc
Shortcut | Description |
---|---|
c iw |
Change inner word |
c i" |
Change inner quotes |
c it |
Change inner tags (HTML) |
c ip |
Change inner paragraph |
y ip |
Yank inner paragraph |
y ap |
Yank paragraph (including newline) |
Selections
Shortcut | Description |
---|---|
v i" |
Select inner quotes "... " |
v a" |
Select quotes "..." |
v i[ |
Select inner brackets [... ] |
v a[ |
Select brackets [...] |
v iw |
Select inner word |
v ip |
Select inner paragraph |
v ipip |
Select more paragraph |
Delete
Shortcut | Description |
---|---|
d iw |
Delete inner word |
d is |
Delete inner sentence |
d i" |
Delete in quotes |
d a" |
Delete in quotes (including quotes) |
d ip |
Delete a paragraph |
Text objects
Shortcut | Description |
---|---|
p | Paragraph |
w | Word |
W | WORD (surrounded by whitespace) |
s | Sentence |
[ ( { < | A [], (), or {} block |
] ) } > | A [], (), or {} block |
' " ` | A quoted string |
b | A block [( |
B | A block in [{ |
t | A HTML tag block |
See :help text-objects |
Vim Operators
Counts
[count] <operator> <motion>
<operator> [count] <motion>
Combination | Description |
---|---|
2d d |
Delete 2 lines |
6y y |
Copy 6 lines |
d 3w |
Delete 3 words |
d 5j |
Delete 5 lines downwards |
> 4k |
Indent 4 lines upwards |
Examples
Combination | Description |
---|---|
d d |
Delete current line |
d j |
Delete two lines |
d w |
Delete to next word |
d b |
Delete to beginning of word |
d fa |
Delete until a char |
d /hello |
Delete until hello |
c c |
Change current line, synonym with S |
y y |
Copy current line |
> j |
Indent 2 lines |
ggd G |
Delete a complete document |
gg= G |
Indent a complete document |
ggy G |
Copy a whole document |
Available Operators
Shortcut | Description |
---|---|
d |
Delete |
y |
Yank (copy) |
c |
Change (delete then insert) |
p |
Paste |
= |
Formats code |
g~ |
Toggle case |
gU |
Uppercase |
gu |
Lowercase |
> |
Indent right |
< |
Indent left |
! |
Filter through external program |
Getting Started
Macros
- | - |
---|---|
qi |
Record macro i |
q |
Stop recording macro |
@i |
Run macro i |
7@i |
Run macro i 7 times |
@@ |
Repeat last macro |
You can save macro for any letters not just i |
Visual mode
Shortcut | Description |
---|---|
v |
Enter visual mode |
V |
Enter visual line mode |
<C-v> |
Enter visual block mode |
ggVG |
Select all text |
> / < |
Shift text right/left |
Repeating
Shortcut | Description |
---|---|
. |
Repeat last command |
; |
Repeat latest f , t , F or T |
, |
Repeat latest f , t , F or T reversed |
& |
Repeat last :s |
@: |
Repeat a command-line command |
Cut and paste
Shortcut | Description |
---|---|
x |
Delete character (Cut) |
p / P |
Paste after/before |
xp |
Swap two characters |
D |
Delete to end of line (Cut) |
dw |
Delete word (Cut) |
dd |
Delete line (Cut) |
ddp |
Swap two lines |
yy |
Yank line (Copy) |
"*p * |
* "+p |
"*y * |
* "+y |
#In visual mode
Shortcut | Description |
---|---|
d * |
* x |
s |
Replace selection |
y |
Yank selection (Copy) |
Normal Mode
Shortcut | Description |
---|---|
r |
Replace one character |
R |
Enter Replace mode |
u / 3u |
Undo changes 1 / 3 times |
U |
Undo changes on one line |
J |
Join with next line |
<C-r> / 5 <C-r> |
Redo changes 1 / 5 times |
Saving and Exiting
Shortcut | Description |
---|---|
:w |
Save |
:q |
Close file |
:wq * |
* :x * |
:wqa |
Save and quit all files |
:q! * |
* ZQ |
:qa |
Close all files |
:qa! |
Force quit all files |
:w now.txt |
Write to now.txt |
:sav new.txt |
Save and edit new.txt |
:w !sudo tee % |
Write to readonly file |
Insert Mode
Shortcut | Description |
---|---|
i / a |
Insert before/after cursor |
I / A |
Insert start/end of line |
o / O (letter) |
Insert new line below/above |
s / S |
Delete char/line and insert |
C / cc |
Change to end of/current line |
gi |
Insert at last insert point |
Esc * |
* <C-[> |
Motions
Shortcut | Description |
---|---|
h * |
* j * |
<C-u> / <C-d> |
Half-page up/down |
<C-b> / <C-f> |
Page up/down |
#Words
Shortcut | Description |
---|---|
b / w |
Previous/Next word |
ge / e |
Previous/Next end of word |
#Line
Shortcut | Description |
---|---|
0 (zero) / $ |
Start/End of line |
^ |
Start of line (non-blank) |
#Character
Shortcut | Description |
---|---|
Fe / fe |
Move to previous/next e |
To / to |
Move before/after previous/next o |
| / n| |
Go to first/n th column |
#Document
Shortcut | Description |
---|---|
gg / G |
First/Last line |
:n * |
* nG |
} / { |
Next/Previous empty line |
#Window
Shortcut | Description |
---|---|
H / M / L |
Top/Middle/Bottom screen |
zt / zz / zb |
Top/Center/Bottom this line |
Motion Diagrams
â¼/ⶠCursor â½/â· Target
#Left-right motions
ââââââââââââââ |
ââââââââââââââ 0 $ âââââââââââââââ
â âââââââââââ ^ fe âââââââââ â
â â ââââââââ Fo te âââââââââ â
â â ââââââââ To 30| ââââ ââ â
â â ââ âââââ ge w ââââ â ââ â
â â ââ â âââ b e ââ â â ââ â
â â ââ â â âh lâ â â â ââ â
â½ â½ â½â½ â½ â½ â½â¼ â¼â½ â½ â½ â½ â½â½ â½
echo "A cheatsheet from quickref.me"
#Up-down motions
- SCREEN 1 START
âââ¬ââââââââââ· #!/usr/bin/python
â â âââââ·
â â â print("Hello")
â â { } ⶠprint("Vim")
â â â print("!")
â â âââ·
â â âââââ¬ââââ· print("Welcome")
G gg H M L k j ⶠprint("to")
â â âââ· print("quickref.me")
â â print("/vim")
â â
â âââââââ·
â - SCREEN 1 END
ââââââââââââââââ· print("SCREEN 2")