Vim has built-in capability to align text within the buffer:
Command | Action |
---|---|
:left | left align lines |
:center | format lines at the center |
:right | right align text |
Each command follows the signature:
:[range]{command} [width]
Where:
-
optional
range
is the range of lines to align. The default range is the current line, and can be set by visual selection. -
command
is one ofleft
,center
,right
-
optional
width
specifies the number of columns to align the content within for center and right (default the width of the window), and the number of columns of indentation for left (default 0).
These will become clear with a few simple examples, using the following buffer:
Initial buffer content
Danielle·Gonzales
Tonya·Reed
Christopher·Shaw
Tammy·Greene
Marcus·Costa
Christopher·Flores
Deanna·Delgado
Steven·Nguyen
COMMAND
Top
1:1
:1,$center
First, lets center the entire buffer within the width of the window:
Align Center
·······························Danielle·Gonzales
···································Tonya·Reed
································Christopher·Shaw
··································Tammy·Greene
··································Marcus·Costa
·······························Christopher·Flores
·································Deanna·Delgado
·································Steven·Nguyen
NORMAL
Top
1:32
Next, lets right-align the text to a width of 40 columns:
Align Right
·······················Danielle·Gonzales
······························Tonya·Reed
························Christopher·Shaw
····························Tammy·Greene
····························Marcus·Costa
······················Christopher·Flores
··························Deanna·Delgado
···························Steven·Nguyen
NORMAL
Top
1:24
Finally, lets left-align the buffer with an indentation of 4 columns:
Align Left
····Danielle·Gonzales
····Tonya·Reed
····Christopher·Shaw
····Tammy·Greene
····Marcus·Costa
····Christopher·Flores
····Deanna·Delgado
····Steven·Nguyen
NORMAL
Top
1:5