Vim operators: dw, cw, ci" and the dot command
Edit like a Vim user: delete and change with d and c plus a motion, ci" to change inside quotes, counts like 3dd, and . to repeat the last change.
Vim's editing keys work like a small language: an operator (what to do) followed by a motion (how far). Once you know a few of each, you can combine them without learning anything new.
| Operator | Does |
|---|---|
d | delete |
c | change: delete, then start typing |
y | yank (copy) |
Put any motion after it: dw deletes to the next word, d$ to the end of the line, dG to the end of the file, cw changes the rest of the word. Press the operator twice for the whole line: dd deletes a line, cc changes it, yy copies it. p pastes what you deleted or copied, after the cursor (or below, for lines).
vim deploy.ymlCounts
A number in front repeats: 3dd deletes three lines, d2w two words, 5x five characters.
Text objects: inside and around
After an operator, i means inside and a around something:
| Keys | Changes or deletes |
|---|---|
ciw | the word under the cursor, wherever in it you are |
ci" | the text inside the quotes on this line |
ca" | the text and the quotes too |
ci( / ci{ | inside brackets or braces |
dap | a whole paragraph, with the blank line after it |
ci" is a favourite: the cursor can be anywhere on the line before the quotes, and Vim finds them.
The dot command
. repeats your last change. Change one word with cwnew and Esc, move to the next place (n after a search is perfect for this), and press . to make the same change again. Search, change, then n.n.n.: that's how Vim users rename things.
Your turn
In deploy.yml: (1) change the host inside the quotes from old.example.com to shop.example.com; (2) delete the three legacy_ lines; (3) change every staging to production, using . to repeat the change. Save and quit.
Your task
In deploy.yml: (1) change the host inside the quotes from old.example.com to shop.example.com; (2) delete the three legacy_ lines; (3) change every staging to production, using . to repeat the change. Save and quit.
- host is "shop.example.com" (not done yet)
- The three legacy lines are gone (not done yet)
- Every staging is now production, with . doing the repeats (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 d- Delete: d plus a motion, such as dw (a word), d$ (to the end of the line) or dd (the line) More on vim
vim c- Change: delete plus insert, such as cw (the rest of the word) or cc (the whole line) More on vim
vim y p- y copies (yanks), such as yy for the line; p pastes after the cursor More on vim
vim ci"- Change inside the quotes: deletes the text between " and " and starts typing More on vim
vim .- The dot repeats your last change, wherever the cursor is now 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.