vim Essentials

You can exit vim now. Let's make you productive in it.

All commands here work in Normal mode. Press Esc first if unsure.

Basic Movement

Terminal
$h
← left
$j
↓ down
$k
↑ up
$l
→ right

Arrow keys work too, but hjkl keeps hands on home row.

Word Movement

Terminal
$w
forward to start of next word
$b
backward to start of word
$e
forward to end of word

Line Movement

Terminal
$0
beginning of line
$$
end of line
$^
first non-blank character

File Movement

Terminal
$gg
first line of file
$G
last line of file
$50G
go to line 50
$:50
also goes to line 50

Combine Numbers with Movement

5j moves down 5 lines. 3w moves forward 3 words. Numbers multiply commands.

Entering Insert Mode

Multiple ways to start typing:

Terminal
$i
insert before cursor
$a
append after cursor
$I
insert at beginning of line
$A
append at end of line
$o
open new line below
$O
open new line above

Most used: i (insert), A (append to line), o (new line below).

Deleting Text

In Normal mode:

Terminal
$x
delete character under cursor
$dd
delete entire line
$dw
delete word
$d$
delete to end of line
$d0
delete to beginning of line

Delete Multiple

Terminal
$3dd
delete 3 lines
$5x
delete 5 characters
$d5w
delete 5 words

Copy and Paste

vim calls copy "yank":

Terminal
$yy
yank (copy) line
$yw
yank word
$y$
yank to end of line
$p
paste after cursor
$P
paste before cursor

Delete = Cut

Everything you delete with d is actually cut - you can paste it with p.

Undo and Redo

Terminal
$u
undo
$Ctrl+r
redo
Terminal
$/pattern
search forward
$?pattern
search backward
$n
next match
$N
previous match

Example:

Terminal
$/error
(highlights 'error' matches)
$n
(jump to next 'error')
$N
(jump to previous 'error')

Search and Replace

Terminal
$:s/old/new/
replace first on line
$:s/old/new/g
replace all on line
$:%s/old/new/g
replace all in file
$:%s/old/new/gc
replace all with confirmation
Knowledge Check

How do you delete an entire line in vim?

Common Workflows

Quick Edit

vim file.txt  → Open
/search       → Find text
i             → Insert mode
(make edits)
Esc           → Normal mode
:wq           → Save and quit

Replace All Occurrences

:%s/oldtext/newtext/g
:wq

Quick Reference

CommandAction
hjklMove cursor
w/bWord forward/back
gg/GStart/end of file
i/a/oInsert mode
ddDelete line
yyCopy line
pPaste
uUndo
/textSearch
:%s/a/b/gReplace all

Key Takeaways

  • hjkl for movement (or arrow keys)
  • i to insert, Esc to return to Normal
  • dd deletes lines, yy copies lines
  • p pastes, u undoes
  • / searches, n/N navigates matches
  • :%s/old/new/g replaces throughout file

Next: more vim power - visual mode and advanced commands.