sacha chua :: living an awesome life


Subscribe!
E-mail Feed reader

Yet another snippet mode for Emacs

While reading gnu.emacs.help, I came across yasnippet (yet another snippet extension for Emacs). It provides interactive templates for text or code entry. It’s similar to SkeletonMode, but with TextMate-type syntax, simpler prompting for information, and more focus on expanding mode-sensitive text in the buffer.

Here’s what I did to get started with it on my system:

  1. Download http://yasnippet.googlecode.com/files/yasnippet-0.5.5.tar.bz2 and unpack it as ~/elisp/snippets.
  2. Download http://yasnippet.googlecode.com/files/yasnippet-bundle-0.5.5.el.tgz and unpack it as ~/elisp/yasnippet-bundle.el .
  3. Symlink lisp-interaction-mode to emacs-lisp-mode with the command
    ln -s ~/elisp/snippets/emacs-lisp-mode ~/elisp/snippets/lisp-interaction-mode
    
  4. Add the following lines to ~/.emacs
    (require 'yasnippet-bundle)
    (yas/initialize)
    (yas/load-directory "~/elisp/snippets")
    ;; I don't like using partial words for completion
    (setq yas/key-syntaxes '("w_" "w_." "^ "))
    

So far, so good. M-x yas/minor-mode lets me use TAB as the completion key, and I can use TAB and S-TAB to navigate between the fields of a snippet as well. I think it’s a decent snippets mode if you don’t need the complexity of skeleton.el.

After some experimentation, I see how this might make me even happier working on Drupal. I’ve put together this first try at a template for Drupal modules. Drupal code has a lot of repetitive typing and/or search-and-replacing because all the hook functions need to start with the module name. With yasnippet, I can type in the module name once, and all the other functions will be updated. I often find myself looking up the argument lists for the hook functions, too, and the template includes the hooks I tend to use. I think this snippet will save me a lot of start-up time.

You know what would be even cooler? If I could create snippets that dynamically calculate values using Emacs Lisp. Then I could create, for example, a hook_user abbreviation that automatically picked up the module name from the filename or from the other functions. Wouldn’t that be cool?

UPDATE: Putting Emacs Lisp in templates is awesome. With this function in my ~/.emacs:

(defun sacha/drupal-module-name ()
  "Return the Drupal module name for .module and .install files."
  (file-name-sans-extension (file-name-nondirectory (buffer-file-name))))

I can make templates like this one for hook_user. Sweet!

So-soHmmGoodGreatAwesome! (No Ratings Yet)
Loading ... Loading ...
Save to - del.icio.us - Digg it - reddit - StumbleUpon -

12 Responses to “Yet another snippet mode for Emacs”

  1. yasnippet supports embedded elisp. I do that for using the current buffer name to declare a “class function” in lua.

    see here:
    http://code.google.com/p/yasnippet/wiki/HowtoDefineSnippet

  2. That makes me so happy. =)

  3. I think you have error with the right parentheses with “${1:module)” in the Drupal module snippets file.

  4. Yeah, yasnippet rocks. Between it and js2-mode, coding javascript is almost dreamy.

    Have you gotten it to work with org-mode though? The way org-mode redefines the TAB keys seems to screw with yasnippet in ways that I couldn’t figure out with my weak emacs-lisp fu.

  5. What I’d love to know is how one chooses a single template mode to commit to. I’m shopping because I’ve used tempo mode heavily in the past and it seems to doubleplus unwork on Aquamacs.

    So how does one sift through all the snippet modes?

    Thanks!

  6. @seth:

    Yeah, I’m having the same issue using YAsnippet in org-mode. When I do a C-h k , it looks like Tab is bound to org-cycle, no matter what I do. YAsnippet is definitely running as a minor mode, but org seems to be hogging the tab key.

    Sacha, have you had any luck using YAsnippet in org-mode?

    –Jeff

  7. It might be a good idea to bind yas/expand to a different key, then… =) Maybe M-?, to make it like an enhanced version of M-/ (dabbrev-expand)?

  8. i’m currently using msf-abbrevs mode – it also supports asking for parameters, mode-dependent snippets, and so on – but it much simple from my point of view

  9. [...] been using yasnippet for my Emacs templates. All I needed to do to automate that little update bit was to write some [...]

  10. Thanks for the suggestion, Sacha. Luckily I didn’t need to use that workaround. Here’s the fix:


    (add-hook 'org-mode-hook
    '(lambda ()
    (make-variable-buffer-local 'yas/trigger-key)
    (setq yas/trigger-key [tab])))

    Worked perfectly for me…

    –Jeff

  11. [...] Yet another snippet mode for Emacs | sacha chua :: enterprise 2.0 consultant, storyteller, geek [...]

  12. Sacha,

    The Drupal module example did not use mirroring in the YASnippet template to minimize typing. I made a quick change and posted it to my blog here.

    It’s almost identical to your original version, but now you can type the module name once and all the hook implementations are changed to the correct value.

