REFERENCE

Learning Vim

Appendix B — Vim Commands

A complete reference of every command introduced across all eight chapters of this course, grouped by topic. Use this alongside the lessons as a quick lookup or print it as a desk reference.

N Normal mode I Insert mode C Command mode V Visual mode S Shell / terminal RC .vimrc setting ANY Any mode

Opening & Quitting

Opening files
vim <file>Open or create a fileS
vim +N <file>Open file at line NS
vim +/pattern <file>Open and jump to first matchS
vim +"cmd" <file>Run command after openingS
vim -R <file>Open in read-only modeS
vim -u NONE <file>Open without loading vimrcS
vim --versionShow Vim versionS
:e <file>Open/edit a file (Tab completes)C
:e!Reload file from disk, discard changesC
:e .Open directory browser (netrw)C
Saving
:wWrite (save) the fileC
:w <file>Save to a new filename (Save As)C
:saveas <file>Save to new filename and switch to itC
Quitting
:qQuit (fails if unsaved changes)C
:q!Force quit, discard changesC
:wqWrite and quitC
:xWrite (only if changed) and quitC
ZZWrite (if changed) and quitN
ZQForce quit without savingN
:qaQuit all open buffersC
:qa!Force quit all buffers, discard all changesC

Modes

iEnter Insert mode at cursorN
EscReturn to Normal modeANY
Ctrl+CAlternative to Esc — return to NormalI
vEnter character-wise Visual modeN
VEnter line-wise Visual modeN
Ctrl+VEnter block-wise (column) Visual modeN
Ctrl+GShow filename, line count, cursor positionN

Navigation — Moving the Cursor

