Deactivate specific yasnippets in Emacs
Asked Answered
F

1

6

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.

Frogman answered 7/3, 2014 at 0:22 Comment(3)
I'm confused. Snippets are organized by major mode (e.g., text-mode), and inside each snippet the user sets the keyboard shortcuts that will activate the snippet. So to modify how each snippet is activated, you need to open each snippet and program what keyboard combinations will be used to activate it. Have you opened up the text-mode snippets to see what makes them tick? If so, then we will need some more information to better assist you.Thill
It may behoove you to also look into the variable yas-snippet-dirs and move your snippets to a location that will not be touched by future updates from MELPA.Thill
@Thill Thanks. however I did not define the snippets mentioned, they come with the Yasnippet package from MELPA. I already mentioned the keys that trigger them.Frogman
T
3

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))
Thill answered 7/3, 2014 at 2:46 Comment(2)
Thanks. I have put modified xxx and todo in ~/.emacs.d/snippets/text-mode, which comes first than the distributed snippets and therefore seem to overwrite their definitions. This seem to work.Frogman
I have tried same for python-mode: ~/.emacs.d/snippets/python-mode but seems like it did not overwrite the ones that exist under /usr/share/yasnippet-snippets/python-modeApophyllite

© 2022 - 2024 — McMap. All rights reserved.