Vim Cheat Sheet

Back to Top
Vim Cheat Sheet
Table of Contents
  1. Ctrl+W Assignments
  2. Leader Assignments
  3. Keyboard Shortcuts
  4. Cursor Movement
  5. Insert Mode - ways to commence editing
  6. Editing
  7. NERDtree
  8. Working with Multiple Files
  9. Tabs
  10. Marking text (visual mode)
  11. Visual Commands
  12. Marks
  13. Cut and Paste
  14. Registers
  15. Search & Replace
  16. Environment/Interface
  17. Exiting
Ctrl+W Assignments

For an exhaustive list of all Ctrl+W syntax, click Vim documentation: windows

Ctrl+W +/-increase/decrease window height; prefix with a number, e.g.: 20<C-w>+
Ctrl+W _increase/decrease window height; prefix with a number, e.g.: 50<C-w>_
Ctrl+W >/<increase/decrease window width; prefix with a number, e.g.: 30<C-w><
Ctrl+W |increase/decrease window width; prefix with a number, e.g.: 30<C-w>|
Ctrl+W =equalise width and height of all windows
Ctrl+W sSplit current window in two horizontally
Ctrl+W vSplit current window in two vertically
Ctrl+W nCreate a new window and start editing an empty file in it.
Ctrl+W Ctrl+J, Ctrl+W downMove cursor to Nth window below current one.
Ctrl+W Ctrl+k, Ctrl+W upMove cursor to Nth window above current one.
Ctrl+W Ctrl+H, Ctrl+W lft, Ctrl+W backspMove cursor to Nth window left of current one.
Ctrl+W Ctrl+l, Ctrl+W rightMove cursor to Nth window right of current one.
Ctrl+W Shift-TMove buffer to a tab
Leader Assignments
, F5NerdTree file manager using directory of current file
, cdSet working directory to path of current file
, dDuplicate current line
, evOpen .vimrc in a new tab for editing
, fnEcho currently edited file's name to system clipboard
, ln
, tn
Toggle appearance of line numbers between absolute consecutive order and relative to current line
, nTurn off find highlighting — :nohl
, svsource .vimrc (reload settings)
, vexecute paste from system clipboard
, ypaste from system clipboard
, + (non-keypad)vertical resize +5 columns
, - (non-keypad)vertical resize -5 columns
Cursor Movement
hmove left
jmove down
kmove up
lmove right
Mmove to middle of screen
Lmove to bottom of screen
wjump forwards to the start of a word
Wjump forwards to the start of a word (words can contain punctuation)
ejump forwards to the end of a word
Ejump forwards to the end of a word (words can contain punctuation)
bjump backwards to the start of a word
Bjump backwards to the start of a word (words can contain punctuation)
0jump to the start of the line
^jump to the first non-blank character of the line
$jump to the end of the line
g_jump to the last non-blank character of the line
gggo to the first line of the document
Ggo to the last line of the document
5Ggo to line 5
fxjump to next occurrence of character x
txjump to before next occurrence of character x
}jump to next paragraph (or function/block, when editing code)
{jump to previous paragraph (or function/block, when editing code)
zzcenter cursor on screen
Ctrl+bmove back one full screen
Ctrl+fmove forward one full screen
Ctrl+dmove forward ½ a screen
Ctrl+umove back ½ a screen
Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.
 Back to Table of Contents
Insert Mode - ways to commence editing
iinsert before the cursor
Iinsert at the beginning of the line
ainsert (append) after the cursor
Ainsert (append) at the end of the line
oappend (open) a new line below the current line
Oappend (open) a new line above the current line
eainsert (append) at the end of the word
Escexit insert mode
 Back to Table of Contents
NERDtree

Files

o:open in previous window
go:preview
t:open in new tab
T:open in new tab silently
i:open split
gi:preview split
s:open vsplit
gs:preview vsplit

Directories

o:open & close
O:recursively open
x:close parent
X:close all children recursively
e:explore selected directory

Bookmarks

o:open bookmark
t:open in new tab
T:open in new tab silently
D:delete bookmark

Filesystem

C:change tree root to selected dir
u:move tree root up a directory
U:move tree root up a directory but leave old root open
r:refresh cursor directory
R:refresh current root
m:show menu
cd:change the CWD to the selected dir
CDchange tree root to CWD
CommandExplain

Tree navigation

p:go to parent
P:go to root
K:go to first child
J:go to last child
Ctrl+k:go to previous sibling
Ctrl+j:go to next sibling

Tree filtring

I:hidden files (off)
f:file filters (on)
F:files (on)
B:bookmarks (off)

Other Commands

q:Close the NERDtree window
A:toggle zoom NERDtree window
?:toggle help
Editing
rreplace a single character
Jjoin line below to the current one
cc or Schange (replace) entire line
cwchange (replace) to the end of the word
c$change (replace) to the end of the line
sdelete character and substitute text
xptranspose two letters (delete and paste)
uundo
Ctrl+Rredo
. (period)repeat last command
 Back to Table of Contents
Working with Multiple Files
:e fileedit a file in a new buffer
:bnext or :bngo to the next buffer
:bprev or :bpgo to the previous buffer
:bddelete a buffer (close a file)
:lslist all open buffers
:sp fileopen a file in a new buffer and split window
:vsp fileopen a file in a new buffer and vertically split window
Ctrl + wssplit window
Ctrl + wwswitch windows
Ctrl + wqquit a window
Ctrl + wvsplit window vertically
Ctrl + whmove cursor to the left window (vertical split)
Ctrl + wlmove cursor to the right window (vertical split)
Ctrl + wjmove cursor to the window below (horizontal split)
Ctrl + wkmove cursor to the window above (horizontal split)
 Back to Table of Contents
Marking text (visual mode)
vstart visual mode, mark lines, then do a command (like y-yank)
Vstart linewise visual mode
omove to other end of marked area
Ctrl + vstart visual block mode
Omove to other corner of block
awmark a word
aba block with ()
aBa block with {}
ibinner block with ()
iBinner block with {}
Escexit visual mode
 Back to Table of Contents
Visual Commands
>shift text right
<shift text left
yyank (copy) marked text
ddelete marked text
~ (tilde)switch case
 Back to Table of Contents
Cut and Paste
yyyank (copy) a line
2yyyank (copy) 2 lines
ywyank (copy) the characters of the word from the cursor position to the start of the next word
y$yank (copy) to end of line
pput (paste) the clipboard after cursor
Pput (paste) before cursor
dddelete (cut) a line
2dddelete (cut) 2 lines
dwdelete (cut) the characters of the word from the cursor position to the start of the next word
Ddelete (cut) to the end of the line
d$delete (cut) to the end of the line
xdelete (cut) character
 Back to Table of Contents
Marks
:markslist if marks
maset current position for mark A
`ajump to position of mark A
y`ayank text to position of mark A
 Back to Table of Contents
Registers
:regshow registers content
"xyyank into register x
"xppaste contents of register x
Registers are stored in ~/.viminfo, and will be loaded again on next restart of vim.
Register 0 contains always the value of the last yank command.
 Back to Table of Contents
Search & Replace
/patternsearch for pattern
?patternsearch backward for pattern
\vpattern'very magic' pattern: non-alphanumeric characters are interpreted as special regex symbols (no escaping needed)
nrepeat search in same direction
Nrepeat search in opposite direction
:%s/old/new/greplace all old with new throughout file
:%s/old/new/gcreplace all old with new throughout file with confirmations
:nohremove highlighting of search matches
 Back to Table of Contents
Search in multiple files
:vimgrep /pattern/ {file}search for pattern in multiple files
e.g: :vimgrep /foo/ **/*
:cnjump to the next match
:cpjump to the previous match
:copenopen a window containing the list of matches
 Back to Table of Contents
Tabs
:tabnew or
:tabnew file
open a file in a new tab
Ctrl+wTmove the current split window into its own tab
gt
:tabn
:tabnext
move to the next tab
gT
:tabprev
:tabp
move to the previous tab
#gtmove to the tab number #
:tabmove #move current tab to the #th position (indexed from 0)
:tabc or
:tabclose
close the current tab and all its windows
:tabo or
:tabonly
close all tabs except for the current one
:tabdorun the command on all tabs (e.g. :tabdo q - closes all opened tabs)
 Back to Table of Contents
Exiting
:wwrite [save] file but don't exit
:wqwrite [save]
:qquit [fails if file has changed]
:q!quit and throw away changes
 Back to Table of Contents

Leave a Reply

Your email address will not be published. Required fields are marked *