Lorem Ipsum
Lorem Ipsum

Reputation: 4534

Vim: Select rectangular block past end of line

How do I select a rectangular block of text which extends beyond the end of the line in Vim?

Suppose that I have

aa
bbb
cc
dddd
ee

I would like to select a rectangular block that extends four characters on all lines. If _ is considered white-space, then I want:

aa__
bbb_
cc__
dddd
ee__

The rectangular visual block, C-v, only extends as far as the end of the last line selected:

rectangular select which extends beyond the end of the line

In Emacs, I can do what I want using C-x <SPC> (rectangle-mark-mode). ;)

Upvotes: 16

Views: 2499

Answers (2)

SergioAraujo
SergioAraujo

Reputation: 11800

Using substitution

:%s,^..$,&__,g | %s,^...$,&_,g

: ............ command
% ............ whole file
^ ............ begining of line
.. ........... two characters
$ ............ end of line
& ............ the whole search pattern
__ ........... (plus) the chars we want
g ............ globally
| ............ another command (this time for 3 chars)

Upvotes: -1

Ingo Karkat
Ingo Karkat

Reputation: 172570

blockwise visual mode with ragged border

To extend the blockwise visual selection to the end of all covered lines, you can press $ to switch Vim into a "ragged border" selection mode. This "trick" is mentioned at :help v_b_A:

With a blockwise selection, A{string} will append {string} to the end of block on every line of the block. There is some differing behavior where the block RHS is not straight, due to different line lengths:

  1. Block was created with $ In this case the string is appended to the end of each line.
  2. Block was created with {move-around} In this case the string is appended to the end of the block on each line, and whitespace is inserted to pad to the end-of-block column.

virtual edit

Another way to solve this is via the 'virtualedit' option:

:set virtualedit=all

This makes the space following the end of the line accessible to cursor movements, so you can extend the selection as much as you need. Yanking that text will have whitespace padding inserted to make a rectangular block, so the behavior is different to the above alternative.

Upvotes: 15

Related Questions