2

I have following yas-snippet (for python-mode):

# -*- mode: snippet -*-
# name: .s-np_array_shape
# key: .s
# --
.shape

And now, .s Tab gives .shape as expected.

What I want:

This doesn't expand if there is another character right before .s.

In other words, I want to expand this snippet from a.s Tab to a.shape.

Is it possible to achieve this?


Update:

  • I found same question asked some years ago link: No exact answer I'm looking for.
  • also found this issue on github. Some people were working on expanding with regex, but it seems like abandoned.
1

1 Answer 1

3

EDIT in response to comment

In order not to break your 'multi-entry snippets', you could create a separate command to expand keys starting with a dot using the extended yas-key-syntaxes list.

For that, do not add the #'yas-try-key-from-dot to yas-key-syntaxes using add-to-list, but instead set the yas-key-syntaxes locally within your custom command as follows:

(defun my-yas-expand ()
  (interactive)
  (let ((yas-key-syntaxes (append yas-key-syntaxes '(yas-try-key-from-dot))))
    (yas-expand)))

(so you should still use/evaluate yas-try-key-from-dot).

Now, you can use M-x my-yas-expand for expanding keys that start with a dot (and you can create an 'alternative' keybinding for it).

END EDIT

Is it really necessary to use the dot in the key (i.e. are you already using the key s without the dot)? Otherwise, it might be easier to just type the dot and then expand 'shape'. Then expansion should already work when the trigger key is right after the dot.

If the dot really needs to be included in the key, then you could add a custom function to yas-key-syntaxes as follows (the following function is inspired by yas-try-key-from-whitespace):

(defun yas-try-key-from-dot (_start-point)
  "As `yas-key-syntaxes' element, look for dot delimited key."
  (skip-chars-backward "^.")
  (backward-char))

(add-to-list 'yas-key-syntaxes #'yas-try-key-from-dot t)
10
  • 2
    Shouldn't it be add-to-list? append won't update yas-key-syntaxes Feb 23, 2023 at 11:38
  • 1
    Thanks for the suggestion (we could use add-to-list, but we should make sure to add it to the end of the list). However, I deleted that original answer immediately after creating it. But to rewrite the answer, I had to undelete that old answer first, and you could see it while I was writing the corrected answer (I consider that a SE bug). So you were writing the comment while I was correcting the answer. Anyway, that answer was wrong, and the string w is already in that list by default. Feb 23, 2023 at 11:50
  • 2
    yes I saw it updated a few seconds after I commented (: , does add-to-list not have an append flag (and wouldn't add it if it was already present - though not that helpful in this situation) ? Anyway, the answer works a treat a for me, thanks. Feb 23, 2023 at 11:57
  • 2
    Yes, you're right, and indeed it is nicer and probably a better option to use here. I have updated the answer. Feb 23, 2023 at 12:08
  • Thanks for the answer, I have zero knowledge on e-lisp. So this is very helpful. This works for .s type snippets. However, after loading this code some of my multi-entry snippets working not correctly. In details, yas-next-field no longer jumps to next field.
    – Garid
    Feb 23, 2023 at 12:45

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.