I have a yasnippet with # binding: TAB f o o in the header. This is processed by the yasnippet library as 9 102 111 111. With respect to a keymap of a major-mode, both \C-i and (kbd "TAB") are converted to the number 9 when said keymap is set up. However, both [(tab)] and (kbd "<tab>") are converted to tab when the the major-mode keymap is set up.

A yasnippet with a key binding in the header consisting of TAB f o o will not trump/supersede a major-mode keybinding of [(tab)] or (kbd "<tab>").

Q:   How can I set up two (2) different yasnippet header keybindings to cover both situations that may exist in a particular major-mode; i.e., a major-mode key binding that uses a number 9 and also a key binding that uses a tab?

Here is a link to a related thread, which is interesting, but does not go as far as I would like in order to be able to answer this question: What is the difference between TAB and <tab>?


1 Answer 1


yas--read-keybinding is a helper function that processes the keybinding contained within the heading of each snippet, and said function calls read-kbd-macro. The following are the results of the four possibilities mentioned in question above:

  1.  (read-kbd-macro "TAB" 'vector) => [9]

  2.  (read-kbd-macro "[(tab)]" 'vector) => [91 40 116 97 98 41 93]

  3.  (read-kbd-macro "<tab>" 'vector) => [tab]

  4. (read-kbd-macro "\C-i" 'vector) => [] => nothing in between the matching square brackets

There are a variety of possibilities to resolve the issue described in the question above, but creating multiple bindings in the snippet itself would require rewiring the innards of the yasnippet.el library.

We can probably all agree that writing virtually duplicate snippets, with the only difference being the keybinding, is not a good solution.

I observed that once a snippet keybinding of 9 trumps a major-mode keybinding, other snippets with a variation of tab will follow suit. I.e., it is possible to have some snippets with a 9 and some snippets with a tab and the combination is sufficient trump either form in the major-mode keybinding. [Although not tried, based on the same rationale, it is probably possible to define both a 9 and a tab in the major-mode keybinding and then either one of them appearing in a snippet would be sufficient to trump.]


If the major-mode uses \C-i or (kbd "TAB"), then the snippet should use TAB.

If the major-mode uses [(tab)] or (kbd "<tab>"), then the snippet should use <tab>.

Or, try defining both forms in the major-mode keybindings as discussed hereinabove and use either TAB or <tab> in the snippet.

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.