How do I select a rectangular block past the end of shorter lines in visual block mode?
Answer
:set virtualedit=block
Explanation
By default, Vim's visual block mode (<C-v>) is limited by line length — if some lines are shorter than others, the block selection gets ragged. Setting virtualedit=block allows the cursor to move freely into virtual space beyond the end of lines during block selections, making it possible to create perfect rectangular selections regardless of line length.
How it works
:set virtualedit=block— enables virtual editing only in visual block mode, allowing the cursor to be positioned past the actual end of a line- With this set,
<C-v>selections form a true rectangle even when lines have different lengths - Inserting text with
IorAin this mode pads shorter lines with spaces as needed to maintain alignment
Example
Given lines of varying length:
foo bar
hi
longer line here
Without virtualedit=block, selecting a block from column 8 onward is impossible on the short hi line. With it enabled, you can select a clean rectangle across all three lines at any column position.
:set virtualedit=block
<C-v> " enter visual block mode
2j10l " select a rectangle spanning all 3 lines
I// <Esc> " insert '// ' at column position on every line
Tips
- Other values:
virtualedit=allallows free cursor movement everywhere (not just block mode), andvirtualedit=insertallows it in insert mode - This is particularly useful when editing columnar data, ASCII art, or adding aligned comments to code with varying indentation
- Combine with
$in block selection to extend to the longest line regardless of individual line lengths