vimtricks.wiki Concise Vim tricks, one at a time.

How do I center or right-align text to a specific width in Vim?

Answer

:center 80

Explanation

Vim has built-in text alignment commands that adjust lines relative to a specified width. :center, :right, and :left reformat text with appropriate padding — useful for headers, documentation, and formatted output.

How it works

  • :center {width} — center text within the specified width
  • :right {width} — right-align text to the specified width
  • :left {indent} — left-align with the specified indent margin
  • All work with ranges: :'<,'>center 80 centers selected lines

Example

:center 60
Before:
Title of Document

After :center 60:
                     Title of Document

Before:
Right aligned text

After :right 60:
                                          Right aligned text

Tips

  • Use textwidth if set: :center without a number uses textwidth
  • :left 4 is a quick way to set indentation to exactly 4 spaces
  • Combine with :g for selective alignment: :g/^#/center 80 centers all headings
  • For column alignment of assignments or tables, use a plugin like vim-easy-align

Next

How do I return to normal mode from absolutely any mode in Vim?