Yasnippet

Description

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 https://github.com/AndreaCrotti/yasnippet-snippets 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:

Customizing snippet prompts

See `yas-prompt-functions'.

With ido

As of yasnippet version 0.9.1, prompts will using ‘ido’ if ‘ido-mode’ is enabled by default.

With 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.'"
    (interactive)
    (if (require 'helm-config nil t)
        (let ((result (helm-other-buffer
                       (list `((name . ,prompt)
                               (candidates . ,(if display-fn (mapcar display-fn choices)
                                                choices))
                               (action . (("Expand" . identity)))))
                       "*helm-select-yasnippet")))
          (cond ((null results)
                 (signal 'quit "user quit!"))
                (display-fn
                 (catch 'result
                   (dolist (choice choices)
                     (when (equal (funcall display-fn choice) result)
                       (throw 'result choice)))))
                (t result)))
      nil))

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

With Popup menu

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)
    (popup-menu*
     (mapcar
      (lambda (choice)
        (popup-make-item
         (or (and display-fn (funcall display-fn choice))
             choice)
         :value choice))
      choices)
     :prompt prompt
     ;; start isearch mode immediately
     :isearch t
     )))

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

See also

Similar packages


CategoryTemplates