Skip to content

Vim: move by word, line and search

AdvancedLesson 38 of 669 min

Stop holding the arrow keys: jump through a config file in Vim with w, b, $, gg, G, line numbers, f and / search, then make quick edits with x, r, A and o.

On a server, the file you need to change is often long, and the line you need is somewhere in the middle. Arrow keys move one character at a time. Vim's motions jump: by word, to the end of a line, to a line number, or straight to a word you search for. In normal mode (press Esc if you're not sure), try them in app.conf:

vim app.conf

Moving

KeysMoves to
w / b / ethe next word / back a word / the end of the word
0 / ^ / $the start of the line / its first character / its end
gg / Gthe first line / the last line
17G or :17 then Enterline 17
/max then Enterthe next max; n for the one after, N back
f=the next = on this line (; repeats it)
j / kdown / up a line

Counts multiply a motion: 3w jumps three words, 5j five lines down. :set number shows line numbers down the side.

Small edits without insert mode

KeysDoes
xdeletes the character under the cursor (3x deletes three)
r8replaces the character under the cursor with 8, and stays in normal mode
Astarts typing at the end of the line
o / Oopens a new line below / above and starts typing
uundoes the last change (Ctrl-r redoes it)

So to change port = 8080 to port = 9090: /8080 Enter, r9, l (one to the right), r9. Then :wq saves and quits.

Your turn

In app.conf: (1) change max_upload = 1M to 8M; (2) turn the comment # gzip = on into a setting by deleting the # in front; (3) add a new last line, owner = ops, below keep = 14. Change nothing else, then save and quit.

Next