Category: vim

Basics of vim – Create, edit, save and quit in vim.

How to use vim in Linux.

Vim has two modes. Command mode and insert mode. In command mode we can tell the editor to substitue, edit, insert, append, save, quit etc. By default vim opens in command mode. To switch to insert mode press “i” in command mode without quotes. Here you can edit the file with whatever content you want to.

To switch to command mode press “Esc” key. Here we can give commands to save and quit. To save press :w then the file will be saved and to save and quit press :wq.

Here is a video enplaning the same.

Swap two columns using Vim in Linux

Suppose you have a file seperated with a delimiter like tab or comma and you want to swap them manually it would take so much time if the file is huge. But if you want to do it using just a single command it would take 5 seconds.

Here is how it is done.

  • Open the file to be edited using following command.
  • vim file.txt
  • Then enter the following substitution commnad.
  • %s/\(.*\)\t(.*\)/\2\t\1/g
  • That’s it you have swapped two columns.
  • Now just save the file and quit vim using :wq

There is a video of the same below