Xah Lee, 2010-04-13, …, 2011-11-09
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 abbrevation and automatically expand the abbreviation into function templates. Bundled language templates includes: C, C++, C#, Perl, Python, Ruby, SQL, LaTeX, HTML, CSS and more. But you can define your own template set for your own language, or for any text that you need a template. The template system is simple plain text based. You do not need to know emacs lisp to define your own templates.
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. They are very confusing.
There is one that is simple to use and reasonably powerful, that's YASnippet.
To download, go to: http://code.google.com/p/yasnippet/. (If you are using ErgoEmacs, then YASnippet is already installed.)
Download the one named like “yasnippet-0.6.1c.tar.bz2”. (Don't download the “bundle” version. That version is a simplified version, and you cannot define your own template with it.) If you are on Windows, you can unpack it using Winzip or 7-Zip.
Then, place the folder in a dir named 〔~/.emacs.d/plugins/〕. Create them if the dir doesn't exist.
Then, add the following to your emacs init file:
(add-to-list 'load-path "~/.emacs.d/plugins/yasnippet-0.6.1c") (require 'yasnippet) (yas/initialize) (yas/load-directory "~/.emacs.d/plugins/yasnippet-0.6.1c/snippets")
Restart emacs.
To test you have installed correctly, open a HTML file. Then, you should see a menu YASnippet. Try it.
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.
After you inserted a template, you can press Tab to go thru the different parameter slots.
It is very easy to define your own template. You don't need to know elisp. See: How to Define Templates In YASnippet.