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).


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.


  • 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:


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.


$ 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>
  • Your vim suggestion works pretty well and the perl script is really powerful. Thanks ...
    – Bichoy
    Aug 13 '13 at 22:45
  • @Bichoy - Glad to hear they were helpful.
    – slm
    Aug 13 '13 at 22:47

In vim the = key can be used to automatically fix indentation. gg=G will fix the indentation for the entire file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.