This document is structured to teach Vim's concepts, from basic survival to its powerful command grammar.
Everything you need to survive your first 5 minutes in Vim.
i
: insert text before the cursora
: append text after the cursorI
: Insert at the beginning of the lineA
: Append at the end of the lineo
: open a new line below and enter insert modeO
: Open a new line above and enter insert modeEsc
: Exit insert mode and return to Command Mode
:w
: write (save) the file:q
: quit the current window/buffer:wq
: write (save) and quit:q!
: quit without saving (discard changes)
u
: undo the last changeCtrl-r
: redo the last undone change
This is where Vim's power lies. Commands are like sentences.
These are the motions you can use alone, or combine with verbs.
h
,j
,k
,l
: left, down, up, rightw
/b
: move to start of next/previous word (by alphanumeric chars)W
/B
: move by WORD (by whitespace)e
: move to the end of the current word^
/$
: move to the first non-whitespace character / end of the linef{char}
/F{char}
: find character on the current line (forward/backward)t{char}
/T{char}
: move till character on the current line (forward/backward);
/,
: repeat the lastf
ort
command (forward/backward)gg
/G
: go to the first/last line of the fileH
/M
/L
: go to High/ Middle/ Low part of the screenctl-u
/ctl-d
: move half a screen up/ down%
: jump to matching bracket()
,{}
,[]
Combine a verb (action) with a noun (motion or text object).
Example: d
(verb) + w
(noun) = dw
(delete word).
You can add a number to repeat: d2w
(delete 2 words).
Verb | Action |
---|---|
d |
delete (acts like a "cut") |
c |
change (deletes and enters Insert Mode) |
y |
yank (copies) |
> /< |
indent / dedent |
gu /gU |
change to lowercase / uppercase |
You can combine any verb above with any noun below.
1. Nouns as Motions (from the navigation section above)
Motion | Meaning | Example: d + Motion |
Example: c + Motion |
---|---|---|---|
w |
to the next word | dw (delete word) |
cw (change word) |
$ |
to the end of the line | d$ (delete to EOL) |
c$ (change to EOL) |
} |
to the next paragraph | d} (delete to para) |
c} (change to para) |
2. Nouns as Text Objects
Act on the "object" your cursor is inside. Use i
for "inner" and a
for "around".
Text Object | Meaning | Example: d + Object |
Example: c + Object |
---|---|---|---|
iw /aw |
inner/ around word | diw / daw |
ciw / caw |
i" /a" |
inner/ around " quotes |
di" / da" |
ci" / ca" |
i( /a( |
inner/ around () parens |
di( / da( |
ci( / ca( |
i{ /a{ |
inner/ around {} braces |
di{ / da{ |
ci{ / ca{ |
it /at |
inner/ around tag | dit / dat |
cit / cat |
Visual Mode is used to make selections of text. After selecting, you can use a verb on the selection, like d
(delete) or y
(yank).
v
: enter visual mode (character-wise).V
: enter Visual mode (line-wise).Ctrl-v
: enter Visual block mode (for column-wise selection).Esc
: exit Visual mode.
One of Vim's most powerful commands. It repeats the last single action that changed the text.
.
: Repeat the last change (e.g., adw
command, aciw
change, or anx
command).
Commands that don't fit the main grammar.
p
/P
: paste after/before the cursorx
: delete character under the cursorr{char}
: replace a single characterdd
/yy
: delete/yank the current line (a common shortcut)
/foo
: search forwards forfoo
?bar
: search backwards forbar
n
/N
: repeat search in the same/opposite direction*
/#
: search for the word currently under the cursor (forward/backward):s/foo/bar/g
: substitutefoo
withbar
globally on the current line
Record any sequence of commands and replay it to automate repetitive tasks.
q{a-z}
: Start recording a macro into a register (e.g.,qa
). Pressq
again while recording to stop.@{a-z}
: Execute the macro from the specified register (e.g.,@a
).@@
: Repeat the last executed macro.
Managing files, tabs, and split windows. The key relationship is: Tabs contain Windows, and Windows display Buffers.
Think of a buffer as a file loaded into Vim's memory. It's the text itself. You can have many files open in the background as buffers, even if you can't see them. Note: For practical purposes, this is a 1-to-1 mapping. A file on disk corresponds to exactly one buffer. The only exception is a "scratch" buffer that you can create in memory which doesn't have a file yet.
:e {file}
: edit a file in a new buffer:bn
: go to next buffer:bp
: go to previous buffer:ls
: list open buffers:bd
: buffer delete (close buffer)
A window is a viewport on your screen that displays a buffer. You can split your screen to create multiple windows, allowing you to view several different files—or even different parts of the same file—at once.
:sp {file}
: split window horizontally:vsp {file}
: vertical split windowctl-w
+h,j,k,l
: move to window left, down, up, rightctl-w
+w
: cycle through windows
A tab is a collection of one or more windows. Use tabs to organize your screen into different workspaces (e.g., a "frontend" tab with HTML/CSS windows, and a "backend" tab with Python windows).
:tabnew
: open a new tabgt
or:tabn
: go to tab nextgT
or:tabp
: go to tab previous:tabclose
: close current tab
Every delete, yank, and paste goes through a "register". Think of them as named clipboards.
"
: prefix to specify a register"
+a
+y
→ yank into registera
"
+a
+p
→ paste from registera
"+y
: yank to system clipboard"+p
: paste from system clipboard
Marks let you set "bookmarks" in text to quickly return.
ma
: set marka
at cursor'a
: jump to beginning of line with marka
`a
: jump to exact cursor position at marka
''
: jump back to previous position
:%s/foo/bar/g
: replace allfoo
withbar
in the whole file:%s/foo/bar/gc
: replace with confirmation:10,20s/foo/bar/g
: replace in lines 10–20 only
Use folds to collapse large sections of code or text.
zf{motion}
: fold a region (e.g.,zf}
folds a paragraph)zo
: open (unfold)zc
: close (fold)za
: toggle fold
Your .vimrc
file makes Vim "yours". A few useful options:
set number " show line numbers
syntax on " enable syntax highlighting
set expandtab " use spaces instead of tabs
set shiftwidth=4 " indent by 4 spaces
set ignorecase " case-insensitive search
set smartcase " ...unless uppercase is used
Vim has a huge plugin ecosystem. Use a plugin manager (e.g., vim-plug):
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-surround'
Plug 'preservim/nerdtree'
call plug#end()