Search code examples
rubyvimcode-formatting

Format Ruby code in Vim


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

Solution

  • 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