I found that yasnippet suggests using this code to limit it to only expand when the trigger text has just been typed:

(setq yas-expand-only-for-last-commands 'self-insert-command)

Unfortunately when I add it to my config, yasnippet no longer expands. My guess is that some other command is being run right after I insert text, and thus the self-insert-command is not the most recent command. Yasnippet says it can take a list of different commands, so how can I check what commands follow a self-insert-command (or just a list of all commands) to try to have yasnippet work with them?

2 Answers 2


The yas-expand-only-for-last-commands variable specifically takes a list of functions, not just a function, so to properly set it, the correct form is this:

(setq yas-expand-only-for-last-commands '(self-insert-command))


You can find out what command gets run immediately after a self-insert by typing something, then querying last-command. The docstring:


The last command executed.

Normally a symbol with a function definition, but can be whatever was found in the keymap, or whatever the variable `this-command' was set to by that command.


  • type something
  • hit M-: last-command
  • it'll tell you if the last command really was self-insert-command or not
  • Well, turns out the last command really was self-insert-command, which means that this probably doesn't have the simple solution I was hoping for. This might be a problem with yasnippet itself.
    – Lucky1313
    Jun 20, 2017 at 23:42
  • 1
    @Lucky1313: your best bet is to start Emacs without your init file (emacs -Q), then load yasnippet, confirm that it fires, then try to (setq yas-expand-only-for-last-commands 'self-insert-command). If that all works as expected, the problem is somewhere in your init file. You can then recursively bisect the init file (comment out half, then half of the non-working half, and so on) until you isolate the code that is causing the issue.
    – Dan
    Jun 20, 2017 at 23:46
  • So it turns out that the proper command is (setq yas-expand-only-for-last-commands '(self-insert-command)), which is how it is documented. I did not properly copy down how it was to be configured. Makes sense since it takes a list of commands. Thanks for your help anyway!
    – Lucky1313
    Jun 21, 2017 at 0:07
  • @Lucky1313: please write up your comment and post it as an answer, and note explicitly that you need to set the variable to a list (even if there's only one element in it). It's likely that someone else will make the same mistake, and your post could help. After you post your answer and the system allows you to do so, feel free to un-accept my answer and accept your own, since it directly answers your original question.
    – Dan
    Jun 21, 2017 at 0:13
  • @Lucky1313: You're welcome! Nice job getting it sorted out on your own.
    – Dan
    Jun 21, 2017 at 0:24

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.