Search code examples
vimcopy-pastepastecut

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'?

Solution

  • 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