Vim text alignment


Here’s a Vim feature you probably never new existed: The ability to align text to the left, center, or right. The text will be padded as needed to fit to the width you specify. Visually select some text and then run one of these commands with a column width parameter. Or leave the width parameter blank and Vim will use your current textwidth setting. For example:

  • center 100 – Centers the text along a 100-column width
  • center – Centers the text along a width equal to your textwidth setting, or the default 80 characters.
  • right 50 – Right-aligns the text to a 50-character width.

The left command works slightly differently, it sets the left indent to the number you specify:

  • left 20 – Left aligns the text offset at 20 characters.

When is this useful? Maybe you want to write a large banner of a comment above a tricky block of code. Or perhaps you want a centered header used in a markdown file or notes file.

Or, like us, you find yourself writing poetry in Vim. Consider this elegant verse inspired by our favorite editor. Here I use the :center command to center the poem across a 25 character width and then across the default width of 80.

How useful was this tip?

Average rating 3.7 / 5. Vote count: 383

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Andy Libby

36 Posts

Rider of bicycles. Writer of code. User of Vim.
View all posts