开发者

What's the best way to rearrange columns in a tab delimited text file in vim?

I already know how to do it with

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

but I feel like I'm开发者_如何学JAVA typing way to much stuff. Is there a cleaner, quicker way to do it?


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.


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 :|

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