YASnippet is a template system for Emacs. It allows you to type an abbreviation and automatically expand it into function templates.

See what it looks like: http://www.youtube.com/watch?v=ZCGmZK4V7Sg.

The MELPA package comes with snippets from https://github.com/AndreaCrotti/yasnippet-snippets, language templates include: C, C++, C#, Perl, Python, Ruby, SQL, LaTeX, HTML, CSS and more. The snippet syntax is inspired from TextMate's syntax, you can even import most TextMate templates to YASnippet.

Hosted at https://github.com/joaotavora/yasnippet, manual at http://joaotavora.github.io/yasnippet/. Stable versions also available from GNU ELPA: https://elpa.gnu.org/packages/yasnippet.html

Installation instructions

The usual ‘M-x package-install yasnippet’ should suffice, see also https://github.com/joaotavora/yasnippet/blob/master/README.mdown#installation

User contributed templates

See http://coderepos.org/share/browser/config/yasnippet for a repository.

Here are some templates for the JSP Standard Tag Library: http://github.com/eentzel/yasnippet-jstl/

There are four packages to automatically create yasnippets:

Select snippet using helm (anything.el)

  (defun shk-yas/helm-prompt (prompt choices &optional display-fn)
    "Use helm to select a snippet. Put this into `yas-prompt-functions.'"
    (setq display-fn (or display-fn 'identity))
    (if (require 'helm-config)
        (let (tmpsource cands result rmap)
          (setq cands (mapcar (lambda (x) (funcall display-fn x)) choices))
          (setq rmap (mapcar (lambda (x) (cons (funcall display-fn x) x)) choices))
          (setq tmpsource
                 (cons 'name prompt)
                 (cons 'candidates cands)
                 '(action . (("Expand" . (lambda (selection) selection))))
          (setq result (helm-other-buffer '(tmpsource) "*helm-select-yasnippet"))
          (if (null result)
              (signal 'quit "user quit!")
            (cdr (assoc result rmap))))

There is also the MELPA package helm-c-yasnippet which can be used outside of the yas itself.

Popup menu for yas-choose-value

Taken from http://blog.iany.me/2012/03/use-popup-isearch-for-yasnippet-prompt/

Note: yasnippet from git (ver. 0.8) (new yas-* instead of yas/* functions and variable names)

;;; use popup menu for yas-choose-value
(require 'popup)

;; add some shotcuts in popup menu mode
(define-key popup-menu-keymap (kbd "M-n") 'popup-next)
(define-key popup-menu-keymap (kbd "TAB") 'popup-next)
(define-key popup-menu-keymap (kbd "<tab>") 'popup-next)
(define-key popup-menu-keymap (kbd "<backtab>") 'popup-previous)
(define-key popup-menu-keymap (kbd "M-p") 'popup-previous)

(defun yas-popup-isearch-prompt (prompt choices &optional display-fn)
  (when (featurep 'popup)
      (lambda (choice)
         (or (and display-fn (funcall display-fn choice))
         :value choice))
     :prompt prompt
     ;; start isearch mode immediately
     :isearch t

(setq yas-prompt-functions '(yas-popup-isearch-prompt yas-ido-prompt yas-no-prompt))

ido-complete yasnippet keys

Note: requires newer yas than v0.8.

Evaluate this snippet:

;; Completing point by some yasnippet key
(defun yas-ido-expand ()
  "Lets you select (and expand) a yasnippet key"
    (let ((original-point (point)))
      (while (and
              (not (= (point) (point-min) ))
               (string-match "[[:space:]\n]" (char-to-string (char-before)))))
        (backward-word 1))
    (let* ((init-word (point))
           (word (buffer-substring init-word original-point))
           (list (yas-active-keys)))
      (goto-char original-point)
      (let ((key (remove-if-not
                  (lambda (s) (string-match (concat "^" word) s)) list)))
        (if (= (length key) 1)
            (setq key (pop key))
          (setq key (ido-completing-read "key: " list nil nil word)))
        (delete-char (- init-word original-point))
        (insert key)

and map yas-ido-expand with a chord, e.g.:

    (define-key yas-minor-mode-map (kbd "<C-tab>")     'yas-ido-expand)

enjoy ;)

rubikitch has some additional yasnippet-related functions in Lisp:yasnippet-config.el

See also


I created a new method of importing TextMate templates entirely in Emacs Lisp. Perhaps later it might be integrated into yasnippet itself. Here is the code Lisp: textmate-to-yas.elMatthewFidler

(old discussions removed to reduce confusion about yasnippet versions)