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

How do I right-align lines of text within a given column width using a built-in Vim command?

Answer

:[range]right [width]

Explanation

Vim's :right command right-aligns text by padding lines with leading spaces up to a given width. It's part of a trio of built-in alignment commands — :left, :center, and :right — that let you align text without needing a plugin.

How it works

  • :[range]right [width] — right-aligns the lines in [range] within a field of [width] columns
  • If [width] is omitted, Vim uses 'textwidth' (or 80 if textwidth is 0)
  • :[range]left [indent] — left-aligns lines with an optional indent
  • :[range]center [width] — centers lines

Example

Given these lines:

hello
world

Running :%right 20 produces:

               hello
               world

To right-align only a visual selection, use :'<,'>right 40.

Tips

  • Ideal for formatting plain text columns, ASCII diagrams, or aligning comment blocks
  • Use with 'textwidth' already set and no argument: :%right aligns to that width automatically
  • Combine with :center for headers and :left to restore alignment: :'<,'>left 0 removes leading indent

Next

How do I prevent Vim from adding a newline at the end of a file when saving?