Using Emacs with Ruby-Mode and Emacs-Rails plugin
If you’re looking for a good IDE for Ruby and Ruby on Rails, Emacs may be an excelent tool for you. If you doubt it, look at the link below. It has a great Emacs screencast showing up how that plugin works.
Emacs Screencast: http://platypope.org/yada/emacs-demo/.
Did you like it? Check this post out to see how to transform your Emacs editor in an excelent IDE for Ruby and Ruby on Rails.
Installing Emacs
First of all, you must install Emacs on your local machine. In linux for instance (my OS) you simply type the following in the sell:
sudo apt-get install emacs22
After that, you must check if a directory called .emacs-d was created into ~/ folder. If not, create it yourself. To test the installation, simply type emacs in the sell. Emacs should come up on your screen.
Installing Ruby-Mode
We can install the ruby mode directly from its SVN server (you must have SVN installed, if not, try sudo apt-get install svn). Enter into ~/.emacs-d/ directory and type the following:
svn co http://svn.ruby-lang.org/repos/ruby/trunk/misc ruby-mode
When the installation finishes a new directory called ruby-mode will be created into ~/.emacs-d/ folder.
Now, create a new file called .emacs into ~/ folder. Insert the following content:
(setq load-path (cons “~/.emacs.d/ruby-mode” load-path))
(autoload ‘ruby-mode “ruby-mode” “Load ruby-mode”)
(add-hook ‘ruby-mode-hook ‘turn-on-font-lock)
Installing Emacs-Rails
Like the Ruby-Mode, let’s install emacs-rails through SVN. Go to the ~/.emacs-d/ directory and type the following:
svn checkout svn://rubyforge.org/var/svn/emacs-rails/trunk emacs-rails
A new folder called emacs-rails will be created. Also, you must download more two files. Go to the directory emacs-rails (that just was created) and type the following:
wget http://www.kazmier.com/computer/snippet.el
wget http://www.webweavertech.com/ovidiu/emacs/find-recursive.txt
mv find-recursive.txt find-recursive.el
Finally, let’s change again the ~/.emacs file and add the following lines:
(setq load-path (cons “~/.emacs.d/emacs-rails” load-path))
(defun try-complete-abbrev (old)
(if (expand-abbrev) t nil))(setq hippie-expand-try-functions-list
‘(try-complete-abbrev
try-complete-file-name
try-expand-dabbrev))(require ‘rails)
;; associate ruby-mode with .rb files
(add-to-list ‘auto-mode-alist ‘(“\.rb$” . ruby-mode));; make #! scripts executable after saving them
(add-hook ‘after-save-hook ‘executable-make-buffer-file-executable-if-script-p)(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won’t work right.
‘(rails-ws:default-server-type “webrick”))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won’t work right.
)
Installing MMM-MODE and support for HTML.ERB files
So far, we’ve got the Ruby-Mode and Emacs-Rails. Both plugin are associated to .rb files. Now let’s add support to .HTML.ERB file as well.
To do that, firstly you must install the MMM-MODE. Simply type the following
sudo apt-get install mmm-mode
After that, add the following to the ~/.emacs
(require ‘mmm-mode)
(require ‘mmm-mode)
(require ‘mmm-auto)
(setq mmm-global-mode ‘maybe)
(setq mmm-submode-decoration-level 2)
(set-face-background ‘mmm-output-submode-face “LightGrey”)
(set-face-background ‘mmm-code-submode-face “white”)
(set-face-background ‘mmm-comment-submode-face “lightgrey”)
(mmm-add-classes
‘((erb-code
:submode ruby-mode
:match-face ((“<%#” . mmm-comment-submode-face)
(“<%=” . mmm-output-submode-face)
(“<%” . mmm-code-submode-face))
:front “<%[#=]?”
:back “-?%>”
:insert ((?% erb-code nil @ “<%” @ ” ” _ ” ” @ “%>” @)
(?# erb-comment nil @ “<%#” @ ” ” _ ” ” @ “%>” @)
(?= erb-expression nil @ “<%=” @ ” ” _ ” ” @ “%>” @))
)))
(add-hook ‘html-mode-hook
(lambda ()
(setq mmm-classes ‘(erb-code))
(mmm-mode-on)))
(add-to-list ‘auto-mode-alist ‘(“\.html.erb$” . html-mode));; shortcut to reparse the buffer
(global-set-key [f8] ‘mmm-parse-buffer)
Installing ECB
ECB is a code browser for Emacs. With ECB, you can split your screen in many others to see the directory content, the methods from a certain class and so on. See an screenshot from ECB below.
To install it, simply:
sudo apt-get install ecb
Also, add to the ~/.emacs the following:
(add-to-list ‘load-path
“/usr/share/emacs22/site-lisp/ecb”)
(require ‘ecb)
If you want to see how ECB does work, open the emacs now. By default, ECB is not started (you can setup it if you want). To start it, go to menu Tools -> Start Code Browser (ECB). Done, you’re gonna see the ECB running.
Disabling Auto-Backup
I do not like the Auto-Backup from Emacs. To disable it, simply add the following to the ~/.emacs
;disable backup
(setq backup-inhibited t)
Installing Tabbar
Another script that I like is the Tabbar. With it, you are able to open multiples BARs within Emacs. It is useful when you are editing multiples files at the same time. You can download the tabbar from here: http://www3.ntu.edu.sg/home5/PG04878518/download/tabbar.el.txt. Download it, rename it to tabbar.el and move it to the ~/.emacs-d/ruby-mode. In the ~/.emacs add the following:
(require ‘tabbar)
(tabbar-mode)
(global-set-key [(control shift left)] ‘tabbar-backward)
(global-set-key [(control shift right)] ‘tabbar-forward)
It’s all done.
If you want, you can download my .emacs file here.
Shortcuts
Below there is a table with the Shortcuts that you can use into Emacs. For instance: open a Rails Controller and type the following into the method: flash[press tab], the result will be: flash[:notice] = “Text here…”.
You can check a table of the Shortcuts in this link: http://www.credmp.org/2006/11/28/ruby-on-rails-and-emacs/
The topic ends up there. I hope this topic be useful for you. Enjoy the Emacs and Ruby world
Source
I’ve used the following URLs to setup my emacs.



Brazilian guy, IT Specialist, Linux and Mac User. Work with Java/JEE and IBM Products, such as: WebSphere and DB2. Like studying Ruby, Android and IOS. Also, I like playing tennis, however I am not good enough. Write a post in this blog once a year. Follow me on twitter if you understand portuguese: @jairrillo.