How do I center, right-justify, or left-justify lines of text in Vim without a plugin?
Answer
:'<,'>center / :'<,'>right / :'<,'>left
Explanation
Vim has built-in Ex commands to align text within a specified width: :center, :right, and :left. Select lines in visual mode, press :, and type the command with an optional column width to instantly format the selected text — no plugin required.
How it works
:'<,'>center [width]— centers each selected line withinwidthcolumns (default:textwidthor 80):'<,'>right [width]— right-justifies each selected line towidthcolumns:'<,'>left [margin]— left-justifies each selected line, with an optionalmarginindent
The width argument is optional; when omitted, Vim uses the value of 'textwidth' (or 80 if textwidth is 0).
Example
Given these selected lines:
hello
world
vim rocks
After :'<,'>center 20:
hello
world
vim rocks
After :'<,'>right 20:
hello
world
vim rocks
Tips
- These commands work on any range:
:%centercenters every line in the file - Combine with
:'<,'>left 4to add a 4-space indent to selected lines (useful for indenting code blocks in Markdown) - For ASCII art or banner text,
:centerquickly aligns headings without manual space counting - See
:help :center,:help :right, and:help :leftfor full details