Sunday, September 2, 2007

ruby-mode for emacs

I'm not really a ruby guy, but recently I had to do some ruby hacking, which means I needed ruby-mode for emacs. Between the InterWeb, RubyGarden, and EmacsWiki, I couldn't find a clear, correct description of how to get ruby-mode. Here's my take.

Step 1: Why not upgrade emacs?

Did you know emacs finally released a new version?!

If you are using MacPorts, do this for a terminal (non-gui version). Do port variants emacs for other options.

sudo port install -v emacs

Step 2: Download the code

We are going to install our lisp code in ~/.emacs.d/site-lisp which I think is more-or-less standard, but you could use any directory.

export SITE_LISP=~/.emacs.d/site-lisp
mkdir -p $SITE_LISP
svn export http://svn.ruby-lang.org/repos/ruby/trunk/misc ${SITE_LISP}/ruby
emacs -batch -f batch-byte-compile ${SITE_LISP}/ruby

Don't forget that last step! That compiles the lisp code, and will make emacs run faster (handy if you re-indent a large file).

Step 3: Configure your .emacs

Add this to your .emacs. This is a complete rip from RubyGardens's Installing Emacs Extensions, with the exception of the first line.

;; ruby                                                                         
;; based on http://www.rubygarden.org/Ruby/page/show/InstallingEmacsExtensions  
;;                                                                              

(add-to-list 'load-path "~/.emacs.d/site-lisp/ruby")

 (autoload 'ruby-mode "ruby-mode"
     "Mode for editing ruby source files")
 (add-to-list 'auto-mode-alist '("\\.rb$" . ruby-mode))
 (add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))
 (autoload 'run-ruby "inf-ruby"
     "Run an inferior Ruby process")
 (autoload 'inf-ruby-keys "inf-ruby"
     "Set local key defs for inf-ruby in ruby-mode")
 (add-hook 'ruby-mode-hook
     '(lambda ()
         (inf-ruby-keys)))
 ;; If you have Emacs 19.2x or older, use rubydb2x                              
 (autoload 'rubydb "rubydb3x" "Ruby debugger" t)
 ;; uncomment the next line if you want syntax highlighting                     
 (add-hook 'ruby-mode-hook 'turn-on-font-lock)

Step 4: Get back to work

EOF

10 comments:

  1. Hey,

    Just wanted to thank you for posting this. I've spent all morning trying to get ruby mode to work, and this finally did it for me. Thanks!

    -Jacob

    ReplyDelete
  2. Thank you! I was surprised by how little Emacs/Ruby info was available, I believe your page is the only one that clearly tells what needs to happen to turn ruby mode on.

    ReplyDelete
  3. Thanks alot! After reading like a dozen other sites and getting nowhere, this finally worked.

    ReplyDelete
  4. Another satisfied customer. Thank you

    ReplyDelete
  5. My thanks also! This was way too easy! And now back to work with increased ruby productivity ...

    Vern

    ReplyDelete
  6. I've spent so much time looking for a decent guide. Yours worked! Thanks!

    ReplyDelete
  7. worked for me as well on openSUSE 11, thanks a lot

    ReplyDelete
  8. Thank you! This was simple and straightforward - now I've got ruby mode working in emacs on windows :-)

    ReplyDelete
  9. Thank you so much for this direct and simple set of steps. I appreciate it

    ReplyDelete