What is Vim?
Vim is a free, open-source, lightweight, and highly customizable text editor for code and plain text. It's fast and efficient because it's designed for keyboard-driven editing, so you can rarely need a mouse. It's an improved version of the classic Unix editor vi. Vim runs on almost every platform, including Linux, macOS, and Windows.
I've used Vim daily at work for about five years. In my experience, it has a steep learning curve and can intimidating at first, especially for beginners or occasional users. This guides rounds up the most useful shortcusts, organized by category and illustraded with examples, to help you get productive quickly.
Cursor Movement (Words, Symbols, syntax tokens)
Words and Symbol
w / b- next / previous worde / ge- next / previous word endW / B / E- like above but BIG words (treat punctuation as part of a word)h- Move the cursor leftl- Move the cursor rightj- Moves the cursor downk- Move the cursor upgj- Move cursor down (for mult-iline text)gk- Move curson up (for multi-line text)
3wjumbs three word ahead.5jjumbs down by line five.
Line Navigation
0- start of the line^- first non-blank$- end of the lineg_- last non-blank
Find with a line (single character)
f{char} / F{char}- jumps onto the next{char}to the right on this linet{char} / T{char}- jumbs before next/prev{char}; / ,- repeat forward or backward
- Type
f,→ cursor lands on the comma afterfoo. - Type
t,→ cursor lands before that comma. - After
f,, press;to jump to the next comma. - Press
,to go back to the prev comma. - Go left, from anywhere on the line,
F=lands on previous=;T=lands just after it. - To replace
foo, bar, baz;-/\w\+EntercgnHello<Esc>thenn .
By sentences and paragraph
) / (- next / prev sentences} / {- next / prev paragraph
By Screen and scrolling
H/M/L- move cursor to screen High / Medium / Lowzz / zt / zb- center / top / bottom current linectrl+d / ctrl+u- half page down/upctrl+f / ctrl+b- page down / upctrl+e / ctrl+y- scroll down / up one line
File Navigation
gg- top of the fileG- bottom of the file{count}G / :{count}- go to line number eg,123G
Editing and Inserting
i- insert before cursora- insert after cursorI- insert at start of line (first non-blank)A- insert at end of lineo- new line below, start insertO- new line above, start insertEsc- go back to Normal mode
Cursor anywhere on the line - A; <Esc> - adds ; at the endDelete / Change
x- delete char under cursordw- delete to start of next wordd$- delete to end of linedd- delete whole line
Change
cw- change word from cursorcaw- change a word (word + space)C / c$- change to end of linecc- change whole line
Copy (yank) / paste
yy- yank line2yy- copy two linesyiw- yank (copy) word under the cursor (without space)yw / yaw- yank to end of word (word + space)y$ (inline) / Y- yank (copy) to end of linep- paste after cursor / below lineP- paste before cursor / above line
Undo / Redo
u- undoctrl + r- redo
Dot repeat (very important)
.- repeats the last change (anything that modified text).
Visual mode
v- character-wise selectionV- line-wise selectionctrl + v- block / column selection
v - start selection
vww - move 2 words forwardCommon action on selection
d- deletey- yankc- change (delete + insert)~- toggle casegU / gu- upper / lower
Line wise (V)
V- select current lineVjj- select current + 2 lines below
Then
> / <- indent / outdent
Example: Vjj< / Vjj >
Visual Block
Suppose you have foo = 1; bar = 2; baz = 3;
- Put cursor on
=of the first line - Press
ctrl+vjj, column block over three lines. - Type
Ithen add a spcae. e.g.,I, then<Esc>.
Search and Replace
Basic searching
/word + Enter- search forward?word + Enter- search backwardn- next matchN- previous match*- search word under cursor (forward, whole word)#- same as*but backward
Substitue
:s/old/new/- substitue on current line:s/old/new/g- substitue globally (all matches on line):%s/old/new/g (all matches)- substitue (s) in the whole (%) file
With confirmation
Type - :%s/old/new/gc
Then:
y- yesn- noa- allq- quit
using regex
:%s/\vfoo\d+/bar/g
\d+= one ore more digits- Replace
foo123tobar
Text Objects (the real power)
Pattern to remember:
operator + text object - daw, ci" vi(, etc.
i- inner (inside, no surrounding spaces / brackets)a- around (includes surrounding spaces / brackets)
Use them with:
d(delete),c(change),y(yank),v(visual select),gU / gu(upper/lower), etc
- Word text objects:
iw/aw
foo bar baz
^Cursor on b:
diw- deletebardaw- deletebarand following spaceciw- chagebar(with type new word)viw- visually selectbar
- Sentence and paragraph:
is/as,ip/ap
This is a sentence. Here is another one.
This is a new paragraph.Cursor inside the first sentence:
dis- delete inner sentencedas- delete sentence + trailing space/punctuationdip- delete inner paragraphvip- visually select paragraph content
- Quotes
i"/a"etc
const msg = "Hello world!";
^
Cursor on Hello:
ci- change inside"...":- deletes
Hello world! - puts you in insert mode to type new text
- deletes
vi"- selectHello world!da"- delete"Hellow world!"including Quotes
Works for ', " ```:
i',a',i",a", i``, a``
- Paranthese / brackets / brackets
foo(bar, baz + qux)
^
Cursor somewhere inside (...):
ci(- change inside paranthesesdi(- delete inside paranthesesva(- select(bar, baz + qux)include(and)vi(- selectbar, baz + quxonly
Same pattern for { / }, [ / ], < / >
- Tags:
it/at(HTML / XML) For HTML/XML and many setups:
<div class="box">
<span>Hello</span>
</div>Cursor inside span:
vit- select inside<span>...</span>dat- delete whole tag pair and contentcit- change inside tag content
If this doesn't work, you may need :set matchpairs+=<:> or a plugin, but many Vim configs support t by default.
Thank you for taking the time to read this post. I hope it helps you become faster and more confident with Vim - happy hacking!