Discussion Area - Leave a Comment

Please comment as you, not your organization.





On This Day...

  • 2009: Weekly review: Week ending July 26, 2009 — From last week’s plans Facilitate idea lab online – going well! Do basic analysis of community participation in idea lab Start with improv [...]
  • 2009: Never get used up — While in a client’s office, I noticed this quote: At times our own light goes out and is rekindled by a [...]
  • 2009: Science 2.0: July 29, 2009 1pm-6pm MaRS — What Every Scientist Needs to Know About How the Web is Changing the Way They WorkThe MaRS Centre, 101 College [...]
  • 2008: Emacs is not just for computer geeks; also, these are the things that keep me going — Reposted with permission because (a) it shows that people who aren’t computer geeks can also love Emacs, and (b) it [...]
  • 2008: Want to subscribe to a subset of my blog? — I write a lot about Emacs. I also write a fair bit about Drupal. Both are rather geeky topics that [...]
  • 2008: Things I can do to make progress on my book — Switch my development environment to Emacs Put together the existing book chapters I have so far Process the tech reviews I’ve gotten [...]
  • 2007: Another day, another revision; Exercising in the park; Pinakbet and cherry pie — I sent off another revision today, and I think I’ll be ready to send a copy of my thesis to my [...]
  • 2006: Signed up for a DemoCamp demo — David Crow made me promise to give a DemoCamp session, so I’m going to do one on Emacs as a [...]
  • 2006: Batik and ethnic clothes — I love wearing ethnic clothes. Traditional outfits are hip enough to pass off as casual but dignified enough to go formal, [...]
  • 2006: In print! — Yup, the July 1 issue of the Guardian has my name in print! Whee! Page 3 of the section titled Rise… Time [...]
  • 2006: DemoCamp! — I love going to conferences and geek get-togethers because I always end up having the most interesting conversations. Even though [...]
  • 2006: Argh, need facial — Also, I’m breaking out into pimples again. Not that you need to hear about that, but it annoys me, particularly [...]
  • 2006: Moved to Vaio! — The power adapter on my Fujitsu Lifebook P1110 has just completely given up. Fortunately, my parents had given me the [...]
  • 2005: Toast IT — Charo and the others have been telling me to join the Toastmasters for the longest time. After sifting through a number [...]
  • 2005: My banking needs — I need a Canada-based credit card for most of my purchases. I’ll keep track of all of the purchases and pay [...]
  • 2005: Paper inbox planner — The Paper Planner Inbox is a good post about keeping an “inbox” section in your planner. My index cards used to be [...]
  • 2005: How to fold a shirt — Clair passed me a link to that nifty tutorial on folding shirts. I wonder if the trick works with the [...]
  • 2004: Any spare Gmail invites? — I haven’t actually sent any mail from my gmail account yet, so I don’t have any invites to send. If you [...]
  • 2002: Online submission system — The online submission system I was hacking together works, and I’m now just fiddling around with ideas for the next one.
  • 2002: Laptop got scared — Yes, it’s me. Apparently my laptop got scared and is cooperating again. I’ve backed up the rest of my data and should [...]