Basic movement
h / j / k / lLeft / down / up / rightN
{n}h/j/k/lMove n timesN
Word movement
w / WForward to start of next word / WORDN
e / EForward to end of word / WORDN
b / BBackward to start of word / WORDN
geBackward to end of previous wordN
Line movement
0Start of line (column 1)N
^First non-whitespace characterN
$End of lineN
g_Last non-whitespace characterN
f{char} / F{char}Jump to char on line (forward/back)N
t{char} / T{char}Jump before/after char on lineN
; / ,Repeat last f/t in same / opposite directionN
Sentence & paragraph
) / (Next / previous sentenceN
} / {Next / previous paragraphN
File-level jumps
ggJump to first lineN
GJump to last lineN
{n}G / :{n}Jump to line nN
{n}%Jump to n% through the fileN
Scrolling
Ctrl+F / Ctrl+BFull page forward / backwardN
Ctrl+D / Ctrl+UHalf page down / upN
Ctrl+E / Ctrl+YScroll screen down / up one lineN
zz / zt / zbCentre / top / bottom current line on screenN

Search

/{pattern}Search forward for patternN
?{pattern}Search backward for patternN
n / NNext / previous search matchN
* / #Search exact word under cursor (fwd/back)N
g* / g#Search partial word under cursor (fwd/back)N
:nohClear search highlightsC
:set hlsearchHighlight all search matchesC
:set incsearchIncremental search as you typeC
:set ignorecaseCase-insensitive searchC
:set smartcaseCase-sensitive when pattern has uppercaseC

Editing Text

Deleting
d{motion}Delete text covered by motionN
dd / {n}ddDelete current line / n linesN
DDelete from cursor to end of lineN
x / XDelete character under / before cursorN
xpSwap current character with nextN
Changing
c{motion}Change text covered by motionN
cc / CChange line / from cursor to end of lineN
s / SSubstitute character / line + Insert modeN
ciw / cawChange inner / around wordN
ci" / ca"Change inside / around double quotesN
ci( / ca(Change inside / around parenthesesN
Undo & redo
uUndo last changeN
UUndo all changes to current lineN
Ctrl+RRedoN
Yank (copy) & put (paste)
y{motion}Yank text covered by motionN
yy / {n}yyYank current line / n linesN
p / PPut after / before cursorN
gp / gPPut and move cursor to end of pasteN
Insert mode editing
Ctrl+WDelete word before cursorI
Ctrl+UDelete from cursor to start of lineI

Registers

"x{op}Use named register x (e.g. "ayy, "ap)N
"X{op}Append to named register x (uppercase)N
"0pPaste from yank register (safe after deletes)N
"+y / "+pYank to / paste from system clipboardN
"_d{motion}Delete to black hole register (truly removes)N
:registersShow contents of all registersC

Substitution & Spell Checking

Substitution
:s/old/newReplace first occurrence on current lineC
:s/old/new/gReplace all on current lineC
:s/old/new/gcReplace all on line with confirmationC
:%s/old/new/gReplace all in entire fileC
:5,15s/old/new/gReplace in line rangeC
:'<,'>s/old/new/gReplace in Visual selectionC
Spell checking
:set spellEnable spell checkingC
:set spelllang=en_gbSet spell check languageC
:set nospellDisable spell checkingC
]s / [sJump to next / previous misspelled wordN
z=Show spelling suggestionsN
zg / zwAdd to personal dictionary / bad-word listN

Marks & the Jump List

Setting & jumping to marks
m{a-z}Set a local mark (current buffer)N
m{A-Z}Set a global mark (across files, persists)N
'{mark}Jump to start of line containing markN
`{mark}Jump to exact position (line + column) of markN
'' / ``Toggle back to line / exact position before last jumpN
Automatic marks
`. / '.Exact position / line of last changeN
`[ and `]Start and end of last changed or yanked textN
`< and `>Start and end of last Visual selectionN
`^Position where Insert mode was last exitedN
Managing marks
:marksList all current marksC
:delmarks {letter}Delete a named markC
:delmarks!Delete all lowercase marks in bufferC
Marks as operator targets
y`{mark}Yank from cursor to exact position of markN
d'{mark}Delete from cursor to line of markN
c`{mark}Change from cursor to exact position of markN
Jump list
Ctrl+OGo to older (previous) position in jump listN
Ctrl+IGo to newer (next) position in jump listN
:jumpsDisplay the full jump historyC

Buffers, Windows & Tabs

Buffers
:lsList all open buffersC
:bn / :bpNext / previous bufferC
:bf / :blFirst / last bufferC
:b {n} / :b {name}Switch to buffer by number / nameC
Ctrl+^Toggle between current and alternate bufferN
:bd / :bd!Delete / force-delete current bufferC
:bwWipe buffer (remove from list entirely)C
Splits
:split / :spHorizontal splitC
:vsplit / :vspVertical splitC
Ctrl+W s / vHorizontal / vertical split (shortcut)N
Ctrl+W h/j/k/lMove to window left/down/up/rightN
Ctrl+W w / WCycle to next / previous windowN
Ctrl+W =Equalise all window sizesN
Ctrl+W _ / |Maximise window height / widthN
Ctrl+W cClose current windowN
Ctrl+W o / :onlyClose all other windowsN
Ctrl+W H/J/K/LMove window to edge of screenN
:resize {n}Set window height to n linesC
:vertical resize {n}Set window width to n columnsC
Tabs
:tabnewOpen new tabC
:tabcloseClose current tabC
gt / gTNext / previous tabN
{n}gtGo to tab nN
:tabsList all tabsC
Reading files into buffer
:r {file}Insert file contents below current lineC
:0r {file}Insert file at top of bufferC
:r! {cmd}Insert shell command output into bufferC

Diff Mode

vim -d file1 file2Open two files in diff modeS
vimdiff file1 file2Alias for vim -dS
:diffsplit {file}Open file in diff mode (horizontal split)C
:vert diffsplit {file}Open file in diff mode (vertical split)C
]c / [cJump to next / previous differenceN
doDiff Obtain — pull change from other fileN
dpDiff Put — push change to other fileN
:diffupdateRecalculate and refresh diff highlightsC
:set scrollbindLink scrolling of multiple windowsC
set diffopt+=verticalAlways use vertical diff layoutRC

File Navigation

gfOpen file whose name is under the cursorN
gFOpen file under cursor at the line number shownN
Ctrl+W fOpen file under cursor in a new splitN
Ctrl+W gfOpen file under cursor in a new tabN
vim {file}.zipBrowse & edit files inside a ZIP archiveS
vim {file}.tar.gzBrowse & edit files inside a TAR archiveS
:set path+=src/**Add directories to search path for gfC

External Commands & Shell Filters

:! {cmd}Run shell command; show output in overlayC
:r ! {cmd}Insert shell output below cursorC
:0r ! {cmd}Insert shell output at top of fileC
:%! {cmd}Filter entire buffer through external commandC
:{range}! {cmd}Filter a line range through external commandC
'<,'>! {cmd}Filter Visual selection through external commandV
:%!jq .Format JSON with jqC
:%!sortSort all lines alphabeticallyC
:%!xxdConvert buffer to hex dumpC
:%!xxd -rConvert hex dump back to binaryC
% (in :! commands)Expands to the current filenameC

Configuration (.vimrc)

Reloading
:source ~/.vimrcReload vimrc without restarting VimC
:so %Source the current fileC
Querying settings
:setShow all non-default settings activeC
:set allShow every setting with its current valueC
:set {option}?Query the current value of an optionC
:set no{option}Disable a boolean optionC
:set {option}!Toggle a boolean optionC
Common settings
set nocompatibleDisable Vi compatibility (always first)RC
set number / nonuShow / hide line numbersRC
set relativenumberRelative line numbersRC
set cursorlineHighlight the current lineRC
set hiddenAllow switching buffers without savingRC
set wildmenuEnhanced Tab-completion menuRC
set tabstop=4Tab display widthRC
set shiftwidth=4Indent width for >> and <<RC
set expandtabInsert spaces instead of tabsRC
set clipboard=unnamedplusUse system clipboard by defaultRC
set history=1000Increase command history sizeRC
set undolevels=1000Increase undo history depthRC
set backspace=indent,eol,startSensible Backspace in Insert modeRC
Colour schemes
:syntax on / offEnable / disable syntax highlightingC
:colorscheme {name}Switch to a colour schemeC
:colorscheme <Tab>Browse all available colour schemesC
Key mappings
nnoremap {key} {action}Map key in Normal mode (non-recursive)RC
inoremap {key} {action}Map key in Insert modeRC
vnoremap {key} {action}Map key in Visual modeRC
let mapleader = ","Set the leader keyRC
Abbreviations
:ab {trigger} {expansion}Define an abbreviation interactivelyC
iabbrev {trigger} {expansion}Define an abbreviation in vimrcRC
Ctrl+V (in Insert)Prevent abbreviation from triggeringI
Custom commands & help
:command! {Name} {action}Define a custom Command-mode commandC
:helpOpen the built-in help indexC
:help {topic}Open help for a specific topicC
:help option-summaryBrowse all available settingsC
Ctrl+] (in help)Follow a help hyperlinkN