How to re-run vim auto-indentation on a tex file

editorslatexvim

I have a LaTeX source file with its indentation messed up.
I am looking for a way to force vim (may be through one of vim-latex-suite commands) to re-run the automatic indentation commands over the whole file once again.

I can easily get rid of the messed up indentation by eating up all the white spaces in the beginning of each line with a simple regex :%s/^\s\+//. My problem is how to re-run the automatic indentation commands over the whole file.

( I am basically looking for something like the smart indent in MATLAB Editor or some other text editor, which can re-indent already existing text).

Best Answer

Method #1: vim

I believe you can do what you want with the following keyboard commands in vim, as follows.

NOTE: =, the indent command can take motions.

So:

  • gg to get the start of the file
  • = to indent
  • G to the end of the file

Putting it all together: gg=G.

Method #2: without vim

This isn't a vim solution but I came across this Perl script titled LaTeXTidy.pl which might be more useful if you have multiple files you have to do this with.

The original script and a copy on pastebin:

Example

To run it you'll need to make it executable after downloading it and then just run it passing it the name of a latex file.

download

$ curl -o latextidy.pl http://bfc.sfsu.edu/LaTeXTidy-0.31.pl
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  4755  100  4755    0     0   1201      0  0:00:03  0:00:03 --:--:--  1334

permissions and running

$ chmod +x latextidy.pl
$ ./latextidy.pl <some_latex_file.tex>
Related Question