Simple Vim trick: sorting alphabetically

Some days I feel like I am only using Vim to a fraction of its capacity. Today I discovered a simple time-saver that I hadn’t known about before — Vim’s :sort command.

The sort function can be used to sort a whole file alphabetically or certain lines within a file. It’s perfect for CSS code, for example, where I like to alphabetize the properties.

So for something like this:

.some-class {
  padding: 5px;
  margin: 5px;
  width: 180px;
  /* ... */
  line-height: 1.2;

… I can position my cursor within the brackets and type vi{ and then :sort, and the properties are sorted alphabetically.

Speak Your Mind