cheat-sheets

Vim

Vim is a modal terminal text editor, installed almost everywhere. Use it for quick edits over SSH or inside tmux, and see nano for a simpler editor.

Documentation: Vim help (vimhelp.org) Project: vim.org

Commands below are checked against the Vim 9.2 help. Neovim was not checked, so defaults and config paths may differ there. The Ctrl-x keys mean hold Ctrl.

Start and Exit #

COMMANDDESCRIPTION
vim FILEEdit a file
vim +LINE FILEOpen with the cursor on line LINE
vim -R FILEOpen read-only
vim -O FILE1 FILE2 / vim -o FILE1 FILE2Open files in vertical / horizontal split windows
vim -d FILE1 FILE2 / vimdiff FILE1 FILE2Diff mode
vim -rList swap files; vim -r FILE recovers that file
:wWrite (save)
:qQuit (fails if there are unsaved changes)
:wq / :xWrite and quit; :x writes only if changed
:q!Quit without writing, discarding changes
ZZ / ZQWrite if changed and close / close without writing
:wa / :qaWrite all buffers / quit Vim

Modes #

KEYDESCRIPTION
EscBack to Normal mode
i / aInsert before / after the cursor
I / AInsert at the first non-blank / at the end of the line
o / OOpen a new line below / above
RReplace mode (overtype)
v / V / Ctrl-vVisual mode: characters, lines, block

Movement #

KEYDESCRIPTION
h j k lLeft, down, up, right
w / b / eWord forward / word back / end of word
0 / ^ / $Start of line / first non-blank / end of line
gg / GFirst / last line (NG or :N goes to line N)
%Jump to the matching bracket
{ / }Paragraph back / forward
Ctrl-d / Ctrl-uScroll half a screen down / up
Ctrl-f / Ctrl-bScroll a screen forward / back
zzCentre the cursor line in the window
ma / 'aSet mark a / jump to mark a

Edit #

KEYDESCRIPTION
xDelete the character under the cursor
dd / DDelete the line / delete to the end of the line
d + motionDelete what the motion covers, e.g. dw, d$
cwChange a word (does not include the trailing space)
c + motionChange what the motion covers, e.g. ciw (inner word), ci" (inside quotes)
yyYank (copy) the line
p / PPut after / before the cursor
"ayy / "apYank into register a / put from register a
u / Ctrl-rUndo / redo
.Repeat the last change
>> / <<Shift the line right / left by shiftwidth
JJoin lines
~Switch case and move right
rXReplace the character with X

Search and Replace #

COMMANDDESCRIPTION
/PATTERN / ?PATTERNSearch forward / backward
n / NRepeat the search in the same / opposite direction
* / #Search forward / backward for the word under the cursor
:nohStop highlighting matches (comes back on the next search)
:s/OLD/NEW/Replace the first match on the current line
:s/OLD/NEW/gReplace all matches on the line
:%s/OLD/NEW/gReplace all matches in the file (% means every line)
:%s/OLD/NEW/gcSame, asking for each one: y yes, n no, a all remaining, q quit

Buffers, Windows and Tabs #

COMMANDDESCRIPTION
:lsList buffers
:bn / :bpNext / previous buffer
:bdRemove the buffer from the list
:sp / :vsSplit horizontally / vertically (optionally :sp FILE)
Ctrl-w h / j / k / lMove to the window left / down / up / right
:tabnewNew tab page
gt / gTNext / previous tab page (wraps around)

Shell and Files #

COMMANDDESCRIPTION
:!COMMANDRun a shell command
:r !COMMANDInsert the command’s output below the cursor
:r FILEInsert a file below the cursor
:w !COMMANDSend the lines to a command (note the space before !); does not write the file
:recoverRecover from the swap file; the current buffer contents are lost
:recover!Discard changes and recover
:help TOPIC / :h TOPICOpen help on a topic

Settings #

Put settings in ~/.vimrc (Unix also reads ~/.vim/vimrc and $XDG_CONFIG_HOME/vim/vimrc). Use :set OPTION to try one in the current session.

OPTIONDESCRIPTION
set number / set relativenumberAbsolute / relative line numbers (off by default)
set ignorecaseIgnore case when searching (off by default)
set smartcaseWith ignorecase, match case if the pattern has capitals (off by default)
set hlsearchHighlight all matches (off by default)
set incsearchShow matches as you type (off by default; defaults.vim turns it on when available)
set expandtabInsert spaces instead of tabs (off by default)
set tabstop=4Width of a tab character (default 8)
set shiftwidth=4Indent width for >>, << and autoindent
set autoindentCopy the indent of the previous line (off by default)
vim
set number
set ignorecase smartcase
set hlsearch incsearch
set expandtab tabstop=4 shiftwidth=4
set autoindent

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.