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?
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'.
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.
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}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With