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 iftextwidthis 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::%rightaligns to that width automatically - Combine with
:centerfor headers and:leftto restore alignment::'<,'>left 0removes leading indent