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

How do I align text around a delimiter character using vim-lion?

Answer

gl{motion}{char}

Explanation

vim-lion (by Tom McDonald) adds gl and gL as alignment operators. Like y (yank) or d (delete), they take a motion or text object, then a delimiter character, and align all matching delimiters within that range to the same column — turning ragged code into neatly formatted tables.

How it works

  • gl{motion}{char} — align lines covered by {motion} around {char}, padding to the left
  • gL{motion}{char} — same, but pads to the right of the alignment character
  • {motion} can be any Vim motion or text object: ip (inner paragraph), 5j (5 lines down), G (to end of file), visual selection, etc.
  • {char} is the character to align on: =, :, ,, |, etc.

Install with: Plug 'tommcdo/vim-lion'

Example

Before (ragged assignments):

const foo = 1;
const longName = 2;
const x = 3;

With cursor on the first line, glip= aligns the inner paragraph on =:

const foo      = 1;
const longName = 2;
const x        = 3;

Tips

  • glip| aligns a Markdown table around | pipe characters — great for hand-editing .md files
  • gLip= right-aligns: the = signs end up at the same column, with values aligned to the right
  • For a visual selection, enter Visual mode, select the lines, then press gl=
  • vim-lion respects the b:lion_squeeze_spaces option (set to 1) to collapse multiple spaces before aligning
  • Unlike some alignment plugins, vim-lion uses native Vim operators — it integrates naturally with . (repeat) and works with count prefixes

Next

How do I run a search and replace only within a visually selected region?