ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

Emacs: Using Templates with YASnippet

Xah Lee, , …,

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

YASnippet is a template system for emacs. It allows you to type a abbreviation and automatically expand into function templates. Bundled language templates includes: {C, C++, C#, Perl, Python, Ruby, SQL, LaTeX, HTML, CSS, …}. You can define your own template set. The template system is simple plain text based. You do not need to know emacs lisp.

If all you want is a inserting static text, without parameters or moving cursor, then use emacs abbrev. 〔☛ Using Emacs Abbrev Mode for Abbreviation

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="▮" 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 ()
  "thisandthat."
  (interactive)
  (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. It's very confusing.

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

Installing YASnippet

You can install it with emacs 24's package system. 〔☛ A Guide on Emacs 24 Package System

Or, go to https://github.com/capitaomorte/yasnippet. (If you are using ErgoEmacs, then YASnippet is already installed.)

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:

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 〖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/html-mode/〕. You'll see a list of files. Those file names, the first part before the dot “.”, is the abbreviation you can use.

Normally, you simply type a keyword or function name of the language then press Tab ↹. If a template is available, it'll be expanded.

Jumping Thru Parameter Slots

After you inserted a template, you can press Tab ↹ 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.

blog comments powered by Disqus