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

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 mode
  • right — the Ex alignment command
  • {width} — the column width to align to (defaults to textwidth if 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 of textwidth (default 0 = 80).
  • Combine with :left and :center to 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.

Next

How do I add a per-window title bar showing the current file name in Neovim?