Search code examples
emacsyasnippet

Emacs: load only necessary yasnippets


Suppose, I have 2 subdirectories for yasnippets:

~/.emacs.d/yasnippets/perl-mode
~/.emacs.d/yasnippets/php-mode

Currently I use the following code in my .emacs:

(defvar *my-emacs-lib-dir* "~/.emacs.d/")
(load (concat *my-emacs-lib-dir* "plugins/yasnippet/yasnippet"))
(setq yas/snippet-dirs nil)
(yas/initialize)

;; Develop and keep personal snippets under ~/emacs.d/yasnippets
(setq yas/root-directory (concat *my-emacs-lib-dir* "yasnippets"))
(yas/load-directory yas/root-directory)

So, it loads all the yasnippets in all the subdirectories of ~/.emacs.d/yasnippets.

Is it possible to make it load the yasnippets on demand? If I open a php file, and the snippets for php-mode were not loaded, load them. But not load everything on startup.


Solution

  • If I remember correctly, in fresh versions, the loading of snippets will performed on demand, if you'll use recommended loading sequence:

    (add-to-list 'load-path "~/path-to-yasnippet")
    (require 'yasnippet)
    (yas-global-mode 1)
    

    You can also use the optional use-jit flag to the yas-load-directory function, that will force on demand loading of snippets from this directory. See description of this function (C-h f yas-load-directory)