Search code examples
emacsyasnippet

Deactivate specific yasnippets in Emacs


With Yasnippet recently updated from MELPA, I would like to be able to deactivate only the snippets xxx and todo that come with text-mode. The first expands with x and the other with t, which bother me because I write math texts in org-mode and I need to write several x's and t's by themselves, and then press TAB to exit parenthesis.

From yas-buffer-local-condition, it seems that I might be able to do something if there was a #condition: directive in the snippets, but the mentioned snippets don't have one.

I get my way if I just delete the files, but unfortunately they reappear at each update of Yasnippet.


Solution

  • One possible solution would be to control the snippets with key bindings by adding a line of code to each snippet -- e.g., # binding: C-I a b c or # binding: C-I d e f  The combination C-I is equivalent to the tab key and the space between the following letters means that they are pressed individually one at a time. In addition, the following lines of code can also be modified to reflect different key(s): # key: a_b_c and # key: d_e_f.

    The variable yas-snippet-dirs can be used to control the location(s) of snippets. It may be a good idea to move snippets to a different location so that they are not touched by future updates (e.g., el-get).


    The xxx snippet looks like this:

    ORIGINAL

    # -*- mode: snippet -*-
    # name: xxx
    # key: x
    # --
    `(yas-with-comment "XXX: ")`
    

    MODIFIED

    # -*- mode: snippet -*-
    # name: xxx
    # key: a_b_c
    # binding: C-I a b c 
    # --
    `(yas-with-comment "XXX: ")`
    

    The todo snippet looks like this:

    ORIGINAL

    # -*- mode: snippet -*-
    # name: todo
    # key: t
    # --
    `(yas-with-comment "TODO: ")`
    

    MODIFIED

    # -*- mode: snippet -*-
    # name: todo
    # key: d_e_f
    # binding: C-I d e f
    # --
    `(yas-with-comment "TODO: ")`
    

    For those who are curious, the function yas-with-comment looks like this

    (defun yas-with-comment (str)
      (format "%s%s%s" comment-start str comment-end))