I will only talk about how to set up yasnippet 0.7.0 or higher version.
Table of contents
- 1 If you follow 'normal install' way, setup is simple
- 2 If you use elpa package system, setup is even simpler
- 3 The Real World setup is not simple
- 3.1 Two snippets may share the same key, so I need activate 'yas/dropdown-prompt'
- 3.2 'yas/dropdown-prompt' is not perfect
- 3.3 yasnippets conflicts with other plugins
- 4 my final yasnippet setup
1 If you follow 'normal install' way, setup is simple
See its official documentation.
Install the yasnippet into somewhere and add following code into your. emacs:
(add-to-list 'load-path "~/.emacs.d/plugins/yasnippet-x.y.z")(require 'yasnippet) ;; not yasnippet-bundle(yas/initialize)(yas/load-directory "~/.emacs.d/plugins/yasnippet-x.y.z/snippets")
2 If you use elpa package system, setup is even simpler
After installation, you only need two lines in. emacs.
(require 'yasnippet)(yas/initialize)
Yasnippet 0.7.0 have already defined two locations for the snippets ,"~ /. Emacs. d/yasnippet-install-path/yasnippet-x.y.z/snippets "and "~ /. Emacs. d/snippets ". yasnippet will load snippets in both directories at startup.
So you only put your own snippets in "~ /. Emacs. d/snippets "and done. no need to tweak. emacs at all. to verify my claim, you can 'C-H v Yas/snippet-dirs' to check Value of "Yas/snippet-dirs ". please note "Yas/root-directory" is the alias of "Yas/snippet-dirs ".
3 The Real World setup is not simple
I will explain the reasons at first and give my complete yasnippet configuration code at the end of the this post.
3.1 Two snippets may share the same key, so I need activate 'yas/dropdown-prompt'
One issue is I need a user-friendly dropdown window popped up when the key I input has several candidates. for example, when I type "Inc" in C code. there are two candicates '# include "... "'And' # include <…> 'Available. A handy dropdown popup will help me
To choose one of them as efficient as possible.
The good news is such fancy popup is a standard component of yasnippet. It's called 'yas/dropdown-Prompt '. yasnippet's default algorithm will activate it at highest prority.
The bad news is for some wierd reason yasnippet won't load that dropdown-list by default. so you need manually load that component by one line of elisp code '(require 'dropdown-list )'.
3.2 'yas/dropdown-prompt' is not perfect
I cannot scroll down the dropdown window when there are more candidates it can display. that's especially annoying when calling 'yas/insert-snippet '. in this case, we need use 'yas/completing-prompt' instead. I will show the fix at the end of this article.
3.3 yasnippets conflicts with other plugins
I use auto complete mode (version 20120327 in elpa). There are two issuses when using it with yasnippets.
First, it use Tab key to do the auto-complete thing while yasnippet also uses Tab key. So I need re-configure hotkeys of yasnippets.
Second, yansippet changed its API 'yas/get-snippet-tables 'since version 0.7.0. This make the auto-complete cannot use yasnippet at all. This issue is
Reported and
Fixed By tkf. Actually all you need do is simple:
cd auto-complete-install-dirrm auto-complete-config.elccurl -L https://raw.github.com/tkf/auto-complete/337caa2ccc254a79f615bb2417f0d2fb9552b547/auto-complete-config.el > auto-complete-config.el
4 my final yasnippet setup
(require 'yasnippet)(yas/initialize);; default TAB key is occupied by auto-complete(global-set-key (kbd "C-c ; u") 'yas/expand);; default hotkey `C-c & C-s` is still valid(global-set-key (kbd "C-c ; s") 'yas/insert-snippet);; give yas/dropdown-prompt in yas/prompt-functions a chance(require 'dropdown-list);; use yas/completing-prompt when ONLY when `M-x yas/insert-snippet';; thanks to capitaomorte for providing the trick.(defadvice yas/insert-snippet (around use-completing-prompt activate) "Use `yas/completing-prompt' for `yas/prompt-functions' but only here..." (let ((yas/prompt-functions '(yas/completing-prompt))) ad-do-it))