5

yasnippet does not complete snippets that I add to yas/root-directory (typing the key and hitting tab does not complete).

If I remove .yas-compiled-snippets.el from an affected directory, then run yas/recompile-all then yas/reload-all, I can temporarily use the snippets in that directory. This works for one emacs session, but when I start emacs again, yasnippet again does not recognize the extra snippets.

How can I make the configuration "stick" across runs of emacs?

My yasnippet configuration is like this:

(require 'yasnippet)
(yas/initialize)
(add-hook 'python-mode-hook '(lambda ()
                               (yas-minor-mode)))
(add-to-list 'yas/root-directory "/path/to/elisp/yasnippet-snippets/")
(add-to-list 'yas/root-directory "/path/to/elisp/yasnippet-extra-snippets/")
2
  • The doc-string for yas-snippet-dirs may answer your question. M-x describe-variable RET yas-snippet-dirs RET. The variable name yas/root-directory is a backwards-compatible alias. "Directory or list of snippet dirs for each major mode. The directory where user-created snippets are to be stored. Can also be a list of directories. In that case, when used for bulk (re)loading of snippets (at startup or via 'yas-reload-all'), directories appearing earlier in the list shadow other dir's snippets. Also, the first directory is taken as the default for storing the user's new snippets."
    – lawlist
    Apr 5 '15 at 16:07
  • @lawlist That doesn't seem to point to a problem in my case: my own snippet directories appear first on yas-snippet-dirs (and also the snippet I tested with has a key that is not used in the snippets that come with yasnippet) Apr 5 '15 at 18:51
6

yas-reload-all is the correct way to tell yasnippet about updates to yas-snippet-dirs (aka yas/root-directory), recompilation is not needed.

yas/initialize is basically an obsolete way of saying (yas-global-mode +1), hence your python-mode-hook is redundant. yas-global-mode also calls yas-reload-all when it's turned on, so if you just set yas-snippet-dirs before calling it, your snippets will already be picked up:

(require 'yasnippet)
(add-to-list 'yas-snippet-dirs "/path/to/elisp/yasnippet-snippets/")
(add-to-list 'yas-snippet-dirs "/path/to/elisp/yasnippet-extra-snippets/")
(yas-global-mode +1)

Or, enabling yasnippet only in python-mode:

(require 'yasnippet)
(add-to-list 'yas-snippet-dirs "/path/to/elisp/yasnippet-snippets/")
(add-to-list 'yas-snippet-dirs "/path/to/elisp/yasnippet-extra-snippets/")
(yas-reload-all)
(add-hook 'python-mode-hook #'yas-minor-mode)
7
  • Thanks! I actually don't really want yasnippet elsewhere than python-mode at the moment, so I added (yas-reload-all) to my hook (and removed the yas/initialize) Apr 5 '15 at 20:05
  • @CroadLangshan: reloading in the hook is overkill, since it runs every time you open a new python file. I added an example for enabling yasnippet only in python-mode.
    – npostavs
    Apr 5 '15 at 20:56
  • Oops, very lazy of me. And ditto re eliminating the lambda. Out of curiosity: are you using the # there just as a coding style convention because sometimes it's useful as documentation to make it clear that it's a function? Seems it can be omitted here... Apr 5 '15 at 22:49
  • yes, #' vs ' in this case is just style, see stackoverflow.com/a/16802304/319698 and emacs.stackexchange.com/a/5364/5296
    – npostavs
    Apr 6 '15 at 14:47
  • 1
    “...so if you just set yas-snippet-dirs before calling it, your snippets will already be picked up”. The before part was what I was missing. Aug 19 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.