Format Ruby code in Vim
Asked Answered
B

8

28

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
Bellhop answered 24/9, 2010 at 9:8 Comment(0)
N
46

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
Nichols answered 24/9, 2010 at 12:2 Comment(4)
I tried this in a ruby file and it indented all lines so that they started 8 characters from the left. It completely ignored the original indentation.Bellhop
See edit in original question for my solution to the formatting problem.Bellhop
Watched the vimcast, it's well worth a lookBellhop
Please make a difference between reformatting and reindenting. Those are two different things.Sphenic
S
17

When I see questions about Vim and reformatting and reindenting, I usually feel confusion. But it is pretty easy.

Reindenting, done with = key, is a process of shifting line indetation without inserting any line ends - no hard wrapping. Simply put, beginning columns of the selected lines can change, but the content cannot.

On the other hand, reformatting is complete rewrite of a selected piece of code. Simply put, everything is deleted and written again according to the language rules defined in Vim. Easy, huh?

The usual patern for reindentation is to go to the beginning of the file (gg), change to line selection (V), go to the end of the file (G) and perform reidentation (=).

That's indenting in vim: ggVG=

Reformatting pattern starts with the very same keys (ggVG), but instead of equal key, you do gq - reformat Vim command.

That's formatting in vim: ggVGgq

This works out-of-box in every Vim instance, even with plain text. Only when Vim does not understand the programming language you need to provide it with correct formatting rules (usually bunch of .vim files which have to go to the .vim directory structure).

Reformatting for Ruby works only when plugin vim-ruby is installed.

I had to publish this on my blog ;-) Isn't Vim cool? It is.

Sphenic answered 27/10, 2011 at 15:13 Comment(0)
T
4

Try:

gg=G

in normal mode.

Towrope answered 24/9, 2010 at 10:26 Comment(0)
S
2

If you're looking for more than just indentation, have a look at ruby-beautify. It can be integrated with vim through vim-autoformat.

Soutine answered 11/12, 2014 at 9:19 Comment(0)
U
0

ggVGgq will reformat the entire file according to the current filetype

Unknowable answered 24/9, 2010 at 9:17 Comment(3)
I guess ggVG= would do the same.Loaning
I tried ggVGqg and while the document was reformatted it was entirely wrong e.g. first line of first method definition was stuck to the end of the class definition line. I couldn't make any sense of the formatting at all.Bellhop
See edit in original question for my solution to the formatting problem.Bellhop
L
0

I released a VIM plugin that will do some more comprehensive formatting for Ruby files. In addition to indenting, it does things like remove trailing whitespace, and consistently spaces out method declarations:

vim-autoformat-rails

Lundgren answered 12/4, 2013 at 19:48 Comment(0)
B
0

For small indentation, try: =}, =)

Barbarity answered 8/12, 2013 at 18:19 Comment(0)
E
0

Someone mentioned. but I share here. so that other people can find it easily.

ggVG=
Encamp answered 2/10, 2023 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.