Matthew Lang avatar

Vim

Modal editing essentials — the motions and operators that make Vim fast once they’re muscle memory.

Modes

Mode Enter with Purpose
Normal Esc / Ctrl-[ navigation, commands
Insert i a I A o O typing text
Visual v character-wise selection
Visual Line V line-wise selection
Visual Block Ctrl-v column/block selection
Command-line : ex commands
Replace R overtype instead of insert

Insert-mode entry points: i/a insert before/after cursor, I/A insert at start/end of line, o/O open a new line below/above.

Motions

h j k l        left/down/up/right
w b e          word forward / back / end of word
0  ^  $        start of line / first non-blank / end of line
gg  G          top / bottom of file
{n}G  :{n}     go to line n
f{char}        jump to next occurrence of char (t stops before it)
F{char} T{char}  same, backward
;  ,           repeat last f/t forward / backward
%              jump to matching bracket/paren
{  }           previous/next paragraph

Operators + motions

Vim’s core idea: {operator}{motion} — combine a verb with a noun.

d w    delete to next word     c $    change to end of line
d d    delete line             y y    yank (copy) line
d $    delete to end of line   y w    yank word
c c    change whole line       p / P  paste after / before cursor
x      delete char under cursor
u      undo          Ctrl-r    redo
.      repeat last change

A count prefixes either the operator or the motion: 3dd deletes 3 lines, d3w deletes 3 words.

Visual mode

v          start character-wise visual
V          start line-wise visual
Ctrl-v     start block-wise visual (great for editing a column, e.g. adding `#` to comment out N lines)

Once selected: d delete, y yank, c change, >/< indent/outdent, gu/gU lower/uppercase, : opens command-line with '<,'> pre-filled for the selection.

Search & replace

/pattern        search forward
?pattern        search backward
n  N            repeat search, same / opposite direction
*  #            search word under cursor, forward / backward
:%s/old/new/g   replace all occurrences, whole file
:%s/old/new/gc  same, with confirmation per match
:s/old/new/g    replace on current line only

Registers & marks

"ayy    yank a line into register a
"ap     paste from register a
"+y     yank into the system clipboard (if compiled with +clipboard)
ma      set mark a at cursor position
`a      jump to mark a (exact position)
'a      jump to mark a (start of line)
Ctrl-o / Ctrl-i   back / forward through the jump list

Macros

qa ... q    record a macro into register a (q to stop)
@a          play back macro a
@@          repeat the last-played macro

Prefix with a count to replay multiple times: 10@a.

Buffers, windows, tabs

:e file        edit a file (new buffer)
:ls / :buffers list open buffers
:bn / :bp      next / previous buffer
:bd            delete (close) buffer
:sp / :vsp     split window horizontally / vertically
Ctrl-w  w      cycle between windows
Ctrl-w  h/j/k/l  move focus in a direction
:tabnew        new tab
gt / gT        next / previous tab

.vimrc basics

set number relativenumber      " absolute number on cursor line, relative elsewhere
set expandtab shiftwidth=2 tabstop=2   " spaces instead of tabs
syntax on
set incsearch hlsearch         " incremental + highlighted search
let mapleader = " "
nnoremap <leader>w :w<CR>

Miscellany worth remembering

gg=G           reindent the whole file
Ctrl-a / Ctrl-x   increment / decrement number under cursor
:noh           clear search highlight without disabling it
:set paste     toggle paste mode (stops auto-indent mangling pasted text)