Skip to content
xt-tools

Vim cheatsheet

Searchable reference of Vim motions, text objects, editing, search and replace, registers, macros, windows and buffers.

248 of 248 entries

Vim keys and commands
SectionKeysDescription
ModesiInsert before the cursor.
ModesaInsert after the cursor (append).
ModesIInsert at the first non-blank character of the line.
ModesAAppend at the end of the line.
ModesoOpen a new line below and insert.
ModesOOpen a new line above and insert.
ModesgiInsert where you last left insert mode.
ModesEscBack to normal mode (Ctrl-[ does the same).
ModesvVisual mode, character-wise.
ModesVVisual mode, line-wise.
ModesCtrl-vVisual block mode (columns).
ModesRReplace mode: typed characters overwrite existing ones.
Modes:Command-line mode for ex commands.
Motionsh j k lLeft, down, up, right.
MotionswStart of the next word.
MotionsWStart of the next WORD (whitespace-separated).
MotionsbBack to the start of the word.
MotionsBBack to the start of the WORD.
MotionseEnd of the word.
MotionsgeEnd of the previous word.
Motions0Start of the line.
Motions^First non-blank character of the line.
Motions$End of the line.
Motionsg_Last non-blank character of the line.
MotionsggFirst line of the file.
MotionsGLast line of the file.
Motions{n}GGo to line n, e.g. 42G (or :42).
Motions{ }Previous or next paragraph (blank line).
Motions( )Previous or next sentence.
Motions%Jump to the matching bracket: (), [] or {}.
Motionsf{char}Forward to the next {char} on the line.
MotionsF{char}Backward to the previous {char} on the line.
Motionst{char}Forward to just before {char}.
MotionsT{char}Backward to just after {char}.
Motions; ,Repeat the last f, F, t or T forward or backward.
MotionsH M LTop, middle or bottom of the screen.
MotionsCtrl-d Ctrl-uScroll half a page down or up.
MotionsCtrl-f Ctrl-bScroll a full page forward or back.
MotionsCtrl-e Ctrl-yScroll the view one line without moving the cursor.
MotionszzCentre the cursor line on screen (zt top, zb bottom).
Motionsgj gkDown or up by screen line when lines wrap.
Text objectsiwInner word, e.g. ciw changes the word under the cursor.
Text objectsawA word plus the space after it, e.g. daw.
Text objectsiW aWInner WORD or a WORD (whitespace-separated).
Text objectsis asInner sentence or a sentence.
Text objectsipInner paragraph, e.g. yip copies the paragraph.
Text objectsapA paragraph plus the blank line after it.
Text objectsi" a"Inside or around double quotes, e.g. ci" replaces a string.
Text objectsi' a'Inside or around single quotes.
Text objectsi` a`Inside or around backticks.
Text objectsi( a(Inside or around parentheses (also ib and ab).
Text objectsi{ a{Inside or around braces (also iB and aB).
Text objectsi[ a[Inside or around square brackets.
Text objectsi< a<Inside or around angle brackets.
Text objectsit atInside or around an XML/HTML tag, e.g. dit empties a tag.
Operatorsd{motion}Delete, e.g. dw, d$, dap. The text goes to the unnamed register.
Operatorsc{motion}Change: delete then enter insert mode, e.g. cw, ci(.
Operatorsy{motion}Yank (copy), e.g. yw, yi".
OperatorsddDelete the line (3dd deletes three).
OperatorsccChange the whole line.
OperatorsyyYank the line.
Operators>{motion}Indent, e.g. >ip. >> indents the current line.
Operators<{motion}Unindent. << unindents the current line.
Operators={motion}Re-indent, e.g. =ip or gg=G for the whole file.
Operatorsgu{motion}Lowercase, e.g. guiw.
OperatorsgU{motion}Uppercase, e.g. gUiw.
Operatorsg~{motion}Toggle case.
Operatorsgq{motion}Reflow text to textwidth, e.g. gqip.
Operators!{motion}{filter}Filter lines through a shell command, e.g. !ip sort.
OperatorsdGDelete to the end of the file.
Operatorsd/{pattern}Delete up to the next match of a pattern.
Operators{n}{operator}{motion}Counts multiply: 2d3w deletes six words.
EditingxDelete the character under the cursor.
EditingXDelete the character before the cursor.
Editingr{char}Replace one character.
EditingsDelete the character and insert (substitute).
EditingSClear the line and insert (same as cc).
EditingCChange to the end of the line.
EditingDDelete to the end of the line.
EditingJJoin the line below, with a space.
EditinggJJoin the line below, without adding a space.
Editing~Toggle the case of the character under the cursor.
EditingpPut (paste) after the cursor or below the line.
EditingPPut before the cursor or above the line.
Editing]pPut and adjust the indent to the current line.
EditinguUndo.
EditingCtrl-rRedo.
Editing.Repeat the last change.
EditingCtrl-a Ctrl-xIncrement or decrement the number under or after the cursor.
EditingxpSwap two characters.
EditingddpSwap the line with the one below.
Editing:earlier 10mRestore the file as it was 10 minutes ago (:later to go forward).
Insert modeCtrl-wDelete the word before the cursor.
Insert modeCtrl-uDelete everything typed on the line before the cursor.
Insert modeCtrl-t Ctrl-dIndent or unindent the line.
Insert modeCtrl-n Ctrl-pComplete a word from the open buffers (next or previous match).
Insert modeCtrl-x Ctrl-fComplete a file name.
Insert modeCtrl-x Ctrl-lComplete a whole line.
Insert modeCtrl-x Ctrl-oOmni completion (language-aware, if the filetype supports it).
Insert modeCtrl-r {register}Insert the contents of a register, e.g. Ctrl-r " or Ctrl-r +.
Insert modeCtrl-o {command}Run one normal-mode command and return to insert mode.
Insert modeCtrl-v {char}Insert a character literally, e.g. a real Tab.
Insert modeCtrl-v u{hex}Insert a Unicode character by code, e.g. Ctrl-v u20ac for €.
Insert modeCtrl-k {char}{char}Insert a digraph, e.g. Ctrl-k e: for ë (:digraphs lists them).
Visual modeoMove to the other end of the selection.
Visual modegvReselect the previous selection.
Visual modevipSelect the paragraph (any text object works after v).
Visual modey d cYank, delete or change the selection.
Visual mode> <Indent or unindent the selected lines.
Visual modeu ULowercase or uppercase the selection.
Visual modeJJoin the selected lines.
Visual modepReplace the selection with the register; the old text goes into the unnamed register.
Visual modePReplace the selection without overwriting the unnamed register.
Visual moder{char}Replace every selected character with {char}.
Visual modeCtrl-v … IBlock mode: insert text before the block on every line, then press Esc.
Visual modeCtrl-v … $ABlock mode: append text at the end of every selected line.
Visual modeCtrl-v … cBlock mode: change the block on every line.
Visual modeg Ctrl-aTurn the selected numbers into an incrementing sequence.
Visual mode:Run an ex command on the selected lines (:'<,'>).
Search & replace/{pattern}Search forward.
Search & replace?{pattern}Search backward.
Search & replacen NNext or previous match.
Search & replace* #Search forward or backward for the whole word under the cursor.
Search & replaceg* g#Like * and #, but also match inside longer words.
Search & replacecgnChange the next match; then press . to repeat on the following ones.
Search & replace:nohClear search highlighting until the next search.
Search & replace:s/old/new/Replace the first match on the current line.
Search & replace:s/old/new/gReplace every match on the current line.
Search & replace:%s/old/new/gReplace in the whole file.
Search & replace:%s/old/new/gcReplace in the whole file, confirming each one.
Search & replace:%s/\<word\>/new/gReplace whole words only.
Search & replace:'<,'>s/old/new/gReplace within the visual selection.
Search & replace:%s//new/gReplace the last search pattern.
Search & replace:%s/\s\+$//eRemove trailing whitespace (e: no error if none).
Search & replace&Repeat the last :s on the current line (g& on every line).
Search & replace\vVery magic: most punctuation is special, as in other regex flavours, e.g. /\v(foo|bar)+.
Search & replace\cMake this search case-insensitive, e.g. /\cerror.
Search & replace:set ignorecase smartcaseCase-insensitive search unless the pattern has an uppercase letter.
Search & replace:g/{pattern}/dDelete every line that matches.
Search & replace:v/{pattern}/dDelete every line that does not match (same as :g!).
Search & replace:g/{pattern}/normal {keys}Run normal-mode keys on every matching line.
Search & replace:vimgrep /{pattern}/ **/*.jsSearch files; results go to the quickfix list (:copen).
Search & replace:cnext :cprevNext or previous quickfix entry.
Search & replace:cdo s/old/new/g | updateRun a replacement on every quickfix entry and save.
Registers"{a-z}y{motion}Yank into a named register, e.g. "ayy.
Registers"{A-Z}y{motion}Append to a named register, e.g. "Ayy.
Registers"{a-z}pPut from a named register, e.g. "ap.
Registers"+y "+pYank to or put from the system clipboard (needs +clipboard).
Registers"*y "*pPrimary selection on X11; the clipboard on Windows and macOS.
Registers"0pPut the last yank, even after deleting something.
Registers"1p … "9pPut one of the last nine multi-line deletes.
Registers"_d{motion}Delete into the black hole register, keeping your registers intact.
Registers".Read-only: the last inserted text.
Registers"%Read-only: the current file name.
Registers":Read-only: the last ex command.
Registers"/The last search pattern.
Registers"=Expression register, e.g. "=6*7 Enter then p puts 42.
Registers:regShow the contents of all registers (:reg a for one).
Registers:let @a = "text"Set a register from the command line.
Marks & jumpsm{a-z}Set a mark local to the file.
Marks & jumpsm{A-Z}Set a global mark that also remembers the file.
Marks & jumps'{mark}Jump to the line of a mark.
Marks & jumps`{mark}Jump to the exact position of a mark.
Marks & jumps`.Position of the last change.
Marks & jumps''Back to the line before the latest jump.
Marks & jumpsCtrl-o Ctrl-iOlder or newer position in the jump list.
Marks & jumpsg; g,Older or newer position in the change list.
Marks & jumps:marksList marks.
Marks & jumps:jumpsShow the jump list.
Marks & jumpsgdGo to the local definition of the word under the cursor.
Marks & jumpsgfOpen the file whose name is under the cursor.
Marks & jumpsCtrl-]Jump to the tag under the cursor (Ctrl-t to go back).
Macrosq{a-z}Start recording a macro into a register.
MacrosqStop recording.
Macros@{a-z}Play a macro, e.g. @a.
Macros@@Repeat the last played macro.
Macros{n}@{a-z}Play a macro n times, e.g. 10@a.
Macrosq{A-Z}Append more keys to an existing macro.
Macros:'<,'>normal @aRun a macro on every selected line.
Macros@:Repeat the last ex command.
Macros"ap … "ayyEdit a macro: put it as text, change it, yank it back (use 0"ay$ to skip the newline).
WindowsCtrl-w sSplit horizontally (:split, or :sp {file}).
WindowsCtrl-w vSplit vertically (:vsplit, or :vs {file}).
WindowsCtrl-w h/j/k/lMove to the window left, below, above or right.
WindowsCtrl-w wCycle to the next window.
WindowsCtrl-w qClose the window (:q).
WindowsCtrl-w oClose every other window (:only).
WindowsCtrl-w =Make all windows the same size.
WindowsCtrl-w _ Ctrl-w |Maximise height or width.
WindowsCtrl-w + Ctrl-w -Taller or shorter by one line (prefix a count).
WindowsCtrl-w > Ctrl-w <Wider or narrower.
WindowsCtrl-w rRotate windows.
WindowsCtrl-w xExchange with the next window.
WindowsCtrl-w H/J/K/LMove the window to the far left, bottom, top or right.
WindowsCtrl-w TMove the window to a new tab.
Windows:terminalOpen a terminal in a split (Ctrl-w N for normal mode inside it).
Buffers & tabs:e {file}Edit a file in the current window.
Buffers & tabs:lsList buffers.
Buffers & tabs:b {n}Switch to buffer n (:b {name} with partial names and Tab completion).
Buffers & tabs:bn :bpNext or previous buffer.
Buffers & tabs:bdDelete (close) the current buffer.
Buffers & tabsCtrl-^Switch to the alternate (previous) buffer, same as :e #.
Buffers & tabs:bufdo {command}Run a command in every buffer, e.g. :bufdo update.
Buffers & tabs:tabnew {file}Open a file in a new tab.
Buffers & tabsgt gTNext or previous tab.
Buffers & tabs{n}gtGo to tab n.
Buffers & tabs:tabcloseClose the tab.
Buffers & tabs:tabonlyClose every other tab.
Buffers & tabs:tabmove {n}Move the tab to position n (0 is first).
Files & ex commands:wWrite (save) the file.
Files & ex commands:w {file}Write to another file.
Files & ex commands:wqWrite and quit (:x or ZZ only write if changed).
Files & ex commands:q!Quit without saving (ZQ).
Files & ex commands:waWrite all changed buffers.
Files & ex commands:qa!Quit everything, discarding changes.
Files & ex commands:e!Reload the file, discarding changes.
Files & ex commands:w !sudo tee %Save a file you opened without permission (Unix).
Files & ex commands:saveas {file}Save under a new name and keep editing that file.
Files & ex commands:r {file}Insert a file below the cursor.
Files & ex commands:r !{command}Insert the output of a shell command.
Files & ex commands:!{command}Run a shell command.
Files & ex commands:%!{filter}Filter the whole file through a command, e.g. :%!jq .
Files & ex commands:5,10dDelete lines 5 to 10.
Files & ex commands:m +1Move the current line down one (:m -2 moves it up).
Files & ex commands:t.Duplicate the current line.
Files & ex commands:sort uSort lines and remove duplicates (:sort n for numbers).
Files & ex commands:set number relativenumberShow absolute and relative line numbers.
Files & ex commands:set expandtab tabstop=4 shiftwidth=4Indent with four spaces.
Files & ex commands:retabConvert tabs to spaces (or back) according to expandtab.
Files & ex commands:set listShow tabs, trailing spaces and line ends.
Files & ex commands:set wrap!Toggle line wrapping.
Files & ex commands:set filetype={type}Set the filetype, e.g. :set ft=json.
Files & ex commands:set fileformat=unixUse LF line endings when saving.
Files & ex commands:set spell spelllang=en_gbSpell checking; ]s and [s jump between errors, z= suggests.
Files & ex commands:h {topic}Open help, e.g. :h text-objects (Ctrl-] follows links).
Files & ex commandsq:Open the command-line history in an editable window.
Foldingzf{motion}Create a fold (manual or marker method), e.g. zfap.
Foldingzo zcOpen or close the fold under the cursor.
FoldingzaToggle the fold under the cursor.
FoldingzO zC zAOpen, close or toggle recursively.
FoldingzR zMOpen or close every fold.
Foldingzd zEDelete the fold under the cursor, or every fold.
Foldingzj zkMove to the next or previous fold.
Folding[z ]zMove to the start or end of the open fold.
FoldingzvOpen just enough folds to show the cursor line.
FoldingziToggle folding on and off.
Folding:set foldmethod=indentFold by indentation (also syntax, marker, expr, manual).
Folding:set foldlevel=99Start with every fold open.