DrChip's Alignment Tool for Vim
| Top of Page |
| Align and AlignCtrl Usage |
| Examples |
Being totally script-based <Align.vim> is easily ported along with Vim and is in fact more powerful than a previous tool, textab:
You may quickly get to some examples by clicking: examples.
The Align.vba.gz file includes three
files: <Align.vim>, <AlignMaps.vim>, and <Align.txt>.
The maps in <AlignMaps.vim> are maps which utilize the
Align() function provided by <Align.vim> to a specific character
or provide more sophisticated alignment operations by using substitutes and Align()
The @ character is often used by the latter maps. <Align.txt>
provides online help (typically after bringing up vim and typing ":helptags ~/.vim/doc").
The s/w is also mentioned at Vim's sourceforge website as a tip: Alignment: =, LaTeX tables, declarations, etc. and <Align.tar.gz> is available there as a script: Align.vim (as requested by several users).
Directions: Simply put <Align.vim>
and optionally <AlignMaps.vim> into your .vim/plugin directory.
Put <Align.txt> into your .vim/doc directory.
The <AlignMaps.vim> maps handle aligning on =,
LaTeX tabulars, |, :, ,,
declarations, definitions, comments, and html tables. See the <AlignMaps.vim>
file itself for comments on the various maps therein or refer to the
accompanying help file.
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 supply the map sequence. In the latter case, the maps retain mark-a, and so it is easier to apply a sequence of maps.
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:
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}
|
| Vim | You are visitor | Top |
| Last Modified Mar 01, 2007 05:18:56 PM | © 2007, Charles E Campbell, Jr. |