Top of Page |
Examples |
Align and AlignCtrl Details |
DrChip's Vim Stuff |
DrChip's Home |
ws field ws separator ws field ws separator ... ws field ws separator ws field ws separator ...
=
,
LaTeX tabulars, |
, :
, ,
,
declarations, definitions, comments, and html tables. Use the help for <AlignMaps.vim>
the various maps therein or refer to the accompanying help file (:help alignmaps).
One may select lines using visual-selection mode and then the map sequence, or one may mark the first line with "ma" and then move to the last line, then use the AlignMaps mapping command you want to use.
@
character is often used by the latter maps. <Align.txt>
provides online help (typically after bringing up vim and typing ":helptags ~/.vim/doc").
For even more information on Align and AlignMaps, see their manual.
The plugins are also available at vimscript#294, although the most recent and up-to-date versions are available here.
There's a nice demonstration of Align that nhooey made here.
The first example shows a typical usage of Align. The next example shows a typical usage of AlignCtrl. Subsequent examples show some alignment operations done using maps from <AlignMaps.vim>.
Original | AlignCtrl l | AlignCtrl r | AlignCtrl lr |
a=bb=ccc=1 ccc=a=bb=2 dd=eee=f=3 | a = bb = ccc = 1 ccc = a = bb = 2 dd = eee = f = 3 | a = bb = ccc = 1 ccc = a = bb = 2 dd = eee = f = 3 | a = bb = ccc = 1 ccc = a = bb = 2 dd = eee = f = 3 |
Alignment | l l l l | r r r r | l r l r |
AlignCtrl W= := | AlignCtrl w= := | AlignCtrl I= := | |
Original | W option | w option | I option |
(retain init ws) | (remove init ws) | (re-use first ws) | |
a := baaa caaaa := deeee ee := f | a := baaa caaaa := deeee ee := f | a := baaa caaaa := deeee ee := f | a := baaa caaaa := deeee ee := f |
Alternatively,
Original Code | After \t= | After \T= | |
a=1; ab=2; abc=3; abcd=4; abcde=5; a+=1; ab+=2; abc+=3; this_is_func(); abcd+=4; abcde+=5; a/=1; ab/=2; abc/=3; abcd/=4; abcde/=5; | a = 1; ab = 2; abc = 3; abcd = 4; abcde = 5; a += 1; ab += 2; abc += 3; this_is_func(); abcd += 4; abcde += 5; a /= 1; ab /= 2; abc /= 3; abcd /= 4; abcde /= 5; | a = 1; ab = 2; abc = 3; abcd = 4; abcde = 5; a += 1; ab += 2; abc += 3; this_is_func(); abcd += 4; abcde += 5; a /= 1; ab /= 2; abc /= 3; abcd /= 4; abcde /= 5; |
\adec re-formats declarations. It expects one declaration per line (ie. no commas), and aligns the variables, comments, and initializations. If your declarations code has comma-separated lists of variables, you need to use \a, first (which breaks up such lists of declarations into the one declaration per line format).
Original Code | After \adec | |
int a; float b; double *c=NULL; char x[5]; struct abc_str abc; struct abc_str *pabc; int a; /* a */ float b; /* b */ double *c=NULL; /* b */ char x[5]; /* x[5] */ struct abc_str abc; /* abc */ struct abc_str *pabc; /* pabc */ static int a; /* a */ static float b; /* b */ static double *c=NULL; /* b */ static char x[5]; /* x[5] */ static struct abc_str abc; /* abc */ static struct abc_str *pabc; /* pabc */ | int a; float b; double *c = NULL; char x[5]; struct abc_str abc; struct abc_str *pabc; int a; /* a */ float b; /* b */ double *c = NULL; /* b */ char x[5]; /* x[5] */ struct abc_str abc; /* abc */ struct abc_str *pabc; /* pabc */ static int a; /* a */ static float b; /* b */ static double *c = NULL; /* b */ static char x[5]; /* x[5] */ static struct abc_str abc; /* abc */ static struct abc_str *pabc; /* pabc */ |
\acom re-formats comments: it aligns both the left and right comment sides, in spite of initial tabs, etc.
Original Code | After \com | |
x= 1; /* this is */ if(x == y) {/* an example */ x+= 2; /* of a completely useless */ y-= 3;/* set of comments */ } | x= 1; /* this is */ if(x == y) { /* an example */ x+= 2; /* of a completely useless */ y-= 3; /* set of comments */ } |
\tt re-formats LaTeX tables.
Original Code | After \tt | |
\begin{center}\begin{tabular}{||l|l|l||} \hline\hline \multicolumn{1}{||c|}{Column 1} & \multicolumn{1}{c|}{Column 2} & \multicolumn{1}{c||}{Column 3} \\ \hline a & bc & def \\ def & bc & a \\ \hline a & bc & def \\ def & bc & a \\ \hline\hline \end{tabular}\end{center} | \begin{center}\begin{tabular}{||l|l|l||} \hline\hline \multicolumn{1}{||c|}{Column 1} & \multicolumn{1}{c|}{Column 2} & \multicolumn{1}{c||}{Column 3} \\ \hline a & bc & def \\ def & bc & a \\ \hline a & bc & def \\ def & bc & a \\ \hline\hline \end{tabular}\end{center} |
The <Align.vim> script contains two commands: AlignCtrl and Align.
AlignCtrl | =ClrcpPIWw | ..list-of-separators... |
= | all separator patterns are equivalent and | |
active simultaneously. Patterns are regexp | ||
C | Cycle through separator patterns (ie. patterns | |
are active sequentially) | ||
l | left justify field | |
r | right justify field | |
c | center field | |
p# | pad separator on its left by # blanks | |
P# | pad separator on its right by # blanks | |
I | preserve and apply first line's leading white | |
space to all aligned lines | ||
W | preserve leading white space on every line, | |
even if it varies from line to line | ||
w | don't preserve leading white space | |
AlignCtrl | ||
Display current alignment settings | ||
[range]Align | [..list-sep..] | |
Aligns text over the given range. The range | ||
may be selected via visual selection or by | ||
any vim-style range from the command line. | ||
If no list of separators is provided, the last | ||
list provided either to Align or via AlignCtrl | ||
will be used. |
The first argument to AlignCtrl is a string which may contain one or more alignment control commands. Most of the commands are single-letter commands; the p# and P# commands interpret the number for padding about the separator.
The typical line is considered to be composed of two or more fields separated by some pattern:
where "ws" stands for "white space" such as blanks and/or tabs.
The Align() function will first convert tabs over the region into spaces and then apply alignment control. Except for the initial white space, white space surrounding the fields is ignored. One has three options for handling initial white space:
--l: | skip first two instances of the separator |
left justify on the separator | |
treat rest of line as a field |
AlignCtrl, when called with no arguments, will display the current alignment control settings. A typical display is shown below:
Once the alignment control has been determined, the user specifies a range of lines for the Align command/function to do its thing. Alignment is usually done on a line basis, but one may restrict alignment to a visual block using ctrl-v. For any visual mode, one types the colon (:) and then "Align". One may also specify a line range: