###

alias hbin me

Emacs User Tips for Rails

Emacs is my favorite text editor. I have been used Emacs for 8 months. And recently I’m learning Ruby on Rails following the Ruby on Rails Tutorial written by Michael Hartl.

Here is my Emacs tricks for programming Rails:

  • Shortcut For Inserting Arrows

    There are so many hash rockets. It’s a good practise to bind some key to insert it handy.

1
2
3
4
5
6
7
8
;;;###autoload
(defun insert-arrow ()
  (interactive)
  (delete-horizontal-space t)
  (insert " => "))

(define-key ruby-mode-map (kbd "C-.") 'insert-arrow)
(define-key rinari-minor-mode-map (kbd "C-.") 'insert-arrow)

Ruby 1.9 added a new syntax for hashes.

  • Modify Ruby Syntax

    I use mark-multiple to highlight symbols/variables in the buffer. But I found it can’t distinguish the four types of scoped variable. This can be solved by modify the syntax table.

1
2
3
4
(add-hook 'ruby-mode-hook
          (lambda ()
            (modify-syntax-entry ?$ "w")
            (modify-syntax-entry ?@ "w")))
Before

After

SUPPLEMENT
I also use auto-complete to complete symbols/variables. Some times, I say

1
let!(:newer_micropost) { FactoryGirl.create(:micropost, user: @user, created_at: 1.day.ago)

and

1
@user.microposts = newe

then I want auto-complete the reset of newer_micropost. So I press TAB, oops! nothing completed.

That’s because the punctuation character : had been declared to be a part of a word. It means that the :newer_micropost and the newer_micropost are two independent words. Yes, they are. But what I want is they should be completed by the auto-complete. This can be accomplished by a little hacky fragment:

1
2
3
(add-hook 'ruby-mode-hood
          (lambda ()
            (modify-syntax-entry ?: "."))

Before                       After

  • Fold/unfold code

    I use hs-minor-mode to fold/unfold blocks, add following snippet to support ruby block syntax.

1
2
3
4
(add-to-list 'hs-special-modes-alist
             '(ruby-mode
               "\\(class\\|def\\|do\\|if\\)" "\\(end\\)" "#"
               (lambda (arg) (ruby-end-of-block)) nil))

Outline-mode is a good choice too.

1
2
3
4
(add-hook 'ruby-mode-hook
          (lambda ()
            (outline-minor-mode)
            (setq outline-regexp " *\\(def \\|class\\|module\\)")))

Actually, I don’t use fold/unfold frequently, I have no idea which is a better solution. Any other good folding add-on?

  • YARI

    YARI(Yet Another Ri Interface) provides an Emacs frontend to Ruby’s ri documentation tool. It offers lookup and completion. It’s a good practice to bind a help-command for call YARI handy.

1
(define-key 'help-command "R" 'yari)

Now you can just type C-h R to call yari anywhere. Yari will show ri in Ido-style. It also support Anything-style, but Anything had renamed to Helm, you should add some snippet to support ‘Helm-style’

1
2
3
4
5
6
7
;;;###autoload
(defun yari-helm (&optional rehash)
  (interactive (list current-prefix-arg))
  (when current-prefix-arg (yari-ruby-obarray rehash))
  (helm 'yari-anything-source-ri-pages (yari-symbol-at-point)))

(define-key 'help-command "R" 'yari-helm)
  • Rinari

    Rinari Is Not A Ruby IDE. Must have add-on.

  • The forgotten TextMate

    TextMate is a minor mode to mimick the TextMate’s awesome. It’s quite common used to navigate between project files. The is bound to Meta(Alt) on the Ubuntu.

  • Other useful add-ons

    Ruby-Tools

    Ruby tools is a collection of handy functions for Emacs ruby-mode.

Ruby-Block

Ruby block will show you which block you are in. Since there are too many describe blocks in rspec, this will be helpful.

1
2
3
4
(require 'ruby-block)
  (setq ruby-block-delay 0)
  (setq ruby-block-highlight-toggle t)
  (ruby-block-mode t)

For more tricks, feel free to search my dotfiles for emacs hosted on the Github, bugs and fixes are welcome ;) The Ultimate Collection of Emacs Resources is also a good place for improvement.

PS: More tips are welcome!

Comments