description |
---|
A gentle guide to vim |
Vim is an improved version of vi editor. Its interface can be scary at the beginning but after understanding its modes and a few commands, it can be very productive.
Vim has two important modes to know:
- command mode: this is the default mode, you can go to other modes from the command.
- From the command mode type
i
to go to insert mode.
- From the command mode type
- insert mode: this is for editing a file as you are expecting, i.e., what you type will be inserted where the cursor is blinking.
- Press
ESC
to go back to command mode.
- Press
h
,J
,k
andl
move the cursor one character to left, down, up and right. Arrow keys also work.0
(zero) and$
move the cursor to the beginning and end of the line. Home and End keys also work.w
andb
move the cursor to the next and the previous word. Using Ctrl + left and right arrows move from words separated by spaces.gg
andG
move the cursor to the beginning or end of file. Ctrl+Home and Ctrl+End also work.
The commands can also be executed multiple times when preceded by a number, e.g., 3w will move the cursor 3 words forward.
d
delete from cursor, e.g.,dh
deletes the character on the left,d0
deletes until the beginning of the text.u
andCtrl+r
undo and redo.
v
,V
andCtrl+v
go to visual from the cursor, entire line and by columns
y
copyp
andP
: paste after current line and on the current line
/text
and?text
search "text" going forward and backwardn
andN
go to next occurrence going forward and backward:%s/text/new text/gc
and:%s/text/new text/g
replace "text" by "new text" asking to confirm on every occurrence and replacing without asking.
:q!
discard changes and quit:w
save changes:wq
save and quit