If I have a text file like this:

alpha: 1
beta: 2
gamma: 3
delta: 4
epsilon: 5

Is there a way to align the numbers like so?:

alpha:   1
beta:    2
gamma:   3
delta:   4
epsilon: 5

You can do this with the vim-lion plugin.

Install it, then place your cursor on the first line. In normal mode, type glip/. You'll be prompted to type in a pattern, so just type \d to match any digit, and press Enter. It will line up the numbers for you.

  • 3
    Actually, this can be done with the gL operator defined by lion.vim. This operator performs alignment by adding spaces to the right of the alignment character (instead of the left, as with the gl operator). The full command would then be gLip:. – tommcdo Feb 7 '15 at 21:53

Here's a method not using any plugin. The strategy is to use tabs as a temporary column delimiter, then reformat the tabs as spaces.

  1. Start by setting :set noexpandtab (noet for short)
  2. Type the text, using the Tab key to insert a horizontal tab character after each colon. It will probably look slightly wrong, like this:

    alpha:  1
    beta:   2
    gamma:  3
    delta:  4
    epsilon:        5
  3. :set tabstop=9 (:set ts=9 for short) makes it look as desired:

    alpha:   1
    beta:    2
    gamma:   3
    delta:   4
    epsilon: 5

    However, the whitespace is actually a horizontal tab character, not spaces.

  4. To indicate that you want spaces instead of tabs, :set et. Then, :%retab to perform the tabs-to-spaces conversion.
  • 1
    This is less than ideal, because this just changes how tabs are visualized in vim, if someone else were to open the file in a different editor it wouldn't look as good. – Dhruva Sagar Feb 8 '15 at 5:49
  • 3
    @DhruvaSagar How so? The result is exactly identical after the tabs-to-spaces conversion in step 4. – 200_success Feb 8 '15 at 6:41
  • Different editors visualize / deal with tabs differently. – Dhruva Sagar Feb 8 '15 at 12:34
  • hmm ok, I guess I overlooked step #4. Sorry for that, my mistake. – Dhruva Sagar Feb 8 '15 at 14:58

Here's another method that doesn't use any plugins.

First replace the spaces with much bigger spaces. No need to be precise: just mash the Space key a bunch of times:

:%s/ /        /

Now trim them all to be the same length:

:%norm! 10|dw

The only tricky part is knowing what count to use. For this example it's fairly easy, The longest line contains a 7 letter word: epsilon plus 2 more characters (a colon and a space), so you need to use a count of 9 + 1.

For more complicated alignments you might benefit by having your current cursor position displayed in your statusline (or by using something like getcurpos()).

Alternatively, you can just position the cursor manually on each line and use normal-mode dw by hand.

You can also use this technique in a macro:

qqf:a    <Esc>10|dw+q4@q
        └─ Mash space bar here

You can try this plugin: vim-easy-align.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.