# I need a code beautifier or formatter

**URL:** <https://rubytalk.org/t/i-need-a-code-beautifier-or-formatter/6382>\
**Category:** ruby-talk\
**Created:** [14 June 2003 14:33 UTC](https://rubytalk.org/t/i-need-a-code-beautifier-or-formatter/6382 "2003-06-14T14:33:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joao\_Pedrosa1](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@Joao\_Pedrosa1](https://rubytalk.org/u/Joao_Pedrosa1)\
**Post date:** [14 June 2003 14:33 UTC](https://rubytalk.org/t/i-need-a-code-beautifier-or-formatter/6382/1 "2003-06-14T14:33:43Z")

</div>

Hi,

> **···**
>
> — Michael Neumann [mneumann@ntecs.de](mailto:mneumann@ntecs.de) wrote:
> 
> > On Sat, Jun 14, 2003 at 10:01:36PM +0900, Joao \> Pedrosa wrote:
> > 
> > > Hello,
> > > 
> > > I have some source code that is terribly  
> > > formatted. It needs some good  
> > > indentation at least. But I am too lazy to do it  
> > > all by hand.
> > > 
> > > Does anyone know a tool to do the job ?
> > 
> > Vim can format Ruby code (visual select then press  
> > ‘=’).
> > 
> > Regards,
> > 
> > Michael
> 
> Vim did format it, but not as expected. It doesn’t even indent  
> correctly for me. Anyone can confirm that it doesn’t work properly ?
> 
> Thanks Michael,  
> Joao

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [14 June 2003 14:51 UTC](https://rubytalk.org/t/i-need-a-code-beautifier-or-formatter/6382/2 "2003-06-14T14:51:08Z")

</div>

You can try to update your indent file:  
[http://www.soyabean.com.au/gavin/vim/index.html](http://www.soyabean.com.au/gavin/vim/index.html)

Works for me most of the time, but sometimes there’s problems w/

def foo  
class \<\< anObject; self end

# thinks def foo is closed now and colors are wrong from now on

and

bla do |x|  
#sometimes indentation fails here???  
#and ‘end’ is mistakenly aligned with the outer scope, e.g. def foo

> **···**
>
> On Sat, Jun 14, 2003 at 11:33:43PM +0900, Joao Pedrosa wrote:
> 
> > Hi,
> > 
> > — Michael Neumann [mneumann@ntecs.de](mailto:mneumann@ntecs.de) wrote:
> > 
> > > On Sat, Jun 14, 2003 at 10:01:36PM +0900, Joao \> \> Pedrosa wrote:
> > > 
> > > > Hello,
> > > > 
> > > > I have some source code that is terribly  
> > > > formatted. It needs some good  
> > > > indentation at least. But I am too lazy to do it  
> > > > all by hand.
> > > > 
> > > > Does anyone know a tool to do the job ?
> > > 
> > > Vim can format Ruby code (visual select then press  
> > > ‘=’).
> > > 
> > > Regards,
> > > 
> > > Michael
> > 
> > Vim did format it, but not as expected. It doesn’t even indent  
> > correctly for me. Anyone can confirm that it doesn’t work properly ?
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> Are Linux users lemmings collectively jumping off of the cliff of  
> reliable, well-engineered commercial software?  
> – Matt Welsh

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@Simon\_Strandgaard](https://rubytalk.org/u/Simon_Strandgaard)\
**Post date:** [14 June 2003 15:03 UTC](https://rubytalk.org/t/i-need-a-code-beautifier-or-formatter/6382/3 "2003-06-14T15:03:27Z")

</div>

I tried the ‘=’ in VIM and the indent then got screwed up…

Michael, How does your .vimrc look ?

> **···**
>
> On Sun, 15 Jun 2003 00:33:43 +0900, Joao Pedrosa wrote:
> 
> > — Michael Neumann [mneumann@ntecs.de](mailto:mneumann@ntecs.de) wrote:
> > 
> > > Vim can format Ruby code (visual select then press  
> > > ‘=’).
> > 
> > Vim did format it, but not as expected. It doesn’t even indent  
> > correctly for me. Anyone can confirm that it doesn’t work properly ?
> 
> –  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Brett\_Williams1](https://avatars.discourse-cdn.com/v4/letter/b/7993a0/32.png) [@Brett\_Williams1](https://rubytalk.org/u/Brett_Williams1)\
**Post date:** [15 June 2003 18:29 UTC](https://rubytalk.org/t/i-need-a-code-beautifier-or-formatter/6382/4 "2003-06-15T18:29:21Z")

</div>

“Joao Pedrosa” [joaopedrosa@myrealbox.com](mailto:joaopedrosa@myrealbox.com) wrote in message  
news:1055601220.9254.8.camel@localhost.localdomain…

> Hi,
> 
> > 
> 
> Vim did format it, but not as expected. It doesn’t even indent  
> correctly for me. Anyone can confirm that it doesn’t work properly ?
> 
> Thanks Michael,  
> Joao

I use vim for my ruby coding. I set smartindent on as well–not sure how  
much difference that makes for ruby. It mostly does the right thing, but…

The problem is that it will end up completely in the weeds after it gets one  
line wrong (since vim indents seem to be very much based on what the  
previous line was). I’ve found I can’t indent too many lines at once  
without this happening. So I don’t find I can use gg=G without being  
unhappy.

Some things that aren’t necessarily ‘wrong’, but aren’t how I like them:

foo = "someverylongstring and some other "+  
“something else”

The “something else” just lines up with the previous line. I’d like to see  
it indented. Same goes for any non-complete line.

bar = some\_method(afjdksajk, gjfrjeje, fjdkjska,  
dfjkdsjk, ss)

I’d like for this to have the second line line up with the open paren.  
Basically, I want things to work how ruby-mode for emacs works but I want to  
use vim 🙂

There are just two things I miss from emacs now that I’ve made the switch:  
psgml-mode, and the near certainty that emacs indent will tell me when I’ve  
goofed something up and make it look just how I like it.

> xemacs -batch myprog.rb -eval ‘(indent-region (point-min) (point-max)  
> nil)’ -f save-buffer -kill

This thread is useful to me though, as I can just define a vim map that will  
run the buffer through emacs batch mode and get it how I like it 🙂 Thanks,  
Tim, for the tip!

---

<div class="post-metadata">

**Author:** ![Bj1](https://avatars.discourse-cdn.com/v4/letter/b/94ad74/32.png) [@Bj1](https://rubytalk.org/u/Bj1)\
**Post date:** [15 June 2003 22:34 UTC](https://rubytalk.org/t/i-need-a-code-beautifier-or-formatter/6382/5 "2003-06-15T22:34:32Z")

</div>

I thought about this not long ago, while working on a piece of software along  
with several others, all with their own special little way of formatting (a  
multi-language project, java, C/C++, C# and ruby mostly) trying to identify  
important changes from cvs logs was a pain in the \*\*\* after the different  
editors and different code styles messed things up. It ended up so bad  
eventually that something had to be done, forcing everyone to write in the  
same fashion would waste time and probably dampen productivity.

Everything that was to be commited to the cvs had to be passed through  
indentation programs, astyle and gnu indent covered C/C++/java but we didn’t  
find any such utilities for Ruby and C# (which had a visual impact on the  
project scheduled after analyzing progress reports).

afaik, there is no such application for Ruby (as a sidenote, any handwritten  
ruby lexer in ruby? 😉 perhaps it is time for the development of such an  
application? Personally I’d love to be able to atleast format around the  
differences in block structures ( {} compared to do … etc.), the obvious  
depth-indentation and probably a few other things I can’t seem to remember  
right now 🙂

/ Björn Ståhl

> **···**
>
> On Saturday 14 June 2003 16.33, Joao Pedrosa wrote:
> 
> > Hi,
> > 
> > — Michael Neumann [mneumann@ntecs.de](mailto:mneumann@ntecs.de) wrote:
> > 
> > > On Sat, Jun 14, 2003 at 10:01:36PM +0900, Joao \> \> \> \> Pedrosa wrote:
> > > 
> > > > Hello,
> > > > 
> > > > I have some source code that is terribly
> > > 
> > > formatted. It needs some good
> > > 
> > > > indentation at least. But I am too lazy to do it
> > > 
> > > all by hand.
> > > 
> > > > Does anyone know a tool to do the job ?
> > > 
> > > Vim can format Ruby code (visual select then press  
> > > ‘=’).
> > > 
> > > Regards,
> > > 
> > > Michael
> > 
> > Vim did format it, but not as expected. It doesn’t even indent  
> > correctly for me. Anyone can confirm that it doesn’t work properly ?
> > 
> > Thanks Michael,  
> > Joao

---

<div class="post-metadata">

**Author:** ![Nikolai\_lone-star\_We](https://avatars.discourse-cdn.com/v4/letter/n/9fc29f/32.png) [@Nikolai\_lone-star\_We](https://rubytalk.org/u/Nikolai_lone-star_We)\
**Post date:** [15 June 2003 18:46 UTC](https://rubytalk.org/t/i-need-a-code-beautifier-or-formatter/6382/6 "2003-06-15T18:46:48Z")

</div>

On Mon, 16 Jun 2003 03:29:21 +0900, Brett Williams wrote something resembling:

> “Joao Pedrosa” [joaopedrosa@myrealbox.com](mailto:joaopedrosa@myrealbox.com) wrote in message  
> news:1055601220.9254.8.camel@localhost.localdomain…
> 
> > Hi,
> > 
> > > 
> > 
> > Vim did format it, but not as expected. It doesn’t even indent  
> > correctly for me. Anyone can confirm that it doesn’t work properly ?
> > 
> > Thanks Michael,  
> > Joao
> 
> I use vim for my ruby coding. I set smartindent on as well–not sure how  
> much difference that makes for ruby. It mostly does the right thing, but…
> 
> The problem is that it will end up completely in the weeds after it gets one  
> line wrong (since vim indents seem to be very much based on what the  
> previous line was). I’ve found I can’t indent too many lines at once  
> without this happening. So I don’t find I can use gg=G without being  
> unhappy.
> 
> Some things that aren’t necessarily ‘wrong’, but aren’t how I like them:
> 
> foo = "someverylongstring and some other "+  
> “something else”
> 
> The “something else” just lines up with the previous line. I’d like to see  
> it indented. Same goes for any non-complete line.
> 
> bar = some\_method(afjdksajk, gjfrjeje, fjdkjska,  
> dfjkdsjk, ss)

if i get my mind of coding my web pages any time soon i’m going to add  
support for this to the ruby indenter. i’m not the maintainer of it  
though. the code for this already exists in the Makefile indenter,  
which i currently maintain. so stay tuned ;-).

enjoy,  
nikolai

> **···**
>
> –  
> ::: name: Nikolai Weibull :: aliases: pcp / lone-star :::  
> ::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::  
> ::: page: [www.pcppopper.org](http://www.pcppopper.org) :: fun atm: gf,lps,ruby,php,war3 :::  
> main(){printf(&linux[“\021%six\012\0”],(linux)[“have”]+“fun”-97);}

---

<div class="post-metadata">

**Author:** ![Michael\_Campbell1](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@Michael\_Campbell1](https://rubytalk.org/u/Michael_Campbell1)\
**Post date:** [15 June 2003 21:08 UTC](https://rubytalk.org/t/i-need-a-code-beautifier-or-formatter/6382/7 "2003-06-15T21:08:59Z")

</div>

> > Some things that aren’t necessarily ‘wrong’, but aren’t how I like them:
> > 
> > foo = "someverylongstring and some other "+  
> > “something else”
> > 
> > The “something else” just lines up with the previous line. I’d like to see  
> > it indented. Same goes for any non-complete line.
> > 
> > bar = some\_method(afjdksajk, gjfrjeje, fjdkjska,  
> > dfjkdsjk, ss)

FWIW, here’s how the emacs ruby-mode indents that:

foo = “something long” +  
“something else”

bar = some\_method(afjdksajk, gjfrjeje, fjdkjska,  
dfjkdsjk, ss)
