Reputation: 4534
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:
In Emacs, I can do what I want using C-x <SPC>
(rectangle-mark-mode
). ;)
Upvotes: 16
Views: 2499
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
Reputation: 172570
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:
- Block was created with $ In this case the string is appended to the end of each line.
- 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.
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