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

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 within width columns (default: textwidth or 80)
  • :'<,'>right [width] — right-justifies each selected line to width columns
  • :'<,'>left [margin] — left-justifies each selected line, with an optional margin indent

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: :%center centers every line in the file
  • Combine with :'<,'>left 4 to add a 4-space indent to selected lines (useful for indenting code blocks in Markdown)
  • For ASCII art or banner text, :center quickly aligns headings without manual space counting
  • See :help :center, :help :right, and :help :left for full details

Next

How do I mark and delete multiple files at once from within Vim's built-in file explorer netrw?