askvity

How Do I Edit Multiple Lines in the Vi Editor?

Published in Vi Editor Multi-Line Editing 5 mins read

To edit multiple lines simultaneously in the Vi or Vim editor, you can use Visual Block mode for block operations or Ex commands for line-based operations.

Editing multiple lines in Vi/Vim often involves selecting a block of text or specifying a range of lines to apply a command to.

Using Visual Block Mode

One common and flexible method, particularly useful for inserting or deleting text at the same column across multiple lines, is Visual Block mode.

Here’s how you use it, incorporating the method described in the reference:

  1. Enter Visual Block mode: Navigate to the beginning of the block of text you want to edit. Press Ctrl + v. You will see -- VISUAL BLOCK -- at the bottom of the screen (in Vim).
  2. Select the lines: Use the arrow keys or j (down) and k (up) to extend the highlighted block vertically to cover the desired lines. You can also use h (left) and l (right) or other movement keys to select horizontally, forming a rectangular block.
  3. Perform the edit: Once the block is selected, you can apply various commands:
    • Insert text: Press I (capital i). Type the text you want to insert. This text will appear before the selected block on the first line. Press Esc. The text will be inserted on all selected lines at the starting column of the block.
    • Append text: Press A (capital a). Type the text you want to append. This text will appear after the selected block on the first line. Press Esc. The text will be appended on all selected lines at the ending column of the block.
    • Delete text: Press d or x. The entire selected block will be deleted.
    • Replace text: Press r followed by the character you want to replace the entire block with.
    • Change text: Press c. The selected block is deleted, and you enter Insert mode to type new text for the first line. Press Esc to apply the change to all lines in the original block.

Example: Inserting text at the beginning of multiple lines

Imagine you have this text:

line one
line two
line three

You want to add * to the beginning of each line:

  1. Move cursor to the start of "line one".
  2. Press Ctrl + v (-- VISUAL BLOCK -- appears).
  3. Press j twice to select all three lines at the first column.
  4. Press I (Insert).
  5. Type * (an asterisk and a space).
  6. Press Esc.

The result will be:

* line one
* line two
* line three

Using Ex Commands (Command-Line Mode)

Another powerful way to edit multiple lines is by using Ex commands (commands starting with :) which operate on specified line ranges.

You define a range using line numbers, patterns, or symbols:

  • : - Current line
  • :$ - Last line
  • :n - Line number n
  • :n,m - Lines from n to m
  • :.,$ - From current line to the last line
  • :% - All lines (equivalent to 1,$)
  • :/pattern/ - The next line containing "pattern"
  • :/pattern1/,/pattern2/ - From the line containing "pattern1" to the line containing "pattern2"

After specifying the range, you add the command. Common commands include:

  • s/old/new/g - Substitute "old" with "new" globally on each line in the range.
  • d - Delete lines in the range.
  • co m - Copy lines in the range to after line m.
  • m m - Move lines in the range to after line m.
  • norm {commands} - Execute normal mode commands on each line in the range.

Example: Replacing text on a range of lines

To replace the word "old" with "new" on lines 10 through 20:

  1. Press :.
  2. Type 10,20s/old/new/g.
  3. Press Enter.

Example: Deleting lines based on a pattern

To delete all lines containing the word "error":

  1. Press :.
  2. Type g/error/d.
  3. Press Enter. (The g command searches for the pattern globally and applies the following command d to each matching line).

Summary of Multi-Line Editing Techniques

Method Mode Used How to Select Lines Common Operations Use Case
Visual Block Visual Block (Ctrl+v) Select a rectangular block using movement keys Insert/Append (I/A), Delete (d/x), Change (c), Replace (r) Editing text at specific columns, block operations
Ex Commands Command-Line (:) Specify line numbers or patterns (e.g., 10,20, %, g/pattern/) Substitute (s), Delete (d), Copy (co), Move (m), Normal mode commands (norm) Applying operations to ranges of lines, search and replace

Both methods are incredibly useful for efficient multi-line editing in Vi/Vim, allowing you to manipulate text blocks or apply commands across specified line ranges quickly.

Related Articles