How do I right-align visually selected lines to a specific column width?
Answer
:'<,'>right {width}
Explanation
The :right Ex command right-aligns lines within a given column width by padding them with spaces on the left. When prefixed with the visual range '<,'>, it operates only on the selected lines — making it easy to right-align a block of text without touching the rest of the file.
How it works
:'<,'>— the visual selection range, inserted automatically when you press:in visual moderight— the Ex alignment command{width}— the column width to align to (defaults totextwidthif omitted)
The command strips leading whitespace from each line and then re-pads it so the line's last non-blank character falls on the specified column.
Example
Given this selection:
foo
bar
baz
Running :'<,'>right 20 produces:
foo
bar
baz
Tips
- Omitting
{width}uses the value oftextwidth(default 0 = 80). - Combine with
:leftand:centerto format multi-column text layouts::'<,'>left— left-align (strip leading whitespace):'<,'>center {width}— center lines within the given width
- These commands are part of Vim's built-in formatting suite; no plugins required.