December 10, 2009

use vim to auto format your code/document

Load your code/document, and make sure the filetype is what you what. (use :set to see what filetype is set to).

Turn indent on, and then do "gg=G".

For example, to autoformat a xml file.

1) :set filetype=xml
2) :filetype indent on
3) gg=G

8 comments:

  1. Great, thanks!

    ReplyDelete
  2. Thanks a lot

    ReplyDelete
  3. I wouldn't use the word "autoformat", it does not format, "=" autoindents.

    ReplyDelete
  4. Thanks really much:)

    ReplyDelete
  5. This was very helpful. I also wanted to first put each XML tag on its own line because they were all on one line. I did that first with this:

    :%s/></>\r</g

    Then I used your command to automatically indent everything.

    ReplyDelete
  6. Have a look at https://github.com/Chiel92/vim-autoformat

    This vim plugin allows you to not only reindent your code, but also to reformat it.

    ReplyDelete
  7. Thanks! That's really handy.

    ReplyDelete