vim· 6 min read

Vim without the panic

Vim stops being scary once you see that commands are small sentences: a verb, an optional count and a motion. Here is the vocabulary.

Modes

ModeEnter withUsed for
NormalEscMoving around and running commands
Inserti · a · o · OTyping text before / after the cursor, on a new line below / above
Visualv · V · Ctrl+vSelecting 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

KeysMoves to
h j k lLeft, down, up, right
w · b · eNext word, previous word, end of word
0 · ^ · $Line start, first non-blank, line end
gg · G · 42GFirst 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+uHalf a page down / up

Editing grammar

Combine a verb with a motion or a text object: verb + [count] + motion.

CommandReads as
dwDelete to the next word
d$ or DDelete to the end of the line
dd · 3ddDelete a line / three lines
ciwChange inner word
ci" · ci(Change inside quotes / parentheses
dapDelete a paragraph
yy · p · PCopy a line, paste after / before
>> · <<Indent / unindent the line
u · Ctrl+rUndo / redo
.Repeat the last change
/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.