tags:

views:

65

answers:

2

I already know how to do it with

:%s/\(\S\+\)^I\(\S\+\)/\2^I\1/

but I feel like I'm typing way to much stuff. Is there a cleaner, quicker way to do it?

+5  A: 

If the columns are lined up, you can use visual block mode by hitting Ctrl+V, then cut and paste. If the columns are not lined up, increase the tab width first so that it's longer than the content of the columns in question.

Brian
Thanks, that seems to work. I don't think I've fully exploited visual block mode in general.
mmarchin
Block mode is great. Try selecting a block and using I or A to add the same text before or after each line in the block (like for commenting out code).
pydave
+1  A: 

Best way to do it in VIM is - not to do it with VIM and (re)use existing tools for the job. *NIX specific solution:

:%!awk -F \\t '{print $2 FS $1}'

Would pipe the content of the tab-delimited file to awk and it will print first two columns swapped, separated by field separator (FS). awk can be also found for Windows.

P.S. Initially I wanted to write the same with cut but for whatever reason on my system the cut -f 2,1 (-d is not needed as TAB is the default delimiter) printed the fields in the same order, not swapped :|

Dummy00001
Yeah, I'm aware of this awk command, but I was actually looking for something more elegant. I also use cut a lot, it doesn't rearrange columns though.
mmarchin