2

I have a small snippet like this:

# -*- mode: snippet -*-
# name: options
# key: vop
# --
var options = {
    target:        '#$1',
    beforeSubmit:  showRequest,
    success:       showResponse
}$0

What I would like to do is to increment function name for each insertion. Like this:

var options1 = {
  target:        '#',
  beforeSubmit:  showRequest,
  success:       showResponse
}

var options2 = {
  target:        '#',
  beforeSubmit:  showRequest,
  success:       showResponse
}

etc... Is there a way to achieve this?

1 Answer 1

2

Yes, in yasnippet you can use elisp code, so it's easy:

# -*- mode: snippet -*-
# name: options
# key: vop
# --
var options`(if (boundp 'vop-count) (setq vop-count (1+ vop-count)) (setq vop-count 1) ) (vop-count)` = {
    target:        '#$1',
    beforeSubmit:  showRequest,
    success:       showResponse
}$0

If you wanna reset the counter, run (setq vop-count 0)

4
  • Thank you. But since I've less than 15 reputation I guess I can not choose it as accepted answer.
    – user13916
    Commented Nov 7, 2016 at 13:15
  • @Erdem really? I didn't know that. It seems stupid since the this site should welcome newcomers. Anyway, glad to help :)
    – Jesse
    Commented Nov 7, 2016 at 14:09
  • @Erdem 15 reputation is the threshold for upvoting, you should still be able to accept the answer. emacs.stackexchange.com/help/someone-answers, emacs.stackexchange.com/help/privileges
    – npostavs
    Commented Nov 7, 2016 at 17:30
  • @npostavs after your post I recognized the accept answer button. But it looks faint so I couldn't recognize it until now.
    – user13916
    Commented Nov 7, 2016 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.