Vim: move by word, line and search
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.confMoving
| Keys | Moves to |
|---|---|
w / b / e | the next word / back a word / the end of the word |
0 / ^ / $ | the start of the line / its first character / its end |
gg / G | the first line / the last line |
17G or :17 then Enter | line 17 |
/max then Enter | the next max; n for the one after, N back |
f= | the next = on this line (; repeats it) |
j / k | down / 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
| Keys | Does |
|---|---|
x | deletes the character under the cursor (3x deletes three) |
r8 | replaces the character under the cursor with 8, and stays in normal mode |
A | starts typing at the end of the line |
o / O | opens a new line below / above and starts typing |
u | undoes 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.
Your task
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.
- max_upload is 8M (not done yet)
- gzip = on is no longer a comment (not done yet)
- owner = ops is the new last line (not done yet)
- Nothing else changed, saved and closed (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
vim w b e- Move to the start of the next word (w), back a word (b), to the end of the word (e) More on vim
vim 0 $- Move to the start (0) or the end ($) of the line More on vim
vim gg G- Go to the first line (gg), the last line (G), or line N (NG or :N) More on vim
vim /text- Search forward for text; n finds the next match, N the one before More on vim
vim f- f followed by a character jumps to that character on the line More on vim
vim x r- x deletes the character under the cursor; r then a character replaces it More on vim
vim A o- A adds text at the end of the line; o opens a new line below More on vim
vim counts- A number before a command repeats it: 3dd deletes three lines, 5j moves down five More on vim
Every everyday command on one page: the Linux commands cheat sheet.