Is there a way of having snippet alias?

Asked by Jorge

Hi,

when typing equations in latex I don't always remember if the snippet for equation is
eqn or equ.
I don't have one of them as a snippet (equ) but it would be nice to have a way to add
the snippet definition of equ to be the same of eqn without typing the actual text again,
so If I later change the existing snippet the new one would change as well, something like

snippet equ = eqn
endsnippet

Is there a way of doing this right now?
Of course this is not necessary, but I guess it would be something nice to have.

Thanks.

Question information

Language:
English Edit question
Status:
Answered
For:
UltiSnips Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

Revision history for this message
SirVer (sirver) said :
#1

snippet "equ?" "Equation snippet" r
blahlbalh
endsnippet

See "r" (regular expression) option at the end of UltiSnips-adding-snippets section in the help.

Revision history for this message
Jorge (skeept) said :
#2

Sorry but I don't understand your answer.
Let me see if I can be more clear.

Suppose I define the snippet:

snippet eqn
\begin{equation}
$1
\end{equation}
endsnipppet

and now I would like to define a new snippet that would be expanded to exactly the same content of this one
but without having to type the text again, so if I would change the definition of the first snippet the definition for the second
would change also.

Of course I could do it manually, but I am just wondering if there is a way to do it.

thanks!

Revision history for this message
Jorge (skeept) said :
#3

one possible way would be maybe to have

snippet equ
eqn<C-R>=UltiSnips_ExpandSnippetOrJump()<CR>
endsnippet

but I am not sure this works, and it doesn't seem the nicest way to do it.

Revision history for this message
SirVer (sirver) said :
#4

Jorge, have you tried the snippet I posted? It works just as you intended. It uses a "regular expression" trigger (snippet option r), that is the trigger is interpreted as a regular expression and then expanded. I pointed you to the documentation, please read this section in its entirety to understand.

http://bazaar.launchpad.net/~sirver/ultisnips/trunk/view/head:/doc/UltiSnips.txt#L251

Revision history for this message
Jorge (skeept) said :
#5

Sorry, I still don't understand if what I want can be done.
Your example was

snippet "equ?" "Equation snippet" r
 blahlbalh
 endsnippet

I understand what it does but I don't think it is what I was asking, because in this example
I would still have to define the snippet again (copying the text from the place where you defined it),
but I was just asking if there is a way of saying
"define snippet equ in such a way that it is equivalent to the existing snippet eqn"

So if later you discovered that there is a bug in your snippet and change it, I wouldn't have to update anything since my snippet would inherit the text from your snippet.

From reading the documentation it doesn't seem that it is possible, I was just wondering.

Revision history for this message
SirVer (sirver) said :
#6

There is no way to assign another trigger to one of the shipped snippets, that is correct. You can only assign multiple triggers to a snippet that you define anew.

Revision history for this message
Jorge (skeept) said :
#7

thanks for your answer.
I was just wondering if that would be possible.
What I do now is the following

snippet equ
eqn
endsnippet

and then if I expand equ it expands to eqn which I then expand to whatever eqn expands too.

It would be nice to do it all in one pass, but it is not a big deal.

Thanks again,

Best regards.

Revision history for this message
SirVer (sirver) said :
#8

you could use an imap of course.

Revision history for this message
Jorge (skeept) said :
#9

I don't think imap is a good solution, snippets are much better :)
Let me ask you the following,

In the tex file if I type
eqn<c-r>=UltiSnips_ExpandSnippet()<cr>

then I get the snippet expanded to whatever eqn expands to (here
<c-r> means hitting ctrl+r and <cr> means pressing Enter).

But if I define the following snippet:

snippet equ
eqn<c-r>=UltiSnips_ExpandSnippet()<cr>
endsnippet

when I expand the snippet it just gets the text literally inserted,
not the value of the snippet.
Is there a way of expanding to whatever eqn expands to, instead of inserting the text?

Thanks

Revision history for this message
SirVer (sirver) said :
#10

No, that would be a feature overlap with mappings, something that I want to avoid. There is an evilish hack to achieve it though

snippet equ
eq`!p vim.command('call feedkeys("\<tab>")')`
endsnippet

the feedkeys call will only be executed by vim after all commands from ultisnips are run therefore immediately expanding the eq snippet.

Revision history for this message
voyeg3r (voyeg3r) said :
#11

I think this answers the initial question
group each option with "(option)" and use the optional "|"

snippet "(eqn)|(equ)" "Equation snippet" r
blalbalh
endsnippet""

Revision history for this message
voyeg3r (voyeg3r) said :
#12

I think this answers the initial question
group each option with "(option)" and use the optional "|"

snippet "(eqn)|(equ)" "Equation snippet" r
blalbalh
endsnippet""

Can you help with this problem?

Provide an answer of your own, or ask Jorge for more information if necessary.

To post a message you must log in.