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/")
Croad Langshan
  • 3,192
  • 14
  • 42
  • 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 05 '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) – Croad Langshan Apr 05 '15 at 18:51

1 Answers1

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)
npostavs
  • 9,033
  • 1
  • 21
  • 53
  • 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`) – Croad Langshan Apr 05 '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 05 '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... – Croad Langshan Apr 05 '15 at 22:49
  • yes, `#'` vs `'` in this case is just style, see http://stackoverflow.com/a/16802304/319698 and http://emacs.stackexchange.com/a/5364/5296 – npostavs Apr 06 '15 at 14:47
  • I'm running emacs 25, so I might have run into something deprecated, but for me the add-hook line should read `(add-hook 'python-mode-hook #'yas-minor-mode)` – Silfheed Nov 18 '15 at 03:15
  • @Silfheed: no, you're correct, I just made a mistake, it's fixed now. – npostavs Nov 18 '15 at 03:25
  • 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. – Fernando Basso Aug 19 '21 at 11:00