Vim Cheatsheet

Edit

Go top
dd
Delete line.
Cut and save line to buffer.
D
Delete to the end of line.
cc
Change line.
C
Change to the end of line.
i
Insert.
I
Line beginning edit.
a
Append.
A
End of line append.
$
End of line.
^
Beginning of line.
s
Replace and insert.
S
Change line.
ci[t,',",{,},[,]]
Change inside.
Change inside tag cit.
di[t,',",{,},[,]]
Delete inside.
Delete inside tag dit.
J
Remove <Enter> char. Join lines.
o
New line.
O
Insert line above.
gt
Go to file under cursor.
:e {file}
Edit {file}.
{visual}!{command}
Insert {command} output in {visual} selection.
<c-w>
Delete word before cursor.
Insert mode.
(
Move sentences backward.
)
Move sentences forward.
{
Move paragraph backward.
}
Move paragraph forward.
Go top
/
Find forward.
?
Find backwards.
*
Highlight cursor text in all files.
gd
Go to definition.
K
Go to man page of cursor text.

Replace

Go top
:%s/<c-r><c-w>/{replace_text}/[modifiers]
Find cursor word.
Modifier g, global.
Modifier c, request confirmation.
:%s/<c-r>{reg}/{replace_text}/[modifiers]
Find for register {reg}.
"_"0p
Replace visual selected text with last yanked text.
0 can be any register.
:s/\%V{pattern}/{replace}/g
Replace in selected text.

Completion

Go top
<c-n>
Find next match for words that start with the keyword in front of the cursor.
Next suggestion.
<c-p>
Find previous match for words that start with the keyword in front of the cursor
Previous suggestion.
<c-x><c-i>
Complete searching in language specific docs.
<c-x><c-o>
In C/C++ complete using methods, for example.

Insert

Go top
Ni{char}<ESC>
Repeat {char} N times.
:set im
Set insert mode.
<c-l> leave insert mode.

Indent

Go top
>
Right indent.
<
Left indent.
<c-t>
Right indent in insert mode.
<c-d>
Left indent in insert mode.
=
Automatic reformatting.
gg=G
Reformat all document.

Undo

Go top
u
Undo.
U
Undo all line changes.
:undolist
Changes list.
:earlier {count} || {N}h || {N}m || {N}s
Return to {count} || {N}h || {N}m || {N}s change.
:later {count} || {N}h || {N}m || {N}s
Go to {count} || {N}h || {N}m || {N}s change.
g-
Go to older text state.
g+
Go to newer text state.
:undo N
Undo to N branch.

Redo

Go top
<c-r>
Redo changes.
@:
Repeat last command mode command.

Select

Go top
v
Select.
Press o will move to the beginning or end of visual selected text.
V
Line select.
<c-v>
Block select.
gv
Select last selected text.

Fold

Go top
:set foldmethod=manual || indent || syntax || expr || marker || diff
manual: set manual folds.
syntax: fold based on file syntax.
marker: based on marks.
expr: based on expressions.
diff: fold away lines that not differ.
zf
Create fold.
zc
Close fold.
zo
Open fold.
za
Toggle fold.
zr
Reduce fold.
zm
More fold.
zC
Close fold in all levels.
zO
Open fold in all levels.
zA
Toggle fold in all levels.
zR
Reduce fold in all levels.
zM
More fold in all levels.

Modes

Go top
<ESC> || <c-c>
Leave insert mode.
<c-o>
Leave insert mode for one command, then return to insert mode.
:Q
Enter command-line mode, Ex mode.
:visual || :vi leave command mode.

Copy

Go top
y
Yank cursor o selected text.
yy
Same as Y.
Yank line.
"+yy
Yank line to system buffer.

Paste

Go top
p
Paste below.
P
Paste above.
"+gP
Paste from system buffer.

Registers

Go top
:reg
List all registers.
"{N|char}yy || V"ky
Copy line to register {N|char}.
"{N|char}p
Paste from {N|char} register.
<c-r>{reg}
Insert {reg}.
Insert mode.
<c-r><c-r>{reg}
Insert {reg} literally.

Macros

Go top
q{N|char}
Record a macro in register {N|char}.
[N]@{N|char}
Execute a macro N times.
@@
Releat last one.

Marks

Go top
:marks
List marks.
m[a-zA-Z]
Set mark named [a-zA-Z].
[A-Z] for global use.
'[a-zA-Z]
Go to mark named [a-zA-Z].
`[a-zA-Z]
Go to mark named [a-zA-Z], column precision.
d'{mark}
Delete from current position to {mark} position.
:delmarks {mark[-mark]}
Delete mark, or marks range.
]'
Jump to next lowercase mark.
['
Jump to prev lowercase mark.
`.
Jump to position where last change occurred in current buffer.
`[0-N]
Jump to position in last file edited (when exited Vim).
''
Jump back (to line in current buffer where jumped from).
``
Jump back (to position in current buffer where jumped from).

Wrap

Go top
:set wrap
Wrap file.
:set nowrap
Unwrap file.
:set wrapwidth=N
Set wrap to N width.
:set wrapmargin=N
Set wrap to N margin.

Tabs

Go top
<c-wT>
Create a new tab from current buffer.
gt
Go to next tab.
gT
Go to prev tab.
$vim -p file1 [fileN]
Open files in tabs.
:tabe file
Edit file in new tab.
:tabclose
Close current tab.
:tabonly
Close other tabs.
:tab ball
Show each buffer in a new tab.
:tab drop {file}
Open {file} in a new tab, or jump to a window/tab containing the file if there is one.
:tab plit
Copy the current window to a new tab of its own.
:tabs
List all tabs.
:tabn
Go to next tab.
:tabp
Go to prev tab.
<c-PgDn>
Go to next tab.
<c-PgUp>
Go to prev tab.
Tab navigation
If the target buffer is already displayed in a window in one of the tabs, that window will be displayed. Otherwise, the current window will be split, and the target buffer will be displayed in the new window.
set switchbuf=usetab
nnoremap <F8> :sbnext<CR>
nnoremap <S-F8> :sbprevious<CR>
:bN
Open buffer N in a new tab.

Diff

Go top
:diffthis
Diff this file.
:diffget || do
Get changes from file.
:diffput || dp
Put changes to file.
:diifupdate
Update diff.
]c
Go to next change.
[c
Go to prev change.

Text transform

Go top
~
Upper, lowercase cursor text.
guu
Lowercase all line.
gUU
Uppercase all line.
xp
Change char position.
<c-a>
Increment cursor number.
<c-x>
Decrement cursor number.
:sort
Sort selected lines.
:sort!
Inverse sort selected lines.
:sort u
Sort deleting repated lines.

Abbreviations

Go top
:ab
List all abbreviations.
:ab {name} {definition}
Define {definition} as {name}.
:una {name}
Unabbreviate {name}.
:abc
Unabbreviate all abbreviations.
<c-v>
Avoid abbreviation expansion.

g

Go top
ga
Print ascii value of character under the cursor.
gd
Go to definition of word under the cursor in current function.
ge
Go backwards to the end of the previous word.
gf
Start editing the file whose name is under the cursor.
gF
Start editing the file whose name is under the cursor and jump to the line number following the filename.
{N}gs
Go to sleep for {N} seconds.
gm
Go to character at middle of the screenline.

Open file under cursor

Go top
gf
Open in same window.
Works for visual selected.
<c-wf>
Open in a new window.
<c-wgf>
Open in a new tab.

Move

Go top
w
Word movement with spaces.
e
Word movement without spaces.
b
Word backwards movement without spaces.
G
Go to bottom.
:N
Go to line number N.
<c-o>
Go to prev position.
<c-i>
Go to next position.

Jumps

Go top
:jumps
List all jumps.
<c-i>
Jump to previous location.
<c-o>
Jump to next location.
g;
Go to older position in change list.
Can use a count.
g,
Go to newer position in change list.
Can use a count.
:changes
Print the change list.
Ng; go to change N. Then can use g, to go to next change.
]m
Go to next start of a method.
[m
Go to previous start of a method.
]M
Go to next end of a method.
[M
Go to previous end of a method.
'[
To the first character of the previously changed or yanked text.
Use ` for column precision.
']
To the last character of the previously changed or yanked text.
Use ` for column precision.
'<
To the first line or character of the last selected Visual area in the current buffer
Use ` for column precision.
'>
To the last line or character of the last selected Visual area in the current buffer
Use ` for column precision.
''
To the position before the latest jump.
Use ` for column precision.
'"
To the cursor position when last exiting the current buffer.
Use ` for column precision.
'^
To the position where the cursor was the last time when Insert mode was stopped.
Use ` for column precision.
'.
To the position where the last change was made.
Use ` for column precision.

Motions

Go top
f{char}
Move to {char} in line.
; repeat command.
F{char}
Move backwards to {char}.
t{char}
Move to one position before {char}.
T{char}
Move backwards to one position after {char}.
;
Go to next match.
,
Go to prev match.

Scroll

Go top
<c-e>
Scroll bottom.
<c-y>
Scroll top.
<c-d>
Scroll half-window viewport bottom.
<c-f>
Scroll window viewport bottom.
<c-u>
Scroll half-window viewport top.
<c-b>
Scroll window viewport top.
zt
Scroll window top, cursor position.
zz
Scroll window middle, cursor position.
zb
Scroll window bottom, cursor position.

History

Go top
q:
Show history in a split.
<CR> repeat command.

.vimrc

Go top
:source {file}
Reload .vimrc file.

Session

Go top
:mksession {name}
Create a session file.
:source {file}
Load vim session.

Shell

Go top
:shell || :sh
"Minimize" vim and start a shell.
exit || <c-d> to return vim.
:!{command}
Execute {command} and print output.
:r! {command} || :read! {command}
Execute {command} and insert output in cursor position.
:!./%
Execute current file.
% is equal to filename.

C/C++

Go top
:cn
Go to next error.
:cp
Go to prev error.
:cc
Display error.
:cl
Display error list.
:cw
Display error list in window.
:cope
Display error list in a new window.
:ccl
Close error list window.

Grep

Go top
:grep {what} {where}
Grep {what} in {where}.
:cl
Open match list.
:cw
Open match list in window.
:ccl
Close match list.

Tags

Go top
$ctags {files}
Create tags file.
<c-]> || :tg {tag_name}
Go to tag definition.
<c-t>
Return back to function call.

Plugins

Go top

NERDCommenter

Go top
<\c >
Toggle comment current line or selection. Automatic detection, line by line.
<\cm>
Comments block.
<\cn>
Comments block, force nesting.
<\ci>
Toggles the comment state of the selected line(s) individually.
<\cs>
Comments out the selected lines 'sexily'.
<\cy>
Same as <\cc> except that the commented line(s) are yanked first.
<\c$>
Comments the current line from the cursor to the end of line.

Tabular

Go top
:Tab[ularize] /=
Tabularize using = symbol.

Matchit

Go top
%
Go to element open/close tag.

Surround

Go top
vst
Surround tag.
VSt
Surround tag, smart indent.
dst
Delete surround tag.
vcst
Change surround tag.

Snipmate

Go top
<c-r><TAB>
In insert mode, list snippets.

Closetag

Go top
<c-_>
Close last opened tag.
html, xml.

Taglist

Go top
:TlistToggle
Toggle taglist.
:TlistAddFiles
Add files to taglist.
:TlistAddFilesRecursive
Recursively add multiple files from a directory tree.
:TlistUpdate
Update taglist.
:TlistHighlightTag
Highlight the current tag in the taglist window.

Fugitive

Go top
:Gdiff
Diff current file.
:Gstatus
Repo status.
Use - to move changes.
:Gblame
Vertical split with $git blame output.
<RETURN> reblame the file as it stood in that commit.
o open split.
:Gedit go back to the work tree version.
:Gmove
$git mv
:Gremove
$git rm
:Ggrep
Search the work tree (or any arbitrary commit).
:Glog
Loads all previous revisions of a file into the quickfix list.

Window

Go top
L
Go to window bottom.
H
Go to window top.
M
Go to window middle.
N%
Go to window N%.

Splits

Go top
<c-wL>
Move split to right.
Convert to vertical split.
<c-wH>
Move split to left.
Convert to vertical split.
<c-wJ>
Move split to bottom.
Convert to horizontal split.
<c-wK>
Move split to top.
Convert to horizontal split.
<c-wq>
Quit current split.
<c-wc>
Close current split.
<c-ww>
Change to next window, cycle.
<c-w[left, up, right, bottom]>
Go to split.
:vs {file}
Vertical split to edit {file}.

Buffers

Go top
:ls
List all buffer.
:bd [N]
Delete buffer.
:bN
Go to N buffer.
:bn
Go to next buffer.
:bp
Go to prev buffer.
:b {N}
Edit buffer N.
:vert sb {N}
Vertical split to edit buffer N.
:badd {file}
Add {file} to buffer list.

File

Go top
:w
Save file.
:w!
Force save file.
:q
Quit file.
:q!
Force quit file.
:qa!
Force quit all files.
:wa!
Force save all files.
:wqa
Save and quit all.
ZZ
Save and quit.

Encrypt

Go top
$vim -x [file]
Open encrypted file.
:X
Encrypt current file.
:set key=
Disable encrypt in current file.

Open files

Go top
$vim +{any :command} {file}
Execute {any :command} in {file}.