Vertically align a table (or something) in vim

Just a short note to myself how to align a table in vim. I found the answer here. We start with the following example, a part of a LateX table (that was generated with an openoffice plugin). But the columns ("&" characters) are not aligned vertically, which is ugly.

      C1 & c3 & 0.30593 & C1 \\ 
      C1X & c3 & 0.13418 & C1X \\ 
      C2 & c3 & 0.08862 & C2 \\ 
      C2X & c3 & -0.08100 & C2X \\ 

Of course, aligning the & characters can easily be done automagically (and this time I did not even have to write the script myself!)

  • download the vim Align plugin
  • install it. details on how to install are on the plugin's page
  • use it: select a block and type (in this case, we want to align the & character):
    :Align &
  • watch the magic
      C1   & c3 & 0.30593  & C1 \\
      C1X  & c3 & 0.13418  & C1X \\
      C2   & c3 & 0.08862  & C2 \\
      C2X  & c3 & -0.08100 & C2X \\

Actually, this was just an example. The Align package has a special command to align LaTeX tables, which also aligns the newine commands \\. Just type \tt instead of :Align &.

(And in case you want to know: the table shows AMBER ff03 atom types and partial charges of an octylglucoside molecule, as calculated by AM1-BCC. But you are probably not interested :) )

This entry was posted in latex, vim. Bookmark the permalink.

Leave a Reply

Your email address will not be published.