Modes
| Mode | Enter with | Used for |
|---|---|---|
| Normal | Esc | Moving around and running commands |
| Insert | i · a · o · O | Typing text before / after the cursor, on a new line below / above |
| Visual | v · V · Ctrl+v | Selecting characters, lines or a block |
| Command-line | : | Saving, quitting, search and replace, settings |
Stuck? Press Esc, then type :q! to quit without saving or :wq to save and quit.
Motions
| Keys | Moves to |
|---|---|
h j k l | Left, down, up, right |
w · b · e | Next word, previous word, end of word |
0 · ^ · $ | Line start, first non-blank, line end |
gg · G · 42G | First line, last line, line 42 |
f, · t, | Onto / just before the next comma on the line |
% | The matching bracket |
{ · } | Previous / next paragraph |
| Ctrl+d · Ctrl+u | Half a page down / up |
Editing grammar
Combine a verb with a motion or a text object: verb + [count] + motion.
| Command | Reads as |
|---|---|
dw | Delete to the next word |
d$ or D | Delete to the end of the line |
dd · 3dd | Delete a line / three lines |
ciw | Change inner word |
ci" · ci( | Change inside quotes / parentheses |
dap | Delete a paragraph |
yy · p · P | Copy a line, paste after / before |
>> · << | Indent / unindent the line |
u · Ctrl+r | Undo / redo |
. | Repeat the last change |
Search and replace
/pattern " search forward, n / N for next / previous
* " search the word under the cursor
:%s/old/new/g " replace everywhere in the file
:%s/old/new/gc " ...asking for confirmation each time
:s/old/new/ " first match on the current line only
:g/TODO/d " delete every line containing TODO
:noh " clear search highlighting
Search patterns are regular expressions — the regex note covers the syntax (Vim needs \v at the start to use most of it unescaped).
Buffers and windows
:e path/to/file " open a file
:ls " list open buffers
:bn :bp " next / previous buffer
:sp :vsp " split horizontally / vertically
Ctrl-w w " jump to the next window
Ctrl-w h/j/k/l " move between windows
:tabnew " open a new tab page
A tiny vimrc
" ~/.vimrc
set number relativenumber
set expandtab shiftwidth=4 tabstop=4
set ignorecase smartcase incsearch hlsearch
set clipboard=unnamedplus
syntax on
filetype plugin indent on
let mapleader = " "
nnoremap <leader>w :w<CR>
nnoremap <leader>h :noh<CR>
Learn by replacing one habit at a time. A week of using ciw and . does more than memorising a hundred commands.