Last major edit (later minor edits)
Changed:
< See what it looks like: http://www.youtube.com/watch?v=18xYbPCJOho (Video removed by user; dead link. There is this one http://www.youtube.com/watch?v=ZCGmZK4V7Sg).
to
> See what it looks like: http://www.youtube.com/watch?v=ZCGmZK4V7Sg.
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.
Bundled language templates includes: 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 http://github.com/capitaomorte/yasnippet
To install the normal archive, download and unpack the latest yasnippet-*.tar.bz2. to a suitable directory like ~/.emacs.d/packages/ and add the following in your ~/.emacs InitFile:
(add-to-list 'load-path "~/.emacs.d/packages/yasnippet-x.y.z") (require 'yasnippet) ;; not yasnippet-bundle (yas--initialize) (yas/load-directory "~/.emacs.d/packages/yasnippet-x.y.z/snippets")
To quickly tryout Yasnippet, download the a simpler “bundle” version. If you plan to modify the existing templates or make your own, you should download the “normal” package.
1. Download yasnippet-bundle-*.el tar gz and unpack it.
2. You’ll get a file named yasnippet-bundle.el, put it in ~/.emacs.d/packages/ (create that directory if not exists).
3. Open the file in Emacs, and type Alt+x eval-buffer.
That’s it. Now, open any file, you’ll see a menu “YASnippet”. You can pull the menu to insert a template. Or, you can type a pre-defined abbrev and press TAB to expand it! To have Emacs load YASnippet automatically when it starts, put the following in your ~/.emacs file:
(add-to-list 'load-path "~/.emacs.d/packages") (require 'yasnippet-bundle)
See few templates I wrote. – AndyStewart
See http://coderepos.org/share/browser/config/yasnippet for another 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:
(defun shk-yas/helm-prompt (prompt choices &optional display-fn) "Use helm to select a snippet. Put this into `yas/prompt-functions.'" (interactive) (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 (list (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)))) nil))
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-ido-prompt yas-no-prompt))
Evaluate this snippet:
;; Completing point by some yasnippet key (defun yas-ido-expand () "Lets you select (and expand) a yasnippet key" (interactive) (let ((original-point (point))) (while (and (not (= (point) (point-min) )) (not (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) (yas-expand)))))
and map yas-ido-expand with a chord, e.g.:
(define-key yas-minor-mode-map (kbd "<C-tab>") 'yas-ido-expand)enjoy ;)
YASnippet has recently entered version 0.6.x. For this version many of the tweaks in Lisp:yasnippet-config.el are probably invalid or unnecessary. See the changelog – JoaoTavora?
This version of Lisp:yasnippet-config.el works fine with 0.6.x. – rubikitch
I created a new method of importing TexMate? templates entirely in emacs lisp. Perhaps later it might be integrated into yasnippet itself. Here is the code Lisp: textmate-to-yas.el – MatthewFidler
I have changed the startup mechanism so it doesn’t load every single snippet upon startup. Only loaded on demand. yas-jit.el