Emacs and Rails

July 3, 2007 at 4:56 pm 6 comments

This tutorial is a bit out dated now.  I have created a new version of it over on my new blog which is an updated version of this one.

In the last article, I described how to get Ruby syntax highlighting, electric mode and some useful techniques to run code you are editing. This time I am going to describe how to install a full blown Rails development environment, that includes code snippets, and much more. Note that the Emacs-rails package requires Emacs 22 or greater.

What you need

If you have been following along with my setup, copy snippet.el and find-recursive.el into your include directory (or somewhere on your emails load path). Extract the Emacs-rails tar file into the include directory too. That should create a new directory, emacs-rails.

Now for the necessary changes to the _emacs file:


; needed for rails mode
(require 'snippet)
(require 'find-recursive)
; The rails require needs to go after ECB
; otherwise it loads a new incompatible speedbar
(add-to-list 'load-path "C:/emacs-22.1/includes/emacs-rails")
(require 'rails)

One other thing to note – I needed to perform these includes in the _emacs AFTER my code to setup ECB. Emacs-rails seems to include a different version of the Speedbar, which breaks ECB.

What can emacs-rails do?

I have found the documentation to be a bit sketchy, but there is a good summary of the various snippets and some of the other commands available.

In my setup, rails-minor-mode started automatically when I browsed into one of my Rails projects using ECB. If for some reason it doesn’t start, you can start it with ALT-x rails-minor-mode.

Since I am using ECB, I don’t envisage using the commands to jump to models and controllers much but I will try them out and see how it goes!

The snippets are very useful. If you are running emacs with toolbars enabled (the default) a new snippets menu will appear when in Rails mode, which you can use to insert them. Better is to learn the short codes for the snippets you use often. For example, typing:


flash

and hitting the tab key will result in the following appearing:


flash[:notice] = 'Successfully'

Each of the placeholder fields are highlighted, so you can hit TAB to get into each of them and just start typing to replace them – its easier to play with than to explain!

There are a host of other menu options available when in Rails mode that allow you to view the log files and start the webserver among other things. I have only just scratched the surface myself and am still learning, so I cannot say much more at the moment!

Update – A few months later

After using Ruby mode and Rails mode for a few months, something I could just not get used to was Electric mode. I can live with the automatically inserted ‘end’ keys words when I open a new method def or an if statement, but single character completions like a closing quote or curly bracket just frustrated me. You still have to type ctrl-f to skip forward a character to get past that auto-inserted quote, or use your arrow keys – its easier to just type the quote!

Different people will prefer different things, so to customise just how much electric you want Emacs to provide you can edit the ruby-electric.el file. Look for a section like the following:

(defun ruby-electric-setup-keymap()
  (define-key ruby-mode-map " " 'ruby-electric-space)
;;  (define-key ruby-mode-map "{" 'ruby-electric-curlies)
;;  (define-key ruby-mode-map "(" 'ruby-electric-matching-char)
;;  (define-key ruby-mode-map "[" 'ruby-electric-matching-char)
;;  (define-key ruby-mode-map "\"" 'ruby-electric-matching-char)
;;  (define-key ruby-mode-map "\'" 'ruby-electric-matching-char)
;;  (define-key ruby-mode-map "|" 'ruby-electric-bar))
  )

Each line provides electric mode for each of the different characters – I commented out all the lines starting with ‘;;’ (as ; denotes a commented line), which means that now I only get electric ‘end’ keywords added and nothing else.

Summary

In this series of five articles, I have explained why I decided to try Emacs, how to install it on windows and the Mac, setup ECB, some Ruby specific tips and finally this article to get the Rails development environment working. Hopefully you found them useful! At this point I am still a relative Emacs newbie – I don’t know much beyond what is in these posts, but I am learning something new almost every day. For the sake of completeness, the contents of my .emacs is below.


; sets emacs to prompt on close so you cannot close by accident!!
;(setq confirm-kill-emacs 'yes-or-no-p)
(global-font-lock-mode 1)

; directory to put various el files into
(add-to-list 'load-path "C:/emacs-22.1/includes")

; 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))

(add-hook 'ruby-mode-hook
          (lambda()
            (add-hook 'local-write-file-hooks
                      '(lambda()
                         (save-excursion
                           (untabify (point-min) (point-max))
                           (delete-trailing-whitespace)
                           )))
            (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)
            ))

; These lines are required for ECB
(add-to-list 'load-path "C:/emacs-22.1/plugins/eieio-0.17")
(add-to-list 'load-path "C:/emacs-22.1/plugins/speedbar-0.14beta4")
(add-to-list 'load-path "C:/emacs-22.1/plugins/semantic-1.4.4")
(setq semantic-load-turn-everything-on t)
(require 'semantic-load)

; This installs ecb - it activated with M-x ecb-activate
(add-to-list 'load-path "C:/emacs-22.1/plugins/ecb-2.32")
(require 'ecb-autoloads)

(setq ecb-gzip-setup (quote cons))
(setq ecb-layout-name "left14")
(setq ecb-layout-window-sizes (quote (("left14" (0.2564102564102564 . 0.6949152542372882) (0.2564102564102564 . 0.23728813559322035)))))
(setq ecb-source-path (quote ("c:/rails")))

; needed for rails mode
(require 'snippet)
(require 'find-recursive)
; The rails require needs to go after ECB
; otherwise it loads a new incompatible speedbar
(add-to-list 'load-path "C:/emacs-22.1/includes/emacs-rails")
(require 'rails)

Advertisements

Entry filed under: emacs.

Emacs Ruby Foo Emacs and Oracle

6 Comments Add your own

  • 1. Phil  |  September 18, 2008 at 8:35 pm

    Nice to see a complete setup from scratch so well-documented.

    You might also want to try rinari. It’s much lighter-weight than emacs-rails, and it’s also still being maintained.

    http://rinari.rubyforge.org

    Also check out the mailing list: http://groups.google.com/group/emacs-on-rails

    Reply
  • 2. daniel  |  February 22, 2009 at 7:32 am

    This tutorial helped me a lot. Thanks

    Reply
  • 3. jv  |  March 24, 2009 at 2:16 am

    Thanks for the tutorial

    Reply
  • […] Emacs and Rails « Software bits and pieces […]

    Reply
  • 5. Paul Gresham  |  June 13, 2009 at 11:25 am

    Just a note to say thanks and agree with #1 above, nice to get a straightforward run through. Saved me a lot of time!

    Reply
  • 6. angel  |  November 26, 2010 at 12:35 am

    for correct indentation only add this at your .emacs

    (add-hook ‘ruby-mode-hook ‘(lambda ()
    (local-set-key (kbd “RET”) ‘newline-and-indent)))

    for ruby and if u want it for rails change ‘ruby-mode for ‘rails-mode

    and forget press the tabs!!!!!

    Reply

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Feeds


%d bloggers like this: