Pragmatic Development Notes


Emacs On Windows – Ruby & Ruby On Rails

Filed under: Emacs,Ruby & RubyOnRails — Boško Ivanišević @ 21:48
Tags: , , , ,

This is the last part of this small series about customizing Emacs. In first two parts I’ve wrote about basic customizations and adding various extensions. In this one I’m focusing on adjusting Emacs for Ruby and Ruby On Rails development. As usual, in order to do that we must get appropriate Lisp files.

On Windows systems they come with One-Click Ruby Installer. After installing it, all files needed for Ruby support in Emacs can be found in %RUBY_INSTALL_DIR%\doc\ruby\ruby-1.8.6\misc\ where %RUBY_INSTALL_DIR% is directory where you installed Ruby. If you are on Linux you can find necessary files directly in Ruby repository in trunk/misc folder (that’s how I did it on my Ubuntu 8.10 box). Copy ruby-electric.el, ruby-mode.el and rubydb3x.el to your includes folder. You can also copy rdebug.el but I’m using newer and better version so if you want to have really good Ruby debugging support wait just a little more, but only if you are using Emacs 22 and later.

Now it’s time to change .emacs file for Ruby support. First we will set up automatic activation of Ruby mode whenever any Ruby file, and HTML mode whenever any of Ruby On Rails (old or new) files are opened:

