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 80centers 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
textwidthif set::centerwithout a number usestextwidth :left 4is a quick way to set indentation to exactly 4 spaces- Combine with
:gfor selective alignment::g/^#/center 80centers all headings - For column alignment of assignments or tables, use a plugin like
vim-easy-align