Emacs Templates with YASnippet

Perm url with updates: http://xahlee.org/emacs/emacs_templates.html

Emacs Templates with YASnippet

Xah Lee, 2010-04-13

This page shows you a very useful and easy to use tool for inserting any type of templates for any programing language.

What's a Template?

A Template is a way to insert text you use often. For example, in html, you can type “div”, then press a hotkey, then it expands to:

<div id="some_id" class="some_class"></div> 

With your cursor placed in a parameter slot, and pressing Tab will move to next parameter slot.

In perl, type “for”, press a hotkey, and it becomes:

for (my $var = 0; $var < expression; $var++) {
    # body...

In emacs lisp, type “defun”, then it expands to:

(defun myFun ()
  (let (var1)
    (setq var1 some)
    ; body...

Emacs has lots of template systems. For example, there are MsfAbbrev, TemplatesMode, ElseMode, SkeletonMode, TempoMode. Their feature are quite different, their documentation quality varies, some requires you to know lisp if you want to define you own. In most cases, you need to be a emacs expert to use them. They are very confusing.

There is one that is simple to use and fairly powerful, that's YASnippet.


Download and Install

To download, go to: http://code.google.com/p/yasnippet/. (If you are using ErgoEmacs.org, then YASnippet is already installed.)

Download the one named like “yasnippet-0.6.1c.tar.bz2”. If you are on Windows, you can unpack it using Winzip or 7-Zip. (Don't download the “bundle” version. That version is a simplified version, and you cannot define your own template with it.)

Then, place the folder in a dir named “~/.emacs.d/plugins/”. Create them if they don't exist.

Then, add the following to your emacs init file “.emacs”:

(add-to-list 'load-path "~/.emacs.d/plugins/yasnippet-x.y.z")
(require 'yasnippet)
(yas/load-directory "~/.emacs.d/plugins/yasnippet-x.y.z/snippets")

The “x.y.z” part should match the yasnippet version number.

Restart emacs.

To test you have installed correctly, open a html file. Then, you should see a menu YASnippet. Try it.

How to Use Yasnippet?

You can insert a template by using the menu, or just type the abbreviation for the template.

emacs YASnippet menu

YASnippet's html submenu. The left side are templates, the right side are abbreviations to use.

Find Out What Templates Are Available

For example, let's say you are coding html. Go to the menu, pull “YASnippet‣html-mode”, then you'll see a submenu. There's a item named “<body>...</body>”. On the right side it shows “body =>”. That means, you can type “body”, then press Tab, then it'll expand to “<body></body>”.

The menu gives you a overview of what templates are available for that language.

Another way to know what templates are available is from the folder. For example, for html, go to the dir: “~/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode”. You'll see a list of file. Those file names, the first part before the dot “.”, is the abbreviation you can use.

Jumping Thru Parameter Slots

After you inserted a template, you can press Tab key to go thru the different parameter slots.

Define Your Own Template

It is very easy to define your own template. You don't need to know elisp. See: How To Define Templates In YASnippet.

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs