Vim Cheatsheet
last modified: | 8 min read #cheatsheet #tools #vimThis cheatsheet is focused on how to work with vanilla (Neo)Vim.
General
Help
:h[elp] keyword
- open help for keywordK
- open man page for word under cursor
Exiting
:x or ZZ
- writes (only if changes were made) and quit the file:wq or :x
- writes (even when no changes were made) and quit the file:wqa
- save all tabs and quit:q
- quit (fails if unsaved changes were made):q! or ZQ
- quit and throw away unsaved changes
File Operations
:w
- save the file:w !sudo tee %
- save the file using sudo:sav[eas] file
- save file as
Movement
Cursor Movement
h
- move cursor leftj
- move cursor downk
- move cursor upl
- move cursor right
Cursor Movement Relative to Screen
H
- move to top of screenM
- move to middle of screenL
- move to bottom of screenzz
- center cursor on screen
Occurence Movement
fx
- jump no next occurence of character xFx
- jump to previous occurence of character xtx
- jump to before next occurence of character xTx
- jump to after previous occurence of character x
Quick Movement
%
- move to matching character (default supported pairs:()
,{}
,[]
) - use:h matchpairs
in Vim for more information)^
- jump to the first non-blank character of the line$
- jump to the end of the lineg_
- jump to the last non-blank character of the linegg
- jump to the first line of the documentG
- jump to the last line of the document:5
- jump to line 5 in the document
Content Movement
w
- jump forwards to the start of a wordW
- jump forwards to the start of a word (words can contain punctuation)e
- jump to the end of a wordE
- jump to the end of a word (words can contain punctuation)b
- jump backwards to the start of a wordB
- jump backwards to the start of a word (words can contain punctuation)ge
- jump backwards to the end of a wordgE
- jump backwards to the end of a word (words can contain punctuation)
Screen Movement
Ctrl + e
- move the screen down one line (without moving the cursor)Ctrl + y
- move the screen up one line (without moving the cursor)Ctrl + b
- move back one full screenCtrl + f
- move forward one full screenCtrl + d
- move forward 1/2 a screenCtrl + u
- move backward 1/2 a screen
Declaration Movement
gd
- jump to local declarationgD
- jump to global declarationgf
- jump to file
Repeat Movement
;
- repeat previousf
,t
,F
,T
movement,
- repeat previousf
,t
,F
,T
movement backwards
Paragraph Movement
}
- jump to next paragraph (or function/block, when editing code){
- jump to previous paragraph (or function/block, when editing code)
Insert Mode
Basic Insertion
i
- insert before the cursorI
- insert at the beginning of the linea
- insert (append) after the cursorA
- insert (append) at the end of the line
Append
o
- append (open) a new line below the current lineO
- append (open) a new line above the current line
Newline Insertion
Ctrl + j
- begin new line during insert mode
Deletion
Ctrl + h
- delete the character before the cursor during insert modeCtrl + w
- delete word before the cursor during insert mode
Auto-complete Insertion
Ctrl + n
- insert (auto-complete) next match before the cursor during insert modeCtrl + p
- insert (auto-complete) previous match before the cursor during insert mode
Indentation
Ctrl + t
- indent (move right) line one shiftwidth during insert modeCtrl + d
- de-indent (move left) line one shiftwidth during insert mode
Exit Insert Mode
Esc
- exit insert modeCtrl + c
- exit insert modeCtrl + [
- exit insert mode
Register Insertion
Ctrl + rx
- insert the contents of register x
Temporal Normal Mode
Ctrl + ox
- Temporarily enter normal mode to issue one normal-mode command x.
Visual Mode
Basic Visual Mode
v
- start visual mode, mark lines, then do a command (like y-yank)V
- start linewise visual modeCtrl + v
- start visual block mode
Movement
o
- move to other end of marked areaO
- move to other corner of block
Exit Visual Mode
Esc
- exit visual mode
Marking
aw
- mark a wordab
- a block with ()aB
- a block with {}at
- a block with <> tags
Inner Marking
ib
- inner block with ()iB
- inner block with {}it
- inner block with <> tags
Editing
Replace
r
- replace a single character.R
- replace more than one character, until ESC is pressed.
Cut
cc
- change (replace) entire lineC
- change (replace) to the end of the linec$
- change (replace) to the end of the lineciw
- change (replace) entire wordcw
orce
- change (replace) to the end of the word
Delete and Insert
s
- delete character and substitute textS
- delete line and substitute text (same as cc)
Copying
y
- yank (copy) the marked text in visual modeyy
- yank (copy) a line2yy
- yank (copy) 2 linesyw
- yank (copy) the characters of the word from the cursor position to the start of the next wordyiw
- yank (copy) word under the cursoryaw
- yank (copy) word under the cursor and the space after or before ity$
- yank (copy) to end of line:#,#y
- yank (copy) the content of the given lines (e.g:34,65y
)
Paste
p
- put (paste) the clipboard after cursorP
- put (paste) before cursor
Repeat
.
- repeat last command
Join
J
- join line below to the current one with one space in betweengJ
- join line below to the current one without space in between
Case Switching
~
- switch case under cursorg~
- switch case up to motiongu
- change to lowercase up to motiongU
- change to uppercase up to motion
Indentation
==
- reindent line>>
- indent line<<
- de-indent line
Undo and Redo
u
- undoU
- restore (undo) last changed lineCtrl + r
- redo
Transpose
xp
- transpose two letters (delete and paste)
Reflow
gwip
- reflow paragraph
Deletion with Cut
dd
- delete (cut) a line2dd
- delete (cut) 2 linesdw
- delete (cut) the characters of the word from the cursor position to the start of the next worddiw
- delete (cut) word under the cursordaw
- delete (cut) word under the cursor and the space after or before itD
- delete (cut) to the end of the lined$
- delete (cut) to the end of the linex
- delete (cut) character:#,#d
- delete (cut) the content of the given lines (e.g:34,65d
)
Working with multiple files
Buffer Operations
:e[dit] file
- edit a file in a new buffer:bd[elete]
- delete a buffer (close a file):ls
or:buffers
- list all open buffers:tab ba[ll]
- edit all buffers as tabs
Split Operations
Ctrl + ws
- split windowCtrl + wv
- split window verticallyCtrl + ww
- switch windowsCtrl + wq
- quit a windowCtrl + wx
- exchange current window with next oneCtrl + w=
- make all windows equal height & widthCtrl + wH
- make current window full height at far left (leftmost vertical window)Ctrl + wL
- make current window full height at far right (rightmost vertical window)Ctrl + wJ
- make current window full width at the very bottom (bottommost horizontal window)Ctrl + wK
- make current window full width at the very top (topmost horizontal window)
Tabs Operations
:tabnew
or:tabnew {page.words.file}
- open a file in a new tabCtrl
+wT
- move the current split window into its own tab:tabm[ove] #
- move current tab to the#th
position (indexed from 0):tabc[lose]
- close the current tab and all its windows:tabo[nly]
- close all tabs except for the current one:tabdo command
- run the command on all tabs (e.g.:tabdo q
- closes all opened tabs):tabedit file2
- will open a new tab and take you to editfile2
$ vim -p file1.c file2.h
- to open multiple files in tabs
Move between Buffers
:bn[ext]
- go to the next buffer:bp[revious]
- go to the previous buffer:b[uffer]#
- go to a buffer by index #:b[uffer] file
- go to a buffer by file
Move between Splits
Ctrl + wh
- move cursor to the left window (vertical split)Ctrl + wl
- move cursor to the right window (vertical split)Ctrl + wj
- move cursor to the window below (horizontal split)Ctrl + wk
- move cursor to the window above (horizontal split)
Buffers and Splits
:sp[lit] fil
- open a file in a new buffer and split window:vs[plit] file
- open a file in a new buffer and vertically split window:vert[ical] ba[ll]
- edit all buffers as vertical windows
Move Between Tabs
:tabs
- to list all the open tabsgt
or:tabn[ext]
- move to the next tabgT
or:tabp[revious]
- move to the previous tab#gt
- move to tab number #:tabfirst
- to directly move to the first tab:tablast
- to directly move to the last tab
Pane
:clo[se]
- close current pane
References
- A lot of the content on this page I’ve found here: here