1

I want to emulate (and even replace) some electric modes (eg. cperl-electric, ruby-electric) with my own custom templates using yasnippet, but it doesn't seem to be possible - can someone enlighten me?

So in addition to normal templates triggered by TAB, I'd like to have some snippets triggered simply by a space, eg. typing "forSPACE" should fire a snippet instead of having to type "forTAB".

Is that possible?

Update: I also realise that for some "electric" features I want some patterns to fire without any sort of trigger key - eg. like cperl-mode turns { into a paired {} with the insertion point in the middle, I'd like to do the same for single and double quotes. (Of course in this case what I really want is paren-mode to work with syntax less regular than lisp, but I fought that battle some years ago with only limited success!)

  • You might want to check out the built-in abbrev-mode. – Dan Jan 16 '16 at 14:33
  • Thanks @Dan, I hadn't really looked at abbrev-mode before. It's a bit confusing! I also realised that to properly do "electric" things I need some patterns to fire without a space, eg. turning a ' into a paired '' with the insertion point in the middle. – Mark Aufflick Jan 17 '16 at 11:48
2

You can change yasnippet's trigger key to SPC, but it's not per snippet.

(define-key yas-minor-mode-map (kbd "<tab>") nil)
(define-key yas-minor-mode-map (kbd "TAB") nil)
(define-key yas-minor-mode-map (kbd "SPC") ; `yas-maybe-expand' new in 0.12
  (or (bound-and-true-p yas-maybe-expand) #'yas-expand))

See trigger key.


what I really want is paren-mode to work with syntax less regular than lisp, but I fought that battle some years ago with only limited success!)

You can try to emulate this with keybinding snippets, see # binding: direct keybinding and joaotavora/yasnippet#533.

e.g. this would give you electric parens:

# -*- mode: snippet; require-final-newline: nil -*-
# name: (
# binding: (
# --
($0)

But it's probably better to use something like smartparens, or electric-pair-mode as joaotavora suggested:

If you want to try out a parenthesis and quote matching alternative to smartparens, I suggest, in this order:

  • Oh awesome, a few great options here. Hooray for emacs.stackechange! – Mark Aufflick Jan 17 '16 at 23:17

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.