Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Advanced Vi Editing Commands: The Full Guide

· 6 min read · Home Tech

In this article
  1. Why Learn Advanced Vi Commands?
  2. Text Objects and Operators: The Core of Advanced Editing
  3. Registers: The Clipboard You Never Knew You Had
  4. Macros: Record and Replay Complex Sequences
  5. Visual Block Mode: Column Editing
  6. Ex-Mode Commands: Global Search and Replace
  7. Common Mistakes and How to Avoid Them
  8. 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., ciw changes the current word)
  • i" – inside double quotes (e.g., di" deletes the quoted content)
  • i( or ib – inside parentheses (e.g., ci( changes text in parens)
  • i{ or iB – inside curly braces (e.g., yi{ copies the block)
  • it – inside a tag (useful for HTML/XML, e.g., cit changes 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 register a.
  • Paste from register: "ap – pastes the content of register a after the cursor.
  • Append to register: "Ayy – appends the line to register a (useful for collecting lines).
  • View registers: :reg or :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.

  1. Press q followed by a letter (e.g., qa) to start recording into register a.
  2. Perform your edit sequence (e.g., 0 to go to line start, i to insert, type ", Esc, e to go to end of word, a to append, type ", Esc).
  3. Press q again to stop recording.
  4. Replay with @a (once) or 5@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 press Esc – the text appears on every selected line at the same column.
  • Change text: Press c to delete the block and enter insert mode, affecting all lines.
  • Append text: Press A to 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 register A (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/\/replacement/g 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 d and c: d deletes and leaves you in normal mode; c deletes and enters insert mode. If you find yourself pressing i after a delete, you probably wanted c.
  • Using i" when you meant a": 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 u to undo the partial changes, fix the macro, and re-run.
  • Mixing up :g and :s: :g selects lines based on a pattern, :s replaces text. They are often used together, e.g., :g/foo/s/bar/baz/g replaces bar with baz only on lines containing foo.

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.