Vim swap first column of a file with second column

In this article we will see how we would swap first column of a file with second column.

Lets take a sample file that we need to perform the substitution. Below is a sample file in.txt which has two columns separated by the delimiter TAB.

Now we want to swap column1 with column2. So open the file in.txt using vim. So type vim in.txt in the command line.

Then press : and type %s/\(.\)\t\(.\)/\2\t\1/g. Press enter and see the magic.

In the image you can see at the bottom the command that does the magic. We have two capturing groups separated by the delimiter tab to capture first column and second column.

Now the columns are swapped with each other. Similarly we can use comma as delimiter if you have comma as delimiter in your file.

Start a Conversation

Your email address will not be published. Required fields are marked *