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:
- 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). - Select the lines: Use the arrow keys or
j
(down) andk
(up) to extend the highlighted block vertically to cover the desired lines. You can also useh
(left) andl
(right) or other movement keys to select horizontally, forming a rectangular block. - 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. PressEsc
. 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. PressEsc
. The text will be appended on all selected lines at the ending column of the block. - Delete text: Press
d
orx
. 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. PressEsc
to apply the change to all lines in the original block.
- Insert text: Press
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:
- Move cursor to the start of "line one".
- Press
Ctrl + v
(-- VISUAL BLOCK --
appears). - Press
j
twice to select all three lines at the first column. - Press
I
(Insert). - Type
*
(an asterisk and a space). - 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 numbern
:n,m
- Lines fromn
tom
:.,$
- From current line to the last line:%
- All lines (equivalent to1,$
):/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 linem
.m m
- Move lines in the range to after linem
.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:
- Press
:
. - Type
10,20s/old/new/g
. - Press
Enter
.
Example: Deleting lines based on a pattern
To delete all lines containing the word "error":
- Press
:
. - Type
g/error/d
. - Press
Enter
. (Theg
command searches for the pattern globally and applies the following commandd
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.