I want to have a way, to efficiently create new YaSnippet-snippets. YaSnippet has the functionality, that via pressing C-c C-n one can create a new snippet. However, the empty new snippet is very basic. It looks something like this:

# -*- mode: snippet -*-
# name: 
# key: 
# --

When creating many similar snippets, which have headers, like this:

# -*- mode: snippet -*-
# name: foo
# key: foo
# group: math
# condition: (and (texmathp) 'auto)
# --

I need to type or copy everything every time I create a new snippet.

How can I create a Template for creating snippets, such that, when pressing C-c C-n the advanced heading gets inserted?

I looked through the YaSnippet-manual and did not find anything appropriate.

Ideally I would have different templates, such that pressing C-c C-n prompts me for a key, to chose which template to use.

(It would be even better when the snippet after saving with C-c C-c then gets put in the correct subdirectory of the snippets folder ("latex-mode" in this case) without asking in which to put it, but this is optional, it's just 1 keystroke everytime, which is not that bad.)

  • With respect to the folder in which new snippets are created, the commentary in the yasnippet.el library for yas-new-snippet states: "Lets you create a new snippet file in the correct subdirectory of yas-snippet-dirs, according to the active major mode." github.com/joaotavora/yasnippet/blob/master/yasnippet.el#L85 If the snippet is not being created in the folder that you expect (which must also coincide with the statement in the library commentary, supra), then check the value of the variable yas-snippet-dirs .... You can inspect the value of a variable with C-h v.
    – lawlist
    Commented Aug 17, 2022 at 16:15

1 Answer 1


Customize the variable yas-new-snippet-default (or let-bind the variable for temporary modification only), which is used when calling the function yas-new-snippet.


  • Thank you! This worked :-) Do you know of a possibility to define multiple such templates, or is it only possible to change the default?
    – Sinthoras
    Commented Aug 17, 2022 at 17:40
  • You can create a custom function that asks for input, and/or accepts arguments, and/or accepts a universal argument, and let-bind various templates .... once the template has been chosen, yas-new-snippet-default can be let-bound to the selected template and then the function yas-new-snippet can be called and it will assume the chosen template is the one to use. You can also create multiple different variables for templates and the custom function can use them; e.g., template-one--var, template-two--var, etc. That would be a new question and would require a new thread.
    – lawlist
    Commented Aug 17, 2022 at 17:55
  • That is a good idea. It would probably be better to have one variable, which is a list, rather than multiple variables, wouldn't it? I will try that, (and on the way learn some more elisp) thank you!
    – Sinthoras
    Commented Aug 17, 2022 at 18:54
  • Yes, I like your idea of a list. Happy coding ...!
    – lawlist
    Commented Aug 17, 2022 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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