Skip to content
Opsfolk
DevOps

Vim cheat sheet

The Vim keys and Ex commands you need on a server and at your desk, from getting out of the editor to macros, splits and search and replace, checked on Vim 9.1 with notes where Neovim differs.

Keys are written as Vim's own help writes them: <C-w> is Ctrl+W, <Esc> is Escape and <CR> is Enter. A line that starts with : is an Ex command: type it in Normal mode and press Enter. Most Normal mode commands take a count in front, so 3dd deletes three lines. Names such as file.txt and old are examples. Everything here works the same in Neovim unless a note says otherwise.

Modes and getting out

Enter Insert and Visual mode, get back to Normal, save and quit.

  • Vim opens in Normal mode, where keys are commands. You type text in Insert mode, select in Visual mode, and <Esc> always takes you back to Normal.

  • <Esc>

    Go back to Normal mode from Insert, Visual or a half-typed command.

    Tip: <C-[> sends the same key, and is easier to reach on many keyboards.

  • i

    Insert before the cursor.

  • a

    Append after the cursor.

  • I

    Insert at the first non-blank character of the line.

  • A

    Append at the end of the line.

  • o O

    Open a new line below the cursor, or above it, and start Insert mode there.

  • s S

    Delete the character under the cursor, or the whole line's text, and start Insert mode.

  • v V <C-v>

    Start Visual mode, selecting by character, by line or by block. The Visual mode section has what to do next.

  • :w

    Write the file, keeping it open.

  • :q

    Quit the window. Vim refuses with E37 while the buffer has unsaved changes.

  • :wq

    Write the file and quit.

  • :x

    Write the file only if it changed, then quit: the file's modification time stays as it was when nothing changed.

    Tip: ZZ in Normal mode does the same.

  • :q!

    Quit and throw away every unsaved change.

    Tip: ZQ in Normal mode does the same.

  • :qa

    Quit every window. :qa! quits even with unsaved changes, :wqa writes them all first.

Moving around

Move by character, word, line, screen and file without the arrow keys.

  • h j k l

    Move left, down, up and right by one character or line. A count moves further: 5j goes five lines down.

  • w b e

    Move to the start of the next word, back to the start of a word, or to the end of a word. A word stops at punctuation.

  • W B E

    The same moves by WORD: anything between spaces, so W crosses user@host:/path in one go.

  • 0 ^ $

    Go to the first column of the line, its first non-blank character, or its end.

  • gg G

    Go to the first line or the last line of the file.

  • 42G

    Go to line 42. :42 does the same.

  • fx tx

    Jump to the next x on the line, or to just before it. F and T search backwards.

    Tip: ; repeats the last f, t, F or T, and , repeats it the other way.

  • %

    Jump to the bracket that matches the one under the cursor: (), [] or {}.

  • { }

    Move to the previous or the next blank line: back or on by a paragraph.

  • <C-d> <C-u>

    Scroll half a screen down or up, taking the cursor with it.

  • <C-f> <C-b>

    Scroll a whole screen forward or back.

  • H M L

    Move the cursor to the top, the middle or the bottom line of the screen.

  • zz zt zb

    Scroll so the cursor's line is in the middle, at the top or at the bottom of the screen, leaving the cursor where it is.

  • gj gk

    Move down or up by a line as it is shown on the screen, when a long line wraps.

Editing

Delete, change, copy, paste, undo and repeat, with an operator and a motion.

  • An operator takes a motion: d deletes, c changes and y yanks (copies) whatever the motion covers, so dw deletes to the next word and y$ copies to the end of the line.

  • x

    Delete the character under the cursor.

  • rx

    Replace the character under the cursor with x, staying in Normal mode.

  • R

    Start Replace mode: what you type overwrites the text until <Esc>.

  • dd

    Delete the whole line. 3dd deletes three lines.

  • D

    Delete from the cursor to the end of the line, the same as d$.

  • cc

    Change the whole line: delete its text, keep its indent, and start Insert mode.

  • C

    Change from the cursor to the end of the line, the same as c$.

  • cw

    Change to the end of the word and start Insert mode.

  • yy

    Yank (copy) the whole line. 5yy copies five lines.

  • p P

    Put (paste) what was deleted or yanked after the cursor, or before it. A whole line goes below or above the current one.

  • u

    Undo the last change. 3u undoes three.

  • <C-r>

    Redo what was undone.

  • .

    Repeat the last change, at the cursor: dw then . deletes the next word as well.

  • J

    Join the line below onto this one, with a space between. gJ joins without adding the space.

  • ~

    Switch the case of the character under the cursor and move on.

  • guiw gUiw

    Make the word under the cursor lower case or upper case. gu and gU take any motion.

  • >> <<

    Indent the line or take one level of indent off, by shiftwidth.

  • <C-a> <C-x>

    Add one to the number under or after the cursor, or take one off. 10<C-a> adds ten.

  • xp

    Swap the character under the cursor with the next one, to fix a typo such as teh.

  • ddp

    Swap the line with the one below it.

Text objects

Act on a word, a sentence, a quoted string or a bracketed block wherever the cursor is in it.

  • After an operator or in Visual mode, i takes the inside of an object and a takes it with its surroundings: ci" changes inside the quotes, da" deletes the quotes too.

  • ciw

    Change the word under the cursor, wherever the cursor is in it.

  • daw

    Delete the word under the cursor and the space after it.

  • ci"

    Change the text inside the double quotes on the line. ci' and `ci `` do the same for other quotes.

    Tip: The cursor need not be inside the quotes: Vim takes the next quoted string on the line.

  • ci(

    Change everything inside the parentheses around the cursor. ci) and cib are the same.

  • da[

    Delete the square brackets around the cursor and what is in them.

  • di{

    Delete what is inside the braces around the cursor, such as a function's body. diB is the same.

  • cit

    Change the text inside the HTML or XML tag around the cursor. dat deletes the tag as well.

  • yip

    Yank the paragraph around the cursor: the lines up to a blank line on each side.

  • das

    Delete the sentence around the cursor and the space after it.

  • vi{

    Select the inside of a block in Visual mode. Pressing i{ again widens it to the block around that one.

Visual mode

Select characters, lines or a block, then act on the selection.

  • v

    Select by character. Move to widen the selection, then press an operator such as d, y, c or >.

  • V

    Select whole lines.

  • <C-v>

    Select a rectangular block of columns.

    Tip: In a terminal where <C-v> pastes, as Windows Terminal does by default, <C-q> starts block selection instead.

  • o

    Move the cursor to the other end of the selection, to widen it from that side.

  • gv

    Select again what was selected last.

  • <C-v>jjjI# <Esc>

    Put # at the start of four lines at once: select a column, insert with I, and the text appears on every line after <Esc>.

    Tip: A instead of I appends after the block; $A appends at the end of each line, however long.

  • Vjj>

    Indent three lines. Then . indents them again, or gv> reselects and indents.

  • :'<,'>

    What Vim types when you press : with a selection: the command that follows runs on the selected lines only, such as :'<,'>s/a/b/g.

  • Vy

    Copy the current line in Visual line mode, the same text as yy.

  • vU vu

    Make the selection upper case or lower case. ~ switches its case.

Search and replace

Find text, substitute across a file and run a command on every matching line.

  • /pattern

    Search forward for a pattern. ?pattern searches backward.

  • n N

    Go to the next match, or to the previous one, in the direction of the last search.

  • * #

    Search forward or backward for the whole word under the cursor.

  • :noh

    Clear the highlighting of the last search until the next one. It does not turn hlsearch off.

  • :s/old/new/

    Replace the first old on the current line with new. Add g to replace every one on the line.

  • :%s/old/new/g

    Replace every old in the file with new. % is every line.

  • :%s/old/new/gc

    Replace every old in the file, asking before each one: y to replace, n to skip, a for all the rest, q to stop.

  • :5,20s/old/new/g

    Replace on lines 5 to 20 only. .,$ is from the current line to the end.

  • :%s#/usr/local#/opt#g

    Use another delimiter than / when the text has slashes in it, such as a path.

  • :%s/\v(\w+) (\w+)/\2 \1/

    Swap two words on each line: \v makes (, ) and + special without a backslash, and \1 and \2 are the groups.

  • :%s/\s\+$//e

    Remove spaces and tabs at the end of every line. e keeps quiet when there are none.

  • :%s//new/g

    Replace the last pattern you searched for: an empty pattern is the last search, so * then this replaces the word under the cursor.

  • :g/pattern/d

    Delete every line that matches the pattern. :g runs any Ex command on each matching line.

  • :v/pattern/d

    Delete every line that does not match the pattern, keeping only the matches. :g! is the same.

  • :set ignorecase smartcase

    Search without case, unless the pattern has a capital letter in it.

  • /\<word\>

    Search for word as a whole word only, not inside password.

  • &

    Repeat the last :s on the current line. g& repeats it on every line.

Registers and macros

Named places to copy into, the system clipboard, and keystrokes recorded to replay.

  • A register is named by " and a letter before a command: "ayy yanks into a, "ap puts from it. Without one, Vim uses the unnamed register ".

  • "ayy

    Yank the line into register a.

  • "Ayy

    Append the line to register a, keeping what was there: a capital letter appends.

  • "ap

    Put the contents of register a after the cursor.

  • "+y

    Yank into the system clipboard, in Visual mode or with a motion. "+p pastes from it.

    Warning: Needs a Vim built with +clipboard: check :echo has('clipboard'). Neovim needs a clipboard tool such as wl-copy, xclip or pbcopy.

  • "0p

    Put the last thing you yanked, even after a delete has replaced the unnamed register.

  • "_d

    Delete into the black hole register, so what you yanked before stays in the unnamed register.

  • :reg

    List the registers and what is in each. :reg a shows only a.

  • <C-r>a

    In Insert mode or on the command line, insert the contents of register a. <C-r>" inserts the last yank or delete.

  • qa

    Start recording keystrokes into register a. q again stops the recording.

  • @a

    Replay the macro in register a. @@ replays the last one played.

  • 10@a

    Replay the macro ten times. It stops early at the first command that fails, such as a search with no match.

  • :%norm @a

    Run the macro once on every line of the file. :'<,'>norm @a runs it on the selected lines.

  • :%norm A;

    Run Normal mode keys on every line: here, append ; to each one.

Marks and jumps

Remember a place in a file and get back to where you were.

  • ma

    Set mark a at the cursor. A lower-case mark belongs to its file; a capital one, such as mA, works across files.

  • `a

    Jump to mark a, at its line and column.

  • 'a

    Jump to the first non-blank character of mark a's line.

  • d'a

    Delete from the current line to the line of mark a. Any operator takes a mark as its motion.

  • :marks

    List the marks, with each one's line, column and text.

  • <C-o> <C-i>

    Go back to where the cursor was before a jump, such as G, a search or %, and forward again.

  • ``

    Jump back to where the cursor was before the last jump, and again to return.

  • `.

    Jump to where the last change was made.

  • gi

    Go to where you last left Insert mode and start Insert mode there.

  • g; g,

    Go back and forward through the places where changes were made.

  • :jumps

    List the jump list that <C-o> and <C-i> move through.

Buffers, windows and tabs

Several files open at once, side by side or one at a time.

  • A buffer is a file loaded in memory, a window is a view of a buffer, and a tab page holds a layout of windows. Several windows can show one buffer.

  • :e file.txt

    Open a file in the current window. :e alone reloads the file from disk; :e! throws away the changes first.

  • :ls

    List the open buffers with their numbers. % marks the current one and # the one before it.

  • :b 3

    Switch to buffer 3. :b part switches to the one whose name matches, and Tab completes it.

  • :bn :bp

    Switch to the next or the previous buffer.

  • <C-^>

    Switch to the buffer you were in before, and back.

  • :bd

    Unload the current buffer and close its window.

  • :sp file.txt

    Split the window in two, one above the other, with a file in the new one. <C-w>s splits on the same file.

  • :vs file.txt

    Split the window side by side. <C-w>v splits on the same file.

  • <C-w>h <C-w>j <C-w>k <C-w>l

    Move to the window to the left, below, above or to the right. <C-w>w goes to the next one.

  • <C-w>=

    Make every window the same size.

  • <C-w>_ <C-w>|

    Make the window as tall, or as wide, as it can be.

  • <C-w>o

    Close every window but this one. :only is the same.

  • <C-w>q

    Close the current window. <C-w>c closes it unless it is the last.

  • :tabnew file.txt

    Open a file in a new tab page.

  • gt gT

    Go to the next or the previous tab page. 3gt goes to the third.

  • :tabclose

    Close the current tab page and its windows.

  • :bufdo %s/old/new/ge | update

    Replace in every open buffer and write each one that changed. :windo and :tabdo do the same for windows and tabs.

Files and the shell

Open, read and write files, and send text through shell commands.

  • vim +42 file.txt

    Open a file at line 42. vim +/pattern file.txt opens it at the first match.

  • vim -R file.txt

    Open a file read-only. view file.txt does the same.

  • vim -d old.conf new.conf

    Show two files side by side with their differences marked. vimdiff is the same.

    Tip: ]c and [c jump to the next and previous change; do takes the other side's version, dp gives yours to it.

  • command | vim -

    Read a command's output into a new buffer, to read or edit it.

  • vimtutor

    Start the built-in lesson, about half an hour, on a copy of its file. In Neovim it is :Tutor.

  • :w other.txt

    Write the buffer to another file, staying on the one you have open. :sav other.txt writes and switches to it.

  • :10,20w part.txt

    Write lines 10 to 20 to a file of their own.

  • :r file.txt

    Read a file in below the cursor's line.

  • :r !date

    Read a shell command's output in below the cursor's line.

  • :!ls

    Run a shell command and show its output, then press Enter to return. % stands for the file's name, as in :!python3 %.

  • :%!jq .

    Send the whole buffer through a command and replace it with the output: here, format JSON with jq. u undoes it.

  • !ipsort

    Send the paragraph through sort and replace it with the result. ! takes a motion and fills in :.,.+N! for you.

  • :w !sudo tee % > /dev/null

    Write a file you opened without the rights to, through sudo, then answer L to load it again.

    Warning: Fails in Neovim, which gives sudo no terminal to ask for a password. Save elsewhere with :w /tmp/file and move it with sudo mv.

  • gf

    Open the file whose path is under the cursor. <C-w>f opens it in a new window.

  • :Ex

    Browse the current file's directory with netrw: Enter opens, - goes up, % makes a file, d a directory.

  • :e %:h/other.txt

    Open a file in the same directory as the current one: %:h is the current file's directory.

  • :set ff=unix

    Change the line endings to Unix ones on the next write, to remove the ^M of a file made on Windows.

Settings and vimrc

Options for the session, and the file that sets them every time.

  • :set number relativenumber

    Show line numbers: the current line's own number, and the distance to every other line, for counts such as 5j.

  • :set nonumber

    Turn a setting off with no in front. :set number! switches it.

  • :set expandtab tabstop=4 shiftwidth=4

    Indent with four spaces: Tab types spaces, a tab shows as four columns and >> moves by four.

  • :retab

    Turn the file's tabs into spaces, or the other way, to match expandtab and tabstop.

  • :set list

    Show tabs and the end of each line, to find stray whitespace. :set listchars=tab:>-,trail:. picks the marks.

  • :set wrap!

    Switch the wrapping of long lines on or off.

  • :set paste

    Turn off auto-indent and mappings while you paste into a terminal Vim, then :set nopaste.

    Tip: Rarely needed now: Vim 8 and later understand bracketed paste from most terminals. Neovim handles pastes itself and marks the option as deprecated.

  • :set filetype?

    Show a setting's value: a ? after its name. :set filetype=yaml changes how the buffer is highlighted and indented.

  • :syntax on

    Turn syntax highlighting on, which most distributions' default vimrc already does.

  • :help :s

    Open Vim's help on a command. :help i_<C-r> is a key in Insert mode, :help 'tabstop' an option.

  • :source ~/.vimrc

    Read the vimrc again without restarting Vim. :so % reads the file you are editing.

  • ~/.vimrc

    set nocompatible
    filetype plugin indent on
    syntax on
    set number relativenumber
    set expandtab tabstop=4 shiftwidth=4 softtabstop=4
    set ignorecase smartcase incsearch hlsearch
    set backspace=indent,eol,start
    set cursorline scrolloff=5
    set undofile undodir=~/.vim/undo//
    let mapleader = " "
    nnoremap <leader>h :nohlsearch<CR>
    autocmd FileType yaml setlocal tabstop=2 shiftwidth=2 softtabstop=2

    A small vimrc for working on servers: numbers, spaces for indent, case-aware search and a visible cursor line.

    Tip: Neovim reads ~/.config/nvim/init.vim or init.lua instead, and already sets nocompatible, hlsearch, incsearch and backspace like this.

    Warning: Make ~/.vim/undo first, with mkdir -p ~/.vim/undo: Vim does not create it.

Pitfalls

What goes wrong, what it looks like, and how to get out of it.

  • The terminal stops answering: keys do nothing and the screen does not change.

    Fix: <C-s> froze the terminal's output, an old flow control the shell kept. Press <C-q> to let it go on. stty -ixon in your shell's rc file turns it off for good.

  • The screen shows Entering Ex mode. Type "visual" to go to Normal mode. and every key waits for Enter.

    Fix: Q or gQ started Ex mode. Type visual and press Enter to get back to Normal mode.

  • Opening a file shows E325: ATTENTION and Found a swap file by the name ".file.txt.swp".

    Fix: Another Vim has the file open, or one ended without closing it. If none is running, press r to recover, write, then delete the .swp file; or e to edit anyway, q to give up.

  • Pasted text ends up indented further on each line, like a staircase, or comment marks repeat down the block.

    Fix: Auto-indent acted on every pasted line. Paste with "+p from the clipboard instead of the terminal, or :set paste before and :set nopaste after.

  • :w answers E45: 'readonly' option is set (add ! to override) or E212: Can't open file for writing.

    Fix: E45 is a file opened read-only: :w! writes it if you own it. E212 is a file you have no right to write: :w !sudo tee % > /dev/null in Vim, or write it to /tmp and move it with sudo.

  • recording @q shows at the bottom of the screen, and keys seem to be stored somewhere.

    Fix: q followed by a letter started recording a macro. Press q again to stop. Nothing changed in the file because of it.

  • :q answers E37: No write since last change (add ! to override).

    Fix: The buffer has changes you have not saved. :wq keeps them, :q! throws them away. :qa answers E162 for the same reason in another buffer.

  • A search or :s for a path or an address matches the wrong text, or none: /1.2.3.4 matches 1a2b3c4.

    Fix: ., *, [ and ~ mean something in a pattern, and / ends it. Escape them with a backslash, /1\.2\.3\.4, or start the pattern with \V for very nomagic: /\V1.2.3.4.

  • A Makefile answers missing separator, or YAML refuses a file that looks indented right.

    Fix: expandtab turned the Makefile's tab into spaces, or a tab got into YAML. :set list shows them. A Makefile needs :setlocal noexpandtab and :retab!; YAML needs spaces only.

  • Backspace does not delete past the point where Insert mode started, or across the start of a line.

    Fix: backspace is empty, as in Vim's old default. Add set backspace=indent,eol,start to your vimrc; Neovim and Vim's defaults.vim already set it.

  • A file made on Windows shows ^M at the end of each line.

    Fix: Vim read it as Unix with carriage returns left in. :e ++ff=dos reads it again as DOS, then :set ff=unix and :w write it with Unix endings.

Every cheat sheet