How to configure yasnippet 0.7.0 and use it with Auto-Complete-Mode

  |   Source

I will only talk about how to set up yasnippet 0.7.0 or higher version.

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")

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". </p> </div> </div> <div id="outline-container-3" class="outline-3"> <h3 id="sec-3">The real world setup is NOT simple</h3> <div class="outline-text-3" id="text-3"> <p>I will explain the reasons at first and give my complete yasnippet configuration code at the end of the this post. </p> </div> <div id="outline-container-3-1" class="outline-4"> <h4 id="sec-3-1">Two snippets may share the same key, so I need activateyas/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. </p> <p> 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)'.

yas/dropdown-prompt' is not perfect</h4> <div class="outline-text-4" id="text-3-2"> <p>I cannot scroll down the dropdown window when there are more candidates it can display. That's especially annoying when callingyas/insert-snippet'. In this case, we need use yas/completing-prompt' instead. I will show the fix at the end of this article. </p> </div> </div> <div id="outline-container-3-3" class="outline-4"> <h4 id="sec-3-3">Yasnippets conflicts with other plugins</h4> <div class="outline-text-4" id="text-3-3"> <p>I use <a href="http://cx4a.org/software/auto-complete/">Auto Complete Mode</a> (version 20120327 in elpa). There are two issuses when using it with yasnippets. </p> <p> 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. </p> <p> Second, yansippet changed its APIyas/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-dir

rm auto-complete-config.elc

curl -L https://raw.github.com/tkf/auto-complete/337caa2ccc254a79f615bb2417f0d2fb9552b547/auto-complete-config.el > auto-complete-config.el

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 &amp; 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 </span><span style="color: #008787;">yas/completing-prompt</span><span style="color: #87005f;">' foryas/prompt-functions' but only here..." (let ((yas/prompt-functions '(yas/completing-prompt))) ad-do-it))

Comments powered by Disqus