4

Let's say I have a yasnippet snippet called foobar and I have configured it to work it with company-mode (which is not relevant here). Now each time I needed to type foobar I would select it from completion popup to expand yasnippet.

But now I would like to bind this yasnippet insertion action to a key.

I found the yas/insert-snippet function. But then I need to select the corresponding snippet.

I'm looking for a configuration like below to insert the foobar snippet.

(global-set-key "C-b" yas/insert-snippet "foobar") ;; PSEUDO CODE

Any suggestions? It would be really appreciated.

  • 1
    Snippets can contain a line that looks like this: # binding: C-I z If you place the snippet inside a folder with the mode-name within the main snippet directory, the keyboard shortcut will work in that major-mode if yasnippet is configured correctly. That method can also be used to execute elisp code within the snippet (if applicable) so long as it is wrapped in back-ticks. I access all my snippets like that, or by using a right click pop-up mouse menu that I have customized to read all available snippets for a given mode. NOTE: This # binding method will hijack the shortcut. – lawlist May 20 '15 at 17:07
  • 2
    Here is a link to the yasnippet manual that describes direct binding: capitaomorte.github.io/yasnippet/… – lawlist May 20 '15 at 17:12
  • 1
    Thanks for the suggestion, lawlist! It's appreciated. But I see two little issues here: - I use evil. I noticed it will work when you're in insert mode of Evil. When you're currently in normal mode, the keybinding will do nothing. - - I would like to bind it to the key gl with keychord. So I might need a solution in Elisp, because yasnippet doesn't know the keys gl. – ReneFroger May 20 '15 at 19:37
  • @lawlist that link doesn't seem to be working. Updated one - joaotavora.github.io/yasnippet/… HTH – Nikita Fedyashev Nov 4 at 19:28
5

The # binding: header documented in the manual is the official method to do this. It's implemented using emulation-mode-map-alists which means it will override any other major or minor mode keybindings, except for packages which also use emulation-mode-map-alists, typically emulation packages like Evil.

I noticed it will work when you're in insert mode of Evil. When you're currently in normal mode, the keybinding will do nothing. -- I would like to bind it to the key gl with keychord.

You could use something like:

(defun exand-yasnippet-foobar ()
  "Expand the yasnippet named `foobar'."
  (interactive)
  (yas-expand-snippet (yas-lookup-snippet "foobar")))

And then bind expand-yasnippet-foobar to some key in whatever keymap you like. Make sure your snippet includes a # name: foobar header.


yas-lookup-snippet was added to yasnippet on August 3rd, 2015. For older versions it can be defined as

(defun yas-lookup-snippet (name &optional _mode noerror)
  (let* ((yas-choose-tables-first nil)  ; avoid prompts
         (yas-choose-keys-first nil)
         (snippet (cl-find name (yas--all-templates
                                 (yas--get-snippet-tables))
                           :key #'yas--template-name :test #'string=)))
    (cond
     (snippet (yas--template-content snippet))
     (noerror nil)
     (t (error "No snippet named: %s" NAME)))))
  • I know I'm late with my comment, but I saw I never thanked you for your solution. Sorry for the delay, but thank you for your support! It's really appreciated! – ReneFroger Aug 24 '18 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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