Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim - swap columns with visual mode

I have a markdown table like this. I want to swap head3 column with head2.

| head1 | head3 | head2 |
|-------|-------|-------|
| foo   | baa   | none  |
| some  | text  | here  |

I can easily cut the column using visual mode (Ctrl-V), but how can I paste the column 'column-wisely'?

Also, which operating is easiert:

  1. cut 'head 3' and paste behind 'head2'
  2. cut 'head 2' and paste before 'head3'?
like image 521
lockdoc Avatar asked Jan 28 '16 15:01

lockdoc


People also ask

How do I use visual block mode in Vim?

Press Ctrl+v to enter visual block mode. The words VISUAL BLOCK will appear at the bottom of the screen. Use the Arrow keys to highlight the single character column. You can verify that each task is indented the same amount.

How do I select vertically in Vim?

ctrl+shift+v is visual block. So just press ctrl+shift+v and then use cursor keys to select what you want.

How do I switch lines in Vim?

To swap the current line with the next one, type ddp while in command mode.


1 Answers

When you've selected and cut something with Ctrl+V, Vim will paste it as a column too. You can Ctrl+V select the column you want to swap into and paste, and the column you just replaced will now be in your paste register. Go back to the column you cut first and paste one more time to move the replaced column.

In steps:

  • Use Ctrl+V to select the entire head3 column
  • x to cut
  • Use Ctrl+V again to select the entire head2 column
  • p to paste
  • Move back to where head3 used to be
  • p to paste

Column swap demo

like image 120
Kristján Avatar answered Sep 17 '22 23:09

Kristján