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

How do I align text around a character like = or : using vim-easy-align?

Answer

gaip=

Explanation

vim-easy-align is a plugin by Junegunn Choi that makes aligning text around delimiters effortless. It is particularly useful when formatting variable assignments, table data, or any structured text where columns should line up.

How it works

The plugin provides the ga operator in normal and visual mode (when mapped). The operator works like any Vim operator: you follow it with a motion or text object to define the range, then specify the delimiter character to align around.

The key sequence gaip= breaks down as follows: ga triggers EasyAlign, ip selects the inner paragraph as the text range, and = specifies the alignment delimiter. The plugin will find all = signs in the selected lines and add padding so they line up vertically.

You can align around many different characters including =, :, ,, |, and spaces. Adding a number before the delimiter aligns around the Nth occurrence. For example, gaip2= aligns around the second = sign on each line.

Example

Given this unaligned code:

name = "Alice"
age = 30
favorite_color = "blue"
x = 10

Place your cursor inside the block and type gaip=. The result becomes:

name           = "Alice"
age            = 30
favorite_color = "blue"
x              = 10

To set up the plugin, add to your .vimrc:

Plug 'junegunn/vim-easy-align'
nmap ga <Plug>(EasyAlign)
xmap ga <Plug>(EasyAlign)

You can also use it in visual mode: select lines with V, then press ga= to align the selection. For interactive mode, press Enter before the delimiter to cycle through left, right, and center alignment.

Next

How do you yank a single word into a named register?