I've decided to try out yasnippet recently, and while it seems to work for me, I have an issue with how it handles comments. I've set my trigger key to SPC, which causes unnecessary snippet expansion in comments (and strings).

It seems that yasnippet has a predefined condition to avoid expansion in such cases in yas-not-string-or-comment-condition, but it causes the trigger key to do nothing rather than falling back to its original behavior, namely self-insert-command. After checking whether setting yas-buffer-local-condition to yas-not-string-or-comment-condition solves the problem for me, I attempted to wrap the function that's bound to trigger keys (yas-expand-from-trigger-key) with advice that falls back to running self-insert-command, but that fails since I don't have access to the key that was pressed.

Has anyone ran into this issue (I wasn't able to find any questions about this here, or in github issues for yasnippet)?



This is the recommended way, but unfortunately it can't work in my Emacs, I don't know why, but you could try it:

(define-key yas-minor-mode-map (kbd "SPC") yas-maybe-expand)

Edit after a few months later: this issue has been fixed, see this issue if you're insterested in.

After digging into the source code a bit, I find that below settings could help:

(setq yas-buffer-local-condition yas-not-string-or-comment-condition)
(define-key yas-minor-mode-map (kbd "SPC") 'yas-expand)  ; assume you use this
(setq yas-fallback-behavior 'call-other-command)         ; default is 'return-nil

But it says yas-fallback-behavior is obsolete, and yas-maybe-expand should be used instead, which I haven't figured out how to yet :(

P.S. There is a github issue about this.

  • Thanks, it seems to work. Here's the related commit for my config file: github.com/DoMiNeLa10/.emacs.d/commit/…. I'll give you the bounty and accept the question once I'm able to.
    – user12563
    Dec 26 '18 at 21:49
  • So yas-maybe-expand also doesn't work for you? Sounds like a bug then.
    – whatacold
    Dec 27 '18 at 9:56

Your Answer

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