jilocoiner
jilocoiner

Reputation: 105

Selecting visual block in the middle of a sentece accross multiple lines in Vim

So, let's imagine I have this code:

print $this_one   = "1";
print $this_tu    = "2";
print $this_three = "3";

and I want to select the middle part using the visual block mode:

$this_one   
$this_tu    
$this_three 

and perhaps append something etc. but that's not important now.

The problem I am facing is that I don't know how to select that.

If it were something like:

print $this_one;
print $this_tu;
print $this_three;

I would just press Ctrl + v then j j and $ and it would be done.

But what if it is in the middle and the words end up on a different columns?

Upvotes: 1

Views: 220

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172570

The blockwise selection with a jagged right edge indeed only works at the end of lines. In the "middle", you're stuck with a rectangular block selection that then includes trailing whitespace.

Unless you switch to a completely different approach (the comments already mention vim-multiple-cursors, which lets you select multiple places and then you can interactively edit all of those in parallel), you have to live with that.

Depending on the command that is applied to the blockwise selection, the trailing whitespace (or even any other characters you inadvertently capture) doesn't necessarily harm.

To append a character (say, $) to all middle words in your example, I would use the vis.vim plugin's :B command to work on the selection only, moving to the end of the word with the E motion, then appending the character with a$:

:'<,'>B normal! Ea$

In this, the whitespace is kept, and everything to the right of the selection moves one character right.

Upvotes: 1

Related Questions