How to reverse the order of line fragments within a visual block in Vim?

Viewed 120

I would like to reverse the order of line fragments selected within a Visual Block in vim. That is, starting from

one   = [33];
two   = [22];
three = [11];

I would like to get,

one   = [11];
two   = [22];
three = [33];

by selecting the block of

         33
         22
         11

and reversing the line order only within this block.

Can this be achieved? I tried using !tac, as described in a related question, but that reversed entire lines, not just the selected block.

2 Answers

There's a plugin called "vis" that tries to do exactly what you're looking for: https://github.com/vim-scripts/vis

After installing it, you can select the columns in visual mode and execute :B sort to sort just that area.

As an unfortunate side effect, I seem to be getting extra spacing around the selection:

one   = [ 11 ];
two   = [ 22 ];
three = [ 33 ];

This might be some oddity in my own Vim config, or it might be a general issue. I'd suggest you try the plugin and see for yourself.

Using vim-exchange plugin we can do the following: Place the cursor over the first number and type cxi[ then 2j.

I am giving this plugin based solution because I think "vim-exchange" it is a more common plugin, specially after those very famous vim screencasts.

Related