Vim cheatsheet
Searchable reference of Vim motions, text objects, editing, search and replace, registers, macros, windows and buffers.
248 of 248 entries
| Section | Keys | Description |
|---|---|---|
| Modes | i | Insert before the cursor. |
| Modes | a | Insert after the cursor (append). |
| Modes | I | Insert at the first non-blank character of the line. |
| Modes | A | Append at the end of the line. |
| Modes | o | Open a new line below and insert. |
| Modes | O | Open a new line above and insert. |
| Modes | gi | Insert where you last left insert mode. |
| Modes | Esc | Back to normal mode (Ctrl-[ does the same). |
| Modes | v | Visual mode, character-wise. |
| Modes | V | Visual mode, line-wise. |
| Modes | Ctrl-v | Visual block mode (columns). |
| Modes | R | Replace mode: typed characters overwrite existing ones. |
| Modes | : | Command-line mode for ex commands. |
| Motions | h j k l | Left, down, up, right. |
| Motions | w | Start of the next word. |
| Motions | W | Start of the next WORD (whitespace-separated). |
| Motions | b | Back to the start of the word. |
| Motions | B | Back to the start of the WORD. |
| Motions | e | End of the word. |
| Motions | ge | End of the previous word. |
| Motions | 0 | Start of the line. |
| Motions | ^ | First non-blank character of the line. |
| Motions | $ | End of the line. |
| Motions | g_ | Last non-blank character of the line. |
| Motions | gg | First line of the file. |
| Motions | G | Last line of the file. |
| Motions | {n}G | Go 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 {}. |
| Motions | f{char} | Forward to the next {char} on the line. |
| Motions | F{char} | Backward to the previous {char} on the line. |
| Motions | t{char} | Forward to just before {char}. |
| Motions | T{char} | Backward to just after {char}. |
| Motions | ; , | Repeat the last f, F, t or T forward or backward. |
| Motions | H M L | Top, middle or bottom of the screen. |
| Motions | Ctrl-d Ctrl-u | Scroll half a page down or up. |
| Motions | Ctrl-f Ctrl-b | Scroll a full page forward or back. |
| Motions | Ctrl-e Ctrl-y | Scroll the view one line without moving the cursor. |
| Motions | zz | Centre the cursor line on screen (zt top, zb bottom). |
| Motions | gj gk | Down or up by screen line when lines wrap. |
| Text objects | iw | Inner word, e.g. ciw changes the word under the cursor. |
| Text objects | aw | A word plus the space after it, e.g. daw. |
| Text objects | iW aW | Inner WORD or a WORD (whitespace-separated). |
| Text objects | is as | Inner sentence or a sentence. |
| Text objects | ip | Inner paragraph, e.g. yip copies the paragraph. |
| Text objects | ap | A paragraph plus the blank line after it. |
| Text objects | i" a" | Inside or around double quotes, e.g. ci" replaces a string. |
| Text objects | i' a' | Inside or around single quotes. |
| Text objects | i` a` | Inside or around backticks. |
| Text objects | i( a( | Inside or around parentheses (also ib and ab). |
| Text objects | i{ a{ | Inside or around braces (also iB and aB). |
| Text objects | i[ a[ | Inside or around square brackets. |
| Text objects | i< a< | Inside or around angle brackets. |
| Text objects | it at | Inside or around an XML/HTML tag, e.g. dit empties a tag. |
| Operators | d{motion} | Delete, e.g. dw, d$, dap. The text goes to the unnamed register. |
| Operators | c{motion} | Change: delete then enter insert mode, e.g. cw, ci(. |
| Operators | y{motion} | Yank (copy), e.g. yw, yi". |
| Operators | dd | Delete the line (3dd deletes three). |
| Operators | cc | Change the whole line. |
| Operators | yy | Yank 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. |
| Operators | gu{motion} | Lowercase, e.g. guiw. |
| Operators | gU{motion} | Uppercase, e.g. gUiw. |
| Operators | g~{motion} | Toggle case. |
| Operators | gq{motion} | Reflow text to textwidth, e.g. gqip. |
| Operators | !{motion}{filter} | Filter lines through a shell command, e.g. !ip sort. |
| Operators | dG | Delete to the end of the file. |
| Operators | d/{pattern} | Delete up to the next match of a pattern. |
| Operators | {n}{operator}{motion} | Counts multiply: 2d3w deletes six words. |
| Editing | x | Delete the character under the cursor. |
| Editing | X | Delete the character before the cursor. |
| Editing | r{char} | Replace one character. |
| Editing | s | Delete the character and insert (substitute). |
| Editing | S | Clear the line and insert (same as cc). |
| Editing | C | Change to the end of the line. |
| Editing | D | Delete to the end of the line. |
| Editing | J | Join the line below, with a space. |
| Editing | gJ | Join the line below, without adding a space. |
| Editing | ~ | Toggle the case of the character under the cursor. |
| Editing | p | Put (paste) after the cursor or below the line. |
| Editing | P | Put before the cursor or above the line. |
| Editing | ]p | Put and adjust the indent to the current line. |
| Editing | u | Undo. |
| Editing | Ctrl-r | Redo. |
| Editing | . | Repeat the last change. |
| Editing | Ctrl-a Ctrl-x | Increment or decrement the number under or after the cursor. |
| Editing | xp | Swap two characters. |
| Editing | ddp | Swap the line with the one below. |
| Editing | :earlier 10m | Restore the file as it was 10 minutes ago (:later to go forward). |
| Insert mode | Ctrl-w | Delete the word before the cursor. |
| Insert mode | Ctrl-u | Delete everything typed on the line before the cursor. |
| Insert mode | Ctrl-t Ctrl-d | Indent or unindent the line. |
| Insert mode | Ctrl-n Ctrl-p | Complete a word from the open buffers (next or previous match). |
| Insert mode | Ctrl-x Ctrl-f | Complete a file name. |
| Insert mode | Ctrl-x Ctrl-l | Complete a whole line. |
| Insert mode | Ctrl-x Ctrl-o | Omni completion (language-aware, if the filetype supports it). |
| Insert mode | Ctrl-r {register} | Insert the contents of a register, e.g. Ctrl-r " or Ctrl-r +. |
| Insert mode | Ctrl-o {command} | Run one normal-mode command and return to insert mode. |
| Insert mode | Ctrl-v {char} | Insert a character literally, e.g. a real Tab. |
| Insert mode | Ctrl-v u{hex} | Insert a Unicode character by code, e.g. Ctrl-v u20ac for €. |
| Insert mode | Ctrl-k {char}{char} | Insert a digraph, e.g. Ctrl-k e: for ë (:digraphs lists them). |
| Visual mode | o | Move to the other end of the selection. |
| Visual mode | gv | Reselect the previous selection. |
| Visual mode | vip | Select the paragraph (any text object works after v). |
| Visual mode | y d c | Yank, delete or change the selection. |
| Visual mode | > < | Indent or unindent the selected lines. |
| Visual mode | u U | Lowercase or uppercase the selection. |
| Visual mode | J | Join the selected lines. |
| Visual mode | p | Replace the selection with the register; the old text goes into the unnamed register. |
| Visual mode | P | Replace the selection without overwriting the unnamed register. |
| Visual mode | r{char} | Replace every selected character with {char}. |
| Visual mode | Ctrl-v … I | Block mode: insert text before the block on every line, then press Esc. |
| Visual mode | Ctrl-v … $A | Block mode: append text at the end of every selected line. |
| Visual mode | Ctrl-v … c | Block mode: change the block on every line. |
| Visual mode | g Ctrl-a | Turn 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 & replace | n N | Next or previous match. |
| Search & replace | * # | Search forward or backward for the whole word under the cursor. |
| Search & replace | g* g# | Like * and #, but also match inside longer words. |
| Search & replace | cgn | Change the next match; then press . to repeat on the following ones. |
| Search & replace | :noh | Clear search highlighting until the next search. |
| Search & replace | :s/old/new/ | Replace the first match on the current line. |
| Search & replace | :s/old/new/g | Replace every match on the current line. |
| Search & replace | :%s/old/new/g | Replace in the whole file. |
| Search & replace | :%s/old/new/gc | Replace in the whole file, confirming each one. |
| Search & replace | :%s/\<word\>/new/g | Replace whole words only. |
| Search & replace | :'<,'>s/old/new/g | Replace within the visual selection. |
| Search & replace | :%s//new/g | Replace the last search pattern. |
| Search & replace | :%s/\s\+$//e | Remove trailing whitespace (e: no error if none). |
| Search & replace | & | Repeat the last :s on the current line (g& on every line). |
| Search & replace | \v | Very magic: most punctuation is special, as in other regex flavours, e.g. /\v(foo|bar)+. |
| Search & replace | \c | Make this search case-insensitive, e.g. /\cerror. |
| Search & replace | :set ignorecase smartcase | Case-insensitive search unless the pattern has an uppercase letter. |
| Search & replace | :g/{pattern}/d | Delete every line that matches. |
| Search & replace | :v/{pattern}/d | Delete 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}/ **/*.js | Search files; results go to the quickfix list (:copen). |
| Search & replace | :cnext :cprev | Next or previous quickfix entry. |
| Search & replace | :cdo s/old/new/g | update | Run 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}p | Put from a named register, e.g. "ap. |
| Registers | "+y "+p | Yank to or put from the system clipboard (needs +clipboard). |
| Registers | "*y "*p | Primary selection on X11; the clipboard on Windows and macOS. |
| Registers | "0p | Put the last yank, even after deleting something. |
| Registers | "1p … "9p | Put 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 | :reg | Show the contents of all registers (:reg a for one). |
| Registers | :let @a = "text" | Set a register from the command line. |
| Marks & jumps | m{a-z} | Set a mark local to the file. |
| Marks & jumps | m{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 & jumps | Ctrl-o Ctrl-i | Older or newer position in the jump list. |
| Marks & jumps | g; g, | Older or newer position in the change list. |
| Marks & jumps | :marks | List marks. |
| Marks & jumps | :jumps | Show the jump list. |
| Marks & jumps | gd | Go to the local definition of the word under the cursor. |
| Marks & jumps | gf | Open the file whose name is under the cursor. |
| Marks & jumps | Ctrl-] | Jump to the tag under the cursor (Ctrl-t to go back). |
| Macros | q{a-z} | Start recording a macro into a register. |
| Macros | q | Stop 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. |
| Macros | q{A-Z} | Append more keys to an existing macro. |
| Macros | :'<,'>normal @a | Run a macro on every selected line. |
| Macros | @: | Repeat the last ex command. |
| Macros | "ap … "ayy | Edit a macro: put it as text, change it, yank it back (use 0"ay$ to skip the newline). |
| Windows | Ctrl-w s | Split horizontally (:split, or :sp {file}). |
| Windows | Ctrl-w v | Split vertically (:vsplit, or :vs {file}). |
| Windows | Ctrl-w h/j/k/l | Move to the window left, below, above or right. |
| Windows | Ctrl-w w | Cycle to the next window. |
| Windows | Ctrl-w q | Close the window (:q). |
| Windows | Ctrl-w o | Close every other window (:only). |
| Windows | Ctrl-w = | Make all windows the same size. |
| Windows | Ctrl-w _ Ctrl-w | | Maximise height or width. |
| Windows | Ctrl-w + Ctrl-w - | Taller or shorter by one line (prefix a count). |
| Windows | Ctrl-w > Ctrl-w < | Wider or narrower. |
| Windows | Ctrl-w r | Rotate windows. |
| Windows | Ctrl-w x | Exchange with the next window. |
| Windows | Ctrl-w H/J/K/L | Move the window to the far left, bottom, top or right. |
| Windows | Ctrl-w T | Move the window to a new tab. |
| Windows | :terminal | Open 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 | :ls | List buffers. |
| Buffers & tabs | :b {n} | Switch to buffer n (:b {name} with partial names and Tab completion). |
| Buffers & tabs | :bn :bp | Next or previous buffer. |
| Buffers & tabs | :bd | Delete (close) the current buffer. |
| Buffers & tabs | Ctrl-^ | 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 & tabs | gt gT | Next or previous tab. |
| Buffers & tabs | {n}gt | Go to tab n. |
| Buffers & tabs | :tabclose | Close the tab. |
| Buffers & tabs | :tabonly | Close every other tab. |
| Buffers & tabs | :tabmove {n} | Move the tab to position n (0 is first). |
| Files & ex commands | :w | Write (save) the file. |
| Files & ex commands | :w {file} | Write to another file. |
| Files & ex commands | :wq | Write and quit (:x or ZZ only write if changed). |
| Files & ex commands | :q! | Quit without saving (ZQ). |
| Files & ex commands | :wa | Write 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,10d | Delete lines 5 to 10. |
| Files & ex commands | :m +1 | Move the current line down one (:m -2 moves it up). |
| Files & ex commands | :t. | Duplicate the current line. |
| Files & ex commands | :sort u | Sort lines and remove duplicates (:sort n for numbers). |
| Files & ex commands | :set number relativenumber | Show absolute and relative line numbers. |
| Files & ex commands | :set expandtab tabstop=4 shiftwidth=4 | Indent with four spaces. |
| Files & ex commands | :retab | Convert tabs to spaces (or back) according to expandtab. |
| Files & ex commands | :set list | Show 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=unix | Use LF line endings when saving. |
| Files & ex commands | :set spell spelllang=en_gb | Spell 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 commands | q: | Open the command-line history in an editable window. |
| Folding | zf{motion} | Create a fold (manual or marker method), e.g. zfap. |
| Folding | zo zc | Open or close the fold under the cursor. |
| Folding | za | Toggle the fold under the cursor. |
| Folding | zO zC zA | Open, close or toggle recursively. |
| Folding | zR zM | Open or close every fold. |
| Folding | zd zE | Delete the fold under the cursor, or every fold. |
| Folding | zj zk | Move to the next or previous fold. |
| Folding | [z ]z | Move to the start or end of the open fold. |
| Folding | zv | Open just enough folds to show the cursor line. |
| Folding | zi | Toggle folding on and off. |
| Folding | :set foldmethod=indent | Fold by indentation (also syntax, marker, expr, manual). |
| Folding | :set foldlevel=99 | Start with every fold open. |