This blog has moved
I'm leaving these up to give myself some time to get the redirects together. Please visit the new blog at: blog.matasar.org
Ben Matasar
Emacs Snippets and Ruby
I've crossed a threshold past which it's actively painful to use any editor that is not emacs. I'm not thrilled by this, especially as I modify my emacs to make it even more different from all other tools I might use in the world. I registered TextMate before I broke myself, and I still use it for files with multiple source languages, like html/css/javascript. For almost everything else, I am now an emacs user. One feature I used to miss from TextMate is snippets, which are little shortcuts that autocomplete common phrases. Happily, an enterprising emacs-lisp hacker wrote snippet.el that builds on abbrev-mode. Combined with a chunk of the emacs-rails project, I have the following in my ~/.emacs:
(setq ruby-mode-abbrev-table (make-abbrev-table)) (require 'rails-lib) (def-snips (ruby-mode-abbrev-table) ("ea" "each { |$${elm}| $${body} }" "each") ("eacho" "each do |$${elm}|\n$>$.\nend" "each do") ("sorb" "sort_by { |$${elm}| $${body} }" "sort_by") ("coll" "collect { |$${elm}| $${body} }" "collect") ("collecto" "collect do |$${elm}|\n$>$.\nend" "collect do") ("sel" "select { |$${elm}| $${body} }" "select") )
Note that this will not work unless you are in abbrev-mode. Other than def-snips, I don't use emacs-rails at all, because I find that it's too heavyweight for me now that I know my way around. So when I type, for example, eacho followed by spacebar, emacs autocompletes with:
each do |elm| end
I can tab around the block to fill in the individual fields. The biggest win is that I don't have to type pipes or other hard to reach characters for common block idioms in ruby. It's sort of hard to explain, but worth trying and wonderful for TextMate users. I use snippets all the time, and add new snippets as I notice common things I type in Ruby or Python.
Ben Matasar ben@matasar.org