Expand with key in middle of word (no space)

37 views
Skip to first unread message

quwa...@gmail.com

unread,
Oct 17, 2017, 11:12:35 AM10/17/17
to YASnippet Discussion Group
Hi,

I have a question (similar to this thread, which did not get any repsonse):

How can I achieve snippet expansion even if the trigger-key is in the middle of a word (no space)?

For example, let's say that I have a snippet for zah with the key #bar. If I then type foo#bar and hit TAB, I want the result to be foozah.

I am looking for exactly this behavior, not typing foo #bar and the snippet then erasing the space upon expansion.

Currently, I can only expand the snippets if I put them as separate words, i.e. foo #bar to foo zah.

Is there any way to write a snippet that does this or any other type of workaround to achieve what I want?

Thank you

Anders Eurenius

unread,
Oct 23, 2017, 5:11:45 AM10/23/17
to YASnippet Discussion Group
Interesting question. Maybe another trick worth trying might be a (.*) prefix that you just put back in the expansion? So foo#bar matches (.*)#bar and expanded to foo#bar, which the behaviour you wanted, right?

--
You received this message because you are subscribed to the Google Groups "YASnippet Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to smart-snippet+unsubscribe@googlegroups.com.
To post to this group, send email to smart-...@googlegroups.com.
Visit this group at https://groups.google.com/group/smart-snippet.
For more options, visit https://groups.google.com/d/optout.

quwa...@gmail.com

unread,
Oct 23, 2017, 2:43:05 PM10/23/17
to YASnippet Discussion Group
Thanks for the reply, this sounds intriguing. I tried experimenting a bit with it, but did not get it to work. If possible, could you post a simple snippet example implementing your idea?

Note that what I am looking for is to expand with the trigger key (TAB) as usual, and the #bar working regardless of what precedes it, i.e.

foo#bar + press TAB = foozah

abcde#bar + press TAB = abcdezah

www#bar + press TAB = wwwzah

and so on. Would the (.*) prefix method be able to render this?

On Monday, October 23, 2017 at 11:11:45 AM UTC+2, aes wrote:
Interesting question. Maybe another trick worth trying might be a (.*) prefix that you just put back in the expansion? So foo#bar matches (.*)#bar and expanded to foo#bar, which the behaviour you wanted, right?
On Tue, Oct 17, 2017 at 4:40 PM, <quwa...@gmail.com> wrote:
Hi,

I have a question (similar to this thread, which did not get any repsonse):

How can I achieve snippet expansion even if the trigger-key is in the middle of a word (no space)?

For example, let's say that I have a snippet for zah with the key #bar. If I then type foo#bar and hit TAB, I want the result to be foozah.

I am looking for exactly this behavior, not typing foo #bar and the snippet then erasing the space upon expansion.

Currently, I can only expand the snippets if I put them as separate words, i.e. foo #bar to foo zah.

Is there any way to write a snippet that does this or any other type of workaround to achieve what I want?

Thank you

--
You received this message because you are subscribed to the Google Groups "YASnippet Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to smart-snippe...@googlegroups.com.

Anders Eurenius

unread,
Oct 24, 2017, 2:48:00 AM10/24/17
to YASnippet Discussion Group
Hah, nope, I think I've confused yasnippet with something else. I think this might what you're looking for, though: https://github.com/joaotavora/yasnippet/blob/master/yasnippet.el#L411 . It's a customization to specify how to find candidate trigger words. If I understand correctly, prefixing this with something like the lambda below might do it. You may need to fiddle with the second syntax class set to make sure it covers "#" in the language you're working with.

(lambda ()
  (if (< (skip-syntax-backward "w") 0)
      (skip-syntax-backward "_'")))

The reason you have to have a function/lambda and not simply specify "w_'" is of course that it would skip "foo", too. This first moves over the "bar" word chars, and then the "#" char in two separate steps.

​See also:

Reply all
Reply to author
Forward
0 new messages