Lisakov and macroworld

Align columns in vim

One may come across the table like this:

There are some utilities to align the columns in the table. But do you really need one, if you are already in vim? Have a look on the alignment process in gif animation or mp4 video.

The general idea is:

  1. Paste enough spaces between the columns, so that the leftmost element from the second column will appear to the right relatively to rightmost element from the first column. In latex files like in the video/animation, & symbol serves as a delimiter between the columns, so I use :%s/&/          &/g to precede all the ampersands with bunch of spaces. How much exactly spaces you need depends on the cell length.

  2. Select empty column in visual block mode via Ctrl + V. To move leftwards everything to the right of the selection, use <. Repeat the action as many times as you need with .. The video and the animation show this only for the first time, when second column is being aligned. It is not the shortest way, and it is shown only for better understanding of what’s going on.

  3. Once you’ve selected an empty column, instead of <....... or so you may do 20<. This method is used to align the remainder of the table in the video/animation.

I started to learn vim about 5 years ago, when someone told me that vim knows how to work with columns. Since then this ability of vim is one of my favorites. I love vim!