;; Loads ruby mode when a .rb file is opened.
(autoload 'ruby-mode "ruby-mode" "Major mode for editing ruby scripts." t)
(setq auto-mode-alist (cons '(".rb$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist (cons '(".rhtml$" . html-mode) auto-mode-alist))
(setq auto-mode-alist (cons '(".html.erb$" . html-mode) auto-mode-alist))

Inserting closing parenthesis, 'end' keyword after 'def' etc. are cool features of lots of modern IDEs. Some developers like it, others not. If you are in the first group now it is time to enable it. For that purpose we copied electric.el file to our includes folder. I like ruby electric although there is one glitch in it I would like to be changed. After it inserts closing parenthesis if you type ‘)’ cursor will not be moved after it, but new ‘)’ character will be added. It would be much better if it moves cursor after inserted closing parenthesis. Anyway if you want to enable this feature use this part of Lisp code I’ve found on Stephen O’Donnell’s Software bits and pieces blog. By the way he has excellent posts about Emacs and Ruby.

(add-hook 'ruby-mode-hook
(add-hook 'local-write-file-hooks
(untabify (point-min) (point-max))
(set (make-local-variable 'indent-tabs-mode) 'nil)
(set (make-local-variable 'tab-width) 2)
(imenu-add-to-menubar "IMENU")
(define-key ruby-mode-map "\C-m" 'newline-and-indent)
(require 'ruby-electric)
(ruby-electric-mode t)

Finally we came to the debugging part. As I mentioned earlier you can save rdebug.el from Ruby installation on Windows or from repository on Linux to includes folder and this is all you need if you are using earlier version of Emacs. But if you have Emacs 22 it is much better to download Lisp files directly from ruby-debug Subversion repository:

svn checkout svn://

Copy all lisp files to emacs-rdebug directory in plugins folder, and add following to your .emacs file:

;; Ruby debugging.
(add-to-list 'load-path "~/.emacs.d/plugins/emacs-rdebug")
(autoload 'rdebug "rdebug" "Ruby debugging support." t)
(global-set-key [f9] 'gud-step)
(global-set-key [f10] 'gud-next)
(global-set-key [f11] 'gud-cont)
(global-set-key "\C-c\C-d" 'rdebug)

Here is a screen shot of Emacs ruby debugging in action. As you can see Emacs really turns into IDE like application with this extension.

Ruby debugging in Emacs

Ruby debugging in Emacs

But what’s an IDE without integrated help for the programming language you’re working in? Of course you can go to the Web or read documentation locally but isn’t it better to have it in our favorite editor? Download ri-emacs, unpack it into includes folder and update your .emacs file:

;; ri emacs
(setq ri-ruby-script "~/.emacs.d/includes/ri-emacs.rb")
(autoload 'ri "~/.emacs.d/includes/ri-ruby.el" nil t)
(add-hook 'ruby-mode-hook (lambda ()
(local-set-key [f1] 'ri)
(local-set-key "\M-\C-i" 'ri-ruby-complete-symbol)
(local-set-key [f4] 'ri-ruby-show-args)

Above code bounds help to F1 key and adds shortcuts for completion based on ri documentation.

Packages snippet.el and finde-recursive.el are required for rails mode. Save downloaded files into includes folder and turn on their loading in Emacs.

(require 'snippet)
(require 'find-recursive)

Finally download emacs-rails. Unpack files to plugins folder and change .emacs file so Emacs switches to RoR minor automatically. Just be careful to add this after ECB is loaded. Otherwise Emacs will load incompatible speedbar.

(add-to-list 'load-path "~/.emacs.d/plugins/emacs-rails")
(require 'rails)

When you work with Ruby On Rails YAML minor mode can come in handy:

(require 'yaml-mode)
(add-to-list 'auto-mode-alist '("\\.yml$" . yaml-mode))
(add-hook 'yaml-mode-hook
'(lambda ()
(define-key yaml-mode-map "\C-m" 'newline-and-indent)))

When I work on Ruby or RoR project I don’t like to leave editor just to execute script and test the code. That’s why I added Mode compile to the list of my extensions:

(autoload 'mode-compile "mode-compile"
"Command to compile current buffer file based on the major mode" t)
(global-set-key "\C-cc" 'mode-compile)
(autoload 'mode-compile-kill "mode-compile"
"Command to kill a compilation launched by `mode-compile'" t)
(global-set-key "\C-ck" 'mode-compile-kill)

MMM mode will enhance syntax coloring when you work on .erb files:

(add-to-list 'load-path "~/.emacs.d/plugins/mmm-mode-0.4.8")
(require 'mmm-mode)
(require 'mmm-auto)
(setq mmm-global-mode 'maybe)
(setq mmm-submode-decoration-level 2)
(set-face-background 'mmm-output-submode-face "#00688b")
(set-face-background 'mmm-code-submode-face "#104e8b")
(set-face-background 'mmm-comment-submode-face "DarkOliveGreen")
:submode ruby-mode
:match-face (("<%#" . mmm-comment-submode-face)
("<%=" . mmm-output-submode-face)
("<%" . mmm-code-submode-face))
:front ""
:insert ((?% erb-code nil @ "" @)
(?# erb-comment nil @ "" @)
(?= erb-expression nil @ "" @)))))

(add-hook 'html-mode-hook
(lambda ()
(local-set-key [f8] 'mmm-parse-buffer)
(setq mmm-classes '(erb-code))

Finally load speedbar and add files for Git support (if you use this wonderful SCM) which come with Git distribution. Just copy git.el and git-blame.el to includes folder.

(autoload 'speedbar "speedbar")

(require 'git)
(autoload 'git-blame-mode "git-blame"
"Minor mode for incremental blame for Git." t)

At the end I like to print out time needed for my .emacs file to be loaded:

(message "My .emacs loaded in %ds" (destructuring-bind (hi lo ms) (current-time)
(- (+ hi lo) (+ (first *emacs-load-start*) (second *emacs-load-start*)))))

The end. With this setup Emacs turns into powerful tool for Ruby and Ruby On Rails development. Give it a try and I'm sure after few days needed to get used to Emacs commands you will hardly leave it for some IDE or other editor. I didn't.


  1. Thank you!

    Comment by Ted Beatie — 2009-04-15 @ 3:24

  2. suse linux commands…

    […]Emacs On Windows – Ruby & Ruby On Rails « Pragmatic Development Notes[…]…

    Trackback by suse linux commands — 2012-05-21 @ 7:05

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at