Vim cheatsheet

Vim commands by mode: move, edit, copy, paste, search, replace, windows, buffers and how to exit.

loading Vim cheatsheet
About this tool

What is a Vim cheatsheet?

Vim is a modal editor. In normal mode, the keys you press are commands: dd deletes a line, w jumps to the next word, u undoes. To type text you enter insert mode with i, and you come back with Esc. This is the reason Vim is fast once you know it, and the reason beginners get stuck on the first day, unable to type or to leave the editor.

This cheatsheet groups the commands by task in eight sections: open, save and exit, modes, move, edit, copy, cut and paste, search and replace, windows, tabs and buffers, and settings with useful extras such as macros, marks and a sample ~/.vimrc. It starts with the question everyone asks: to leave, press Esc, then :q and Enter, or :q! to leave and throw the changes away.

Placeholders appear between angle brackets, such as <file>, <number> and <text>; replace them, brackets included. A command that starts with : is typed in command-line mode and ends with Enter. Keys without a colon, such as dd or ci", are typed in normal mode. Vim and Neovim share almost everything listed here. The page runs in your browser, with no account and nothing sent. If a simpler editor is enough, see the nano cheatsheet.

How to use the Vim cheatsheet

  1. Open the page and scroll, or use the section chips at the top to jump to a group such as Search and replace.
  2. Type in the search box to filter. It looks at the description, the section title and the commands, and every word has to match, so save quit finds the items about saving and leaving. Capital letters and accents are ignored.
  3. Press / anywhere on the page to focus the search box.
  4. Click the copy button next to an item to copy its command. When an item has several commands, they are copied together, one per line.
  5. Use the Copy section button in a section title to take every command of the group.
  6. Click Print for a paper or PDF version of the sheet, without the buttons.
  7. In Vim, check you are in normal mode (press Esc twice if in doubt) and type the command.

For the pattern syntax used in / and :s, the regular expressions cheatsheet helps, even though Vim has small differences in escaping.

Examples

Edit a file and leave

vim notes.txt

Press i, type your text, press Esc. Then type the command and press Enter:

:wq

:wq saves and quits; ZZ in normal mode does the same. To leave without saving, :q!. If you only press :q with unsaved changes, Vim refuses:

E37: No write since last change (add ! to override)

Replace text in the whole file

Item "Replace in the whole file, asking for each one":

:%s/http:/https:/gc

% means every line, g every match in each line, and c asks for confirmation. At each match press y to replace, n to skip, a for all the rest, q to stop. Without c, Vim replaces everything and reports it, for example 4 substitutions on 3 lines. u undoes it.

Change what is inside quotes

With the cursor anywhere on the line name = "old value", type in normal mode:

ci"

Vim deletes old value, keeps the quotes and enters insert mode between them. Type the new text and press Esc. ci( does the same inside parentheses.

Save a root-owned file you opened without sudo

:w !sudo tee % > /dev/null

The % is the current file name. Vim asks you to reload the file afterwards; answer L.

Common errors and how to fix them

  • You cannot type, or the text appears as commands: you are in normal mode. Press i to insert. If you are in the opposite situation, with the screen showing -- INSERT -- and you want commands, press Esc.
  • You cannot leave: press Esc, then :q! and Enter to leave discarding changes, or :wq to save and leave. If the cursor is on a : line at the bottom, finish or clear it with Esc first.
  • E45: 'readonly' option is set or E212: Can't open file for writing: the file belongs to another user or is read-only. Save with :w !sudo tee % > /dev/null, or quit and reopen with sudo vim <file>.
  • A swap file warning (E325: ATTENTION): another Vim has the file open, or a previous session crashed. Choose R to recover or Q to quit if it is open elsewhere. Delete the .swp file only when you are sure no other session uses it.
  • Pasting from the terminal breaks the indentation: Vim treats each pasted line as typed and indents it again. Use :set paste before pasting and :set nopaste after, or paste with "+p if your Vim has clipboard support.
  • "+y does nothing: your Vim was built without clipboard support. vim --version shows -clipboard; install the gvim or vim-gtk build.
  • :%s changed more than you wanted: without a range it affects the whole file. Press u, and use :10,20s/old/new/g to limit it, or add c to confirm each one.
  • No command matches your search: remove a word. All words must match at once.

Frequently asked questions

How do I exit Vim?

Press Esc to be sure you are in normal mode, then type :q and Enter. If Vim complains about unsaved changes, use :wq to save and leave, or :q! to leave without saving.

How do I copy and paste in Vim?

yy copies a line, yw a word, and in visual mode (v) y copies the selection. p pastes after the cursor and P before it. To use the system clipboard, "+y and "+p.

How do I search and replace in Vim?

/text searches forward, n goes to the next match. To replace in the whole file, :%s/old/new/g. Add c to confirm each change.

What is the difference between Vim and Vi?

Vi is the original editor. Vim means "Vi IMproved" and adds multiple undo, syntax highlighting, windows, tabs and plugins. On most systems, typing vi opens Vim or a compatible build.

How do I undo and redo?

u undoes the last change and Ctrl+R redoes it. . repeats the last change, which is different from redo.

Report a problem