Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM: swap two columns separated by space

Tags:

vim

I have two columns:

cats dog  
dog cats

I want to swap the two columns:

dog cats
cats dog 

Using vim how would i do this?

like image 456
4reel77 Avatar asked Jun 25 '14 22:06

4reel77


People also ask

How do I swap two columns in awk?

To keep the tabulation intact, you need to specify the separator. Incoming separator is defined by -F $'\t' and the separator for the output is defined by OFS=$'\t'.

How do you switch two columns in Unix?

The simple swapping of 2 fields using a temporary variable is done here. Two variables i and j contain the field numbers which are to be swapped. Just be setting the appropriate numbers in these 2 variables, any set of columns can be swapped. The 1 outside the braces prints every line by default.


1 Answers

My first idea, a substitution, ended up looking too much like Birei's so I went with AWK (that I don't know very much) used as a filter:

:%!awk '{print $2, $1}'
like image 133
romainl Avatar answered Sep 18 '22 17:09

romainl