Skip to main content

How to Edit Columns of Text in Vim

·1151 words·6 mins
Linux Learning Lab
Author
Linux Learning Lab
Writing about code, tools, and workflows.

What Is Visual Block Mode?
#

Normal visual mode (v) selects characters in a stream. Visual line mode (V) selects whole lines. Visual block mode (Ctrl-V) selects a rectangle — a column of text spanning multiple lines.

This lets you operate on vertical slices of a file: insert a prefix on 20 lines at once, delete a column of data, replace aligned text, or reindent a block — all without regex or macros.

Entering Visual Block Mode
#

Press Ctrl-V (sometimes shown as <C-v>) in normal mode. You’ll see -- VISUAL BLOCK -- in the status bar.

Then move to expand the selection:

KeyMovement
j / kExtend down / up
l / hExtend right / left
wExtend by word
$Extend to end of each line
}Extend to next blank line
GExtend to end of file

The selection is always rectangular — it doesn’t follow line length.

Note: On some terminal emulators or with tmux, Ctrl-V might be intercepted for paste. If that happens, use Ctrl-Q instead, or remap your terminal’s paste shortcut.

Insert Text on Multiple Lines
#

This is the most common use case — adding a prefix or indent to a block of lines.

Steps:

  1. Place cursor at the starting position
  2. Ctrl-V to enter visual block mode
  3. j (or movement) to extend the selection down
  4. I (capital I) to insert before the block
  5. Type your text
  6. Esc — the text appears on all selected lines

Example — add # to comment out lines:

Before:

server_name example.com
listen 80
root /var/www/html

With cursor on line 1 column 1: Ctrl-V, 2j, I, # , Esc

After:

# server_name example.com
# listen 80
# root /var/www/html

Append After a Block
#

Use A (capital A) instead of I to append text after the selection:

  1. Ctrl-V, select the block
  2. $ to extend to end of line (important for ragged lines)
  3. A to append
  4. Type your text
  5. Esc

Example — add a trailing comment:

Before:

port = 8080
host = localhost
debug = true

Select all lines with Ctrl-V, 2j, $, A, # default, Esc

After:

port = 8080  # default
host = localhost  # default
debug = true  # default

Delete a Column
#

Select a rectangle and press d (or x) to delete it.

Example — remove line numbers from pasted output:

Before:

  1  fn main() {
  2      println!("hello");
  3  }

Place cursor on line 1 column 1, Ctrl-V, 2j, 4l, d

After:

fn main() {
    println!("hello");
}

Replace a Block
#

Select a rectangle and press r followed by a character to replace every character in the selection.

Example — redact a column:

Before:

user    password    role
alice   s3cr3t!     admin
bob     hunter2     user
carol   p@ssw0rd    user

Select the password column: position cursor on s of s3cr3t!, Ctrl-V, 2j, 7l, r, *

After:

user    password    role
alice   ********    admin
bob     ********    user
carol   ********    user

Change a Block
#

Select a rectangle and press c to delete the selection and enter insert mode. What you type replaces the block on every line.

Example — change a repeated value:

Before:

ENV=production
ENV=production
ENV=production

Select production on all lines: Ctrl-V, 2j, e, c, staging, Esc

After:

ENV=staging
ENV=staging
ENV=staging

Shift and Indent a Block
#

You don’t always need visual block mode for indentation — visual line mode (V) with > and < works well. But visual block is useful when you want to indent only part of a line or a precise column.

Indent lines with visual line mode:

V, select lines, > to indent, < to outdent

Insert spaces at a specific column with block mode:

  1. Ctrl-V, select the column position across lines
  2. I, type spaces, Esc

Working with Aligned Data
#

Visual block mode shines when editing tabular or column-aligned text.

Reorder Columns
#

Combine block select with delete and paste:

  1. Select a column with Ctrl-V
  2. d to cut it
  3. Move to the destination
  4. p to paste the block

Align Equals Signs
#

If you have misaligned assignments:

name = "alice"
port = 8080
max_connections = 100
debug = true

Select the column after the longest key, use I to add spaces where needed. Or use block delete to remove extra spaces first, then re-add them uniformly.

Number a List
#

You can use g Ctrl-A to create incrementing numbers after inserting a column of identical starting values:

  1. Write a 0 (or 1) at the start of each line using block insert
  2. Select all the numbers with Ctrl-V
  3. g Ctrl-A to increment them sequentially

Before (after inserting 0. on each line):

0. First item
0. Second item
0. Third item
0. Fourth item

Select the column of 0s, then g Ctrl-A:

1. First item
2. Second item
3. Third item
4. Fourth item

Practical Scenarios
#

Comment/Uncomment a Block of Code
#

Comment out:

Ctrl-V → select lines → I → // → Esc

Uncomment (remove // ):

Ctrl-V → select lines → 2l (to cover "// ") → d

Add Quotes Around a Column of Values
#

Before:

alice
bob
carol

Add opening quote: Ctrl-V, 2j, I, ", Esc Add closing quote: Ctrl-V, 2j, $, A, ", Esc

After:

"alice"
"bob"
"carol"

Fix Indentation on Pasted Code
#

When code pastes with wrong indentation, select the misaligned block with Ctrl-V and either delete extra spaces or insert missing ones.

Convert Spaces to Tabs (or Vice Versa) in a Region
#

Select the leading whitespace column and replace it:

  1. Ctrl-V, extend down across lines
  2. Select the whitespace width
  3. c, type the replacement whitespace, Esc

Tips
#

  • $ makes ragged lines uniform. When lines have different lengths, $ extends the selection to the end of every line, so A appends at the true end of each line rather than at a fixed column.

  • O toggles the cursor corner. While in visual block mode, pressing O (capital O) moves the cursor to the opposite corner of the selection. This lets you adjust the selection from either end.

  • gv reselects. If you lose your selection, gv brings back the last visual selection — works for block mode too.

  • Dot repeat works. After a block insert/append, . repeats it. Useful if you need to apply the same edit to another block of lines.

  • : commands apply to block selections. If you press : while in visual block mode, you get :'<,'> which applies commands to the selected line range (not the block rectangle specifically). For true column operations, use the block operators (I, A, d, c, r).

Quick Reference
#

ActionKeys
Enter visual block modeCtrl-V
Insert on all linesCtrl-V, select, I, type, Esc
Append on all linesCtrl-V, select, $, A, type, Esc
Delete a columnCtrl-V, select, d
Replace characters in blockCtrl-V, select, r, char
Change a blockCtrl-V, select, c, type, Esc
Extend to end of lines$
Toggle cursor cornerO
Reselect last selectiongv
Sequential numberingSelect numbers, g Ctrl-A