Advanced Vi Editing Commands: The Full Guide
In this article
- Why Learn Advanced Vi Commands?
- Text Objects and Operators: The Core of Advanced Editing
- Registers: The Clipboard You Never Knew You Had
- Macros: Record and Replay Complex Sequences
- Visual Block Mode: Column Editing
- Ex-Mode Commands: Global Search and Replace
- Common Mistakes and How to Avoid Them
- Frequently Asked Questions
This guide covers the advanced vi/vim commands that go beyond basic navigation and insertion, giving you the power to edit text at the speed of thought. You'll learn how to use registers, macros, visual blocks, and ex-mode commands to perform complex edits with a few keystrokes. By the end, you'll be able to replace repetitive tasks with single commands and navigate a file with surgical precision.
Why Learn Advanced Vi Commands?
Vi (and its modern successor Vim) is a modal editor, meaning the same keys do different things depending on the mode you're in. Most users learn the basics—i to insert, w to move a word, dd to delete a line—but stop there. The real power lies in combining commands with counts, text objects, and operators. Once you internalize these, you can delete everything inside quotes, change a word across the whole file, or reorder lines with a single command. This isn't about showing off; it's about reducing the friction between thinking and editing.
Text Objects and Operators: The Core of Advanced Editing
Text objects are the building blocks that let you target specific parts of your code or prose. They work with operators like d (delete), c (change), and y (yank/copy). The syntax is operator + text object.
iw– inner word (e.g.,ciwchanges the current word)i"– inside double quotes (e.g.,di"deletes the quoted content)i(orib– inside parentheses (e.g.,ci(changes text in parens)i{oriB– inside curly braces (e.g.,yi{copies the block)it– inside a tag (useful for HTML/XML, e.g.,citchanges the tag content)a"– includes the quotes (e.g.,da"deletes the quotes and content)
Combine with counts: 2daw deletes two words including the trailing space. This is far faster than moving to the start, entering insert mode, and deleting character by character.
Registers: The Clipboard You Never Knew You Had
Vi has 26 named registers (a–z) plus special ones like the unnamed register (""), the numbered registers ("0 to "9), and the system clipboard ("+). You can store text in a register and paste it later, even after many other edits.
- Yank to register:
"ayy– yanks the current line into registera. - Paste from register:
"ap– pastes the content of registeraafter the cursor. - Append to register:
"Ayy– appends the line to registera(useful for collecting lines). - View registers:
:regor:display– shows all registers and their contents.
The numbered registers are especially handy: "0 always holds the last yank, while "1 through "9 hold the last nine deletions or changes. If you accidentally delete something, "1p brings it back.
Macros: Record and Replay Complex Sequences
Macros let you record a series of keystrokes and replay them as many times as you need. This is a game-changer for repetitive edits like adding a semicolon to every line or wrapping every word in quotes.
- Press
qfollowed by a letter (e.g.,qa) to start recording into registera. - Perform your edit sequence (e.g.,
0to go to line start,ito insert, type",Esc,eto go to end of word,ato append, type",Esc). - Press
qagain to stop recording. - Replay with
@a(once) or5@a(five times).
For a whole file, you can combine a macro with a range: :%normal @a runs the macro on every line. If you make a mistake mid-macro, just undo and re-record—it's rarely worth fixing manually.
Visual Block Mode: Column Editing
Visual block mode (Ctrl+v) allows you to select a rectangular block of text, not just contiguous lines. This is perfect for editing columns of data, adding comments to multiple lines, or inserting text at the same column position.
- Select a block: Move to the top-left corner, press
Ctrl+v, move down and right to select the area. - Insert text: Press
I(capital i), type the text, then pressEsc– the text appears on every selected line at the same column. - Change text: Press
cto delete the block and enter insert mode, affecting all lines. - Append text: Press
Ato append after the block on all lines.
For example, to comment out 10 lines of code, select the first column of those lines with Ctrl+v, press I, type #, then Esc. This is far more efficient than editing each line.
Ex-Mode Commands: Global Search and Replace
Ex-mode (commands starting with :) is where vi's scripting power shines. The :global command lets you run an ex command on every line matching a pattern.
:g/pattern/d– delete all lines containing pattern.:g/pattern/y A– yank all matching lines into registerA(appending).:g!/pattern/d– delete all lines that do not match (the!inverts).:v/pattern/d– same as:g!, a common shorthand.
For search and replace, you can use the s command with a range:
:s/foo/bar/g– replace all occurrences on the current line.:%s/foo/bar/g– replace all occurrences in the file.:1,10s/foo/bar/g– replace only in lines 1 through 10.:%s/foo/bar/gc– ask for confirmation before each replacement.
You can also use regular expressions in patterns, e.g., :%s/\ to match whole words only.
Common Mistakes and How to Avoid Them
Even experienced users trip up on a few things. Here are the most frequent pitfalls:
- Forgetting the difference between
dandc:ddeletes and leaves you in normal mode;cdeletes and enters insert mode. If you find yourself pressingiafter a delete, you probably wantedc. - Using
i"when you meanta":i"selects inside the quotes,a"includes them. For example,di"leaves the quotes,da"removes them. - Macro playback errors: If a macro fails halfway through, the rest of the lines are untouched. Press
uto undo the partial changes, fix the macro, and re-run. - Mixing up
:gand:s::gselects lines based on a pattern,:sreplaces text. They are often used together, e.g.,:g/foo/s/bar/baz/greplacesbarwithbazonly on lines containingfoo.
Frequently Asked Questions
How do I undo a macro recording?
If you start recording and make a mistake, just press q again to stop, then use u to undo the changes you made during recording. The macro itself is overwritten when you record again with the same letter.
Can I use these commands in plain vi, or only Vim?
Most of these commands work in both vi and Vim. Visual block mode (Ctrl+v) and the + register for the system clipboard are Vim additions, but text objects, macros, and ex commands are standard vi features. If you're on a modern system, you likely have Vim installed.
How do I copy a block of text from vi to another application?
Use the system clipboard register: first yank the text (e.g., "+yy to yank a line), then paste it in the other application. To paste from the system clipboard into vi, use "+p. If these don't work, check that your Vim was compiled with clipboard support (vim --version and look for +clipboard).
What is the difference between :wq and :x?
Both write the file and quit. The difference is that :x only writes if the file has been modified, while :wq always writes. This can matter if you want to preserve the file's timestamp when you haven't changed anything.
How do I edit a file in place without opening it?
You can use ex commands directly from the shell: vi -c '%s/foo/bar/g' -c 'wq' file.txt. This opens the file, runs the substitution, saves, and quits. For more complex edits, consider using sed or awk, but for quick replacements, this works well.
Is it worth learning advanced vi commands when I already use a GUI editor?
If you spend a lot of time editing text, the answer is yes. The learning curve is steep, but the payoff is speed and the ability to work over SSH or in environments without a GUI. Start with text objects and macros—they give the biggest boost for the least effort.