I'm Matthew J. Morrison.

A Passionate, professional software developer & hobbyist; Language nerd & regular user of Unix, Python, Ruby & JavaScript.

Fork me on GitHub

Align stuff in Vim

October 07, 2014

I ran into a situation today where I wanted to align some columns of text. It was some fairly minimal css that looked something like this:

.something { height: 100px; width: 100px; }
.another { position: absolute; }
.last-one { font-size: 10pt; }

I used the EasyAlign plugin to line things up on the { and } characters. After installing EasyAlign (I will let you figure that one out on your own) I added the following key mapping in my .vimrc file:

vmap <Enter> <Plug>(EasyAlign)

Now, I am able to visually select the rows that I want to align, press Enter and type a {. That will align everything to look like this:

.something { height: 100px; width: 100px; }
.another   { position: absolute; }
.last-one  { font-size: 10pt; }

Now, if I want to also align the }s I can visually select those rows again and type Enter followed by } which will produce the following:

.something { height: 100px; width: 100px; }
.another   { position: absolute;          }
.last-one  { font-size: 10pt;             }

The EasyAlign plugin is capable of a whole lot more than that, it is worth checking out.

Knowledge has to be improved, challenged, and increased constantly, or it vanishes. - Peter Drucker