0

I want to create a snippet, which does the following: When I write . foo it creates A.B while the cursor is in front of B.

I only found ways to insert text after the key and to delete text before, but not to insert text before the key. I suspect this can be done by some Emacs lisp, similarly to deleting characters in front, but I failed to find out how. How can I obtain the described behavior?

The reason I want this is, because I want to create a snippet, which puts an expression into LaTex math mode, i.e. I write x and want it to be put into math mode \(x\) by the snippet.
The goal is to then execute other context-sensitive (i.e. only working in math mode) manipulations of the character inside the snipped. For example, when I type x⃗, then the snipped should turn it into \(\vec{x}\). In this case ⃗ is the key of the snipped. I already have a snipped which turns x⃗ into \vec{x}, and I only want it to also put it into maths mode. This is the reason, why I want a technique to put a x into \(x\) because my hope is, that then I am able to combine this with the snipped I already have to get the desired result.

3
  • If you don't mind selecting region, there is another small template package TempEl that can wrap region and there are also general templates for latex-mode.
    – roomworoof
    Jun 5, 2023 at 4:07
  • Could give an example including what commands you run? Like (I is the position of the cursor): Buffer content is xI, I use yas-expand to expand the snippet (is x actually the key for your snippet? or can this be a whole formula?) and it should give me \(xI\). This can most likely be achieved with a function as you can programmatically expand a snippet with elisp (yas-expand-snippet (yas-lookup-snippet "block" 'org-mode)). A function not using yasnippet is probably even easier to write.
    – Hubisan
    Jun 19, 2023 at 8:40
  • The buffer content is xI, where x is not the key of my snipped, but instead it should be any char. I've edited my question to include some more context to, why I want this.
    – Sinthoras
    Jun 23, 2023 at 20:26

1 Answer 1

0

Using $0, you can set a position within the snippet that the cursor will go to after the snippet is loaded to the buffer, for example

# -*- mode: snippet -*-
# name: math mode
# key: m
# --
\\($0\\)

Will output your desired \( and \) then let you type in between

enter image description here

1
  • Thanks, but this is not quite what I want. I was aware of this functionality, but I want to type a character first, then afterwards execute the snipped which then turns the character into math mode. This is especially useful, because my aim is to then execute other context-sensitive (i.e. only working in math mode) manipulations of the character inside the snipped.
    – Sinthoras
    Jun 7, 2023 at 15:52

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.