sponsor Vim development Vim logo Vim Book Ad

basic Tip #139: Alignment: =, LaTeX tables, declarations, etc

 tip karma   Rating 59/25, Viewed by 15871 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   October 16, 2001 8:05      complexity:   basic
author:   Charles E. Campbell, Jr.      as of Vim:   5.7

Check out

http://www.erols.com/astronaut/vim/textab.html

and see some examples of text alignment (its hopeless to do it here with
proportional fonts).  You'll be able to download textab source, a Windows-based
textab executable, and a scriptfile containing a convenient interface (ttalign.vim).  The textab program coupled with <ttalign.vim> lets you:

1. align C language statements on their = += -= /= etc symbols
2. align C language declararations: separate columns for types, *[, variable
   names, initializations (=), and comments (// or /* .. */)
3. align C/C++ language comments (//, /* .. */)
4. align C/C++ language (ansi) function argument lists
5. align LaTeX tables on their && separators
6. align HTML tables with </TD><TD> separators
7. align on several characters: < ? : | @ ;  (or modify them to handle whatever
   alignment characters you want)

 rate this tip  Life Changing Helpful Unfulfilling 

<< Getting name of the function | tip using embedded perl interpreter >>

Additional Notes

[email protected], May 7, 2002 11:14
Textab and <ttalign.vim> have been superceded by
<Align.vim> and <AlignMaps.vim>.  Thus alignment
is now done completely by Vim scripts, so no more
compiling and worrying with filter options.  The
new Align() function is more powerful, too:

   * cyclic left, right, and centering justification
   * cyclic or equivalent multiple regular-expression separators
   * improved initial white space control
   * specifiable separator padding
   * visual range specified alignment, including via visual-block
   * a <Align.txt> help file is now available

These new tools are available at the same page indicated above.
[email protected] - NOSPAM, November 4, 2004 6:12
My website has changed addresses; the examples are now given at:

    http://mysite.verizon.net/astronaut/vim/textab.html
Anonymous, December 2, 2004 14:16
I remember Peppe had a simple "justify.vim" for text files that used to be bundled in older (ie., 6+ but 6.2-) versions of vim.  Where can I find that?
[email protected] - NOSPAM, January 6, 2005 5:56
Check on vimscript#177 -- that's Gergely  Kontra's MyJustify.vim.
I also found Peppe's script at http://rastasoft.org/configs/justify.vim
Align.vim isn't a textwidth justification script, as no doubt you've noticed.
[email protected], March 22, 2005 16:53
Hi,

I followed Charles Campbell instructions and downloaded Align.tar.gz file.
After untarring it I copied AlignMaps.vim and Align.vim and Align.txt files
into my plugin and doc directories:
/usr/local/share/vim/vim63/doc
/usr/local/share/vim/vim63/plugin

I restarted vim and tried to use Align command.
Here is my text in vim6.3
123 = rwer
123  = rwer
123    = rwer

:Align=

The reply from vim was:
E492: Not an editor command: Align=  

Do you know what could be the problem?

Thank you,

Jacob Nikom
Anonymous, April 16, 2005 15:32
I don't think you should include cecutil.vim with plugins, since cecutil is available separately. It could lead to someone installing a newer cecutil, and overwriting it with whatever version is in the latest Align package.
[email protected] - NOSPAM, August 9, 2005 11:04
I've altered the name of the examples page on my website; the examples are now given at:

    http://mysite.verizon.net/astronaut/vim/align.html
[email protected], August 10, 2005 21:57
For # style comments (TCL, perl, shell) , the following maps "\a#" to align them based on the rightmost "#".  It also removes extraneous whitespace after the "#".


map <silent> <Leader>a# \WS:'a,.s/#\([ \t]*\)\(.*\)$/###@@# \2/e<CR>'zk<Leader>tldW@:'y,'zs/^\(\s*\) @@/\1/e<CR>:'y,'zs/ @@ //eg<CR>:'y,'zs/###//eg<CR>\WE
vmap <silent> <Leader>a#    :<BS><BS><BS><CR>ma'><Leader>a#


It uses special character sequences "@@" and "###" so if these occur naturally in your code, globally replace them with something less common.  

Enjoy!

Tom
[email protected], October 7, 2005 11:23
I really don't like breaking out Excel for every little table that I make.  So I wanted a simple way to make
column aligned text.  Align was it.

I did not want visible delimiters in my little tables, so I made a small addition to the AlignMaps.vim file that I have found quite useful.  I added a new macro that treats two or more spaces as a delimiter for column definition.  So I can just highlight text like this:

_who_  _what_  _where_
Fred  ate  kitchen
Joe  read a boring detective novel  bathroom

then I highlight and invoke macro (bound to a key of course) and get:

_who_  _what_                         _where_
Fred   ate                            kitchen
Joe    read a boring detective novel  bathroom

The two or more spaces idea means that you can use spaces in column text.

Anyway, the modified macro really just a hack to the existing Align 'tsp' macro:

map <silent> <Leader>tss  \WS:'a,.s/^\(\s*\)\(.*\)/\=submatch(1).substitute(submatch(2),'\s\s\+','@','g')/<CR>:AlignCtrl mIp0P0=l @<CR>:'a,.Align<CR>:'y+1,'z-1s/@/  /g<CR>\WE

May be that there is a more elegant way to get the same result, but I'm pretty happy with the way this one works... I use it all the time.  

Long live fixed width fonts!
[email protected], December 31, 2005 6:33
A pitty... This doesn't work for me. Just nothing happens
(I have the last Align plugin).

L.
sussycadillac at ig..., January 1, 2006 12:24
I made cranq map work, by changing a little bit the map, and by
adding to AlignMaps.vim (what I don't like to do because of updating)
L.
[email protected] - NOSPAM, January 5, 2006 13:15
I agree that there are advantages in keeping cecutil separate from Align (and other plugins, too).  Unfortunately, that means that a large number of potential users would get immediately frustrated.  I've been including cecutil with the plugins because of feedback.

If the Align/AlignMaps plugins don't work for you, or you're otherwise having problems, I suggest sending a message about it to me!  Often its a matter of some settings the user has/doesn't have set that I don't/do have set.  Its helpful to include a copy of your .vimrc so I can figure out what settings are involved.  Please also tell me what o/s you're using, and whether you're using vim or gvim.  I generally can't fix it if I can't replicate the problem.
[email protected] - NOSPAM, January 5, 2006 13:38
About \WS and \WE -- I've made :call AlignWrapperStart() and :call AlignWrapperEnd() globally available.  Calls to these functions can be made from outside the AlignMaps.vim script (v33).  For the \tss map:

map <silent> <Leader>tss  :call AlignWrapperStart()<cr>
  \:'a,.s/^\(\s*\)\(.*\)/\=submatch(1).substitute(submatch(2),'\s\s\+','@','g')/<cr>
  \:AlignCtrl mIp0P0=l @<cr>
  \:'a,.Align<cr>
  \:silent 'y+1,'z-1s/@/  /g<cr>
  \:call AlignWrapperEnd()<cr>
cbeltran, August 3, 2006 7:19
Hi Charles,

Using the align I would like to align Doxygen comments using \acom
However, in the following doxygen comments:

int x; /// my first variable
int y;    /// my second variable

using \acom produces the next code:

int x: // / my first variable
int y; // / my second variable

Notice the space between the first two // and the third /
Thanks,
Carlos.
ethan dot mallove (no spam please) at sun dot com, December 14, 2006 8:10
When Align gives me the following:

define('GRAY'    , "#A0A0A0");
define('DGRAY'   , "#808080");
define('LGRAY'   , "#C0C0C0");

Sometimes I would rather have:

define('GRAY',     "#A0A0A0");
define('DGRAY',    "#808080");
define('LGRAY',    "#C0C0C0");

(Note the space *after* the separator.)

Right now I select the text and do:

:'<,'>g/./norm Elr,f,r

I thought maybe this AlignCtrl setting would give me the behavior I'm looking for, but no luck.

:cal AlignCtrl('<')

Is there an an AlignCtrl command for what I want?

Thanks,
Ethan
[email protected] - NOSPAM, January 9, 2007 6:35
Look at:  :help alignmaps  -- the AlignMaps includes a bunch of maps which do various jobs which depend upon Align.  In particular, look at   :help alignmap-ts,  -- hope that helps!
Chip
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo