Format Ruby code in Vim

opsb picture opsb · Sep 24, 2010 · Viewed 11.4k times · Source

Just moving over to Vim at the moment. In TextMate I could format code by hitting Cmd-Alt-[. How do I achieve the same in Vim?


See the answer below for the command. I found I also needed the following in my .vimrc so that Vim knew how to autoindent Ruby.

if has("autocmd")
  filetype indent on
endif

Answer

Codebeef picture Codebeef · Sep 24, 2010

Vimcasts has a useful screencast on this subject that you may be interested in

gg=G

gg => go to start of file
=  => apply autoformatting
G  => ... to the end of file