Examples of anonymous snippets?

Asked by Jorge

Hi is it possible to get more examples on how anonymous snippets work?

What I am trying to do is to use in latex so that when I type ^^ I would get ^{$1}$0
I tried to do the same for the $$ with the example you provide. So from

inoremap <silent> $$ $$<C-R>=UltiSnips_Anon(':latex:\`$1\`', '$$')<cr>

I change it to

inoremap <silent> $$ $$<C-R>=UltiSnips_Anon('$$1$', '$$')<cr>

and it seems to work, but

inoremap <silent> ^^ ^^<C-R>=UltiSnips_Anon('^{$1}', '^^')<cr>

gives me two ^^ when I type two ^^ in insert mode.

Could you give an example on how to make this work?

Thanks,

Jorge

Question information

Language:
English Edit question
Status:
Answered
For:
UltiSnips Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
SirVer (sirver) said :
#1

The problem here is not the Anyonmity of the snippet, but that ^^ is not a word (as defined by vims in-word characters for latex). To match this as a snippet, you need to use a regular expression trigger:

snippet "\^\^" "Hello" r
Hello world
endsnippet

Or for your example:

inoremap <silent> ^^ ^^<C-R>=UltiSnips_Anon('^{$1}', '\^\^', "name of snippet", "r")<cr>

Note that this does not quite work on my setup, because ^ expects a second character to be entered to make for example â ô û. This confuses Vim and/or UltiSnips so your snippet does just not quite work. It might work under Linux/Win though.

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

Thank you Sirver, that works.

I have a related question. The reason I want to use these snippets instead of just defining the imaps directly
is that if I use the snippets I can use <c-j> to jump out of the snippets.

The current snippets I have are:

inoremap <silent> $$ $$<C-R>=UltiSnips_Anon('$$1$$0', '$$')<cr>
inoremap <silent> ^^ ^^<C-R>=UltiSnips_Anon('^{$1}$0', '\^\^', 'hat', "r")<cr> inoremap <silent> __ __<C-R>=UltiSnips_Anon('_{$1}$0', '__', 'unders', "w")<cr>

If I use any of these by them selves it works fine, so I type $$ I get the cursor inside the $$ and I can type text and hit <c-j> to jump outside, but if inside the $$ I use one of the other snippets say __ and then hit <c-j> it will jump outside the inner snippet but then hitting <c-j> again will not move the cursor.

This does not happen for the regular snippets, do you know if there is a way of making it work for these kind of snippets?

Thanks!

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

Unfortunately no. I have the same problem with my own Latex snippets. I am not sure how/why Vim handles the noremap keys differently internally than the typed keys. However, I was unable to track this problem down to its root. If you find the reason for this strange behavior, be sure to let me know.

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

Hi,

I tried asking this on the vim_use list but one one has a clue up to this point:
https://groups.google.com/forum/?fromgroups=#!topic/vim_use/rG8L9eJ_eXU

Also I would like to know there exists or it is possible to set a variable with with an int depending on the sucess of the function UltiSnips_ExpandSnippetOrJump, say g:ultisnip_expand_or_jump_res

with the following:
did nothing -> 0
expanded -> 1
jumped -> 2

this would allow to write a mapping (or a function) that depending on the success of this function would do something else.

Thanks.

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

Hi,

>I tried asking this on the vim_use list but one one has a clue up to this point:
>https://groups.google.com/forum/?fromgroups=#!topic/vim_use/rG8L9eJ_eXU
Cool, thanks for caring!

>Also I would like to know there exists or it is possible to set a
>variable with with an int depending on the sucess of the function
>UltiSnips_ExpandSnippetOrJump, say g:ultisnip_expand_or_jump_res
Unfortunately not currently. Such a thing would be easy to implement
in UltiSnips/__init_.py though. Patches are welcome!

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

I tried creating a patch to add these global variables that contain
the status of the called function. Please include it if it makes sense
or let me know if there is something I can do
to improve it.

Best
Jorge

On Mon, Sep 3, 2012 at 1:31 PM, SirVer
<email address hidden> wrote:
> Your question #206555 on UltiSnips changed:
> https://answers.launchpad.net/ultisnips/+question/206555
>
> SirVer proposed the following answer:
> Hi,
>
>>I tried asking this on the vim_use list but one one has a clue up to this point:
>>https://groups.google.com/forum/?fromgroups=#!topic/vim_use/rG8L9eJ_eXU
> Cool, thanks for caring!
>
>>Also I would like to know there exists or it is possible to set a
>>variable with with an int depending on the sucess of the function
>>UltiSnips_ExpandSnippetOrJump, say g:ultisnip_expand_or_jump_res
> Unfortunately not currently. Such a thing would be easy to implement
> in UltiSnips/__init_.py though. Patches are welcome!
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
> https://answers.launchpad.net/ultisnips/+question/206555/+confirm?answer_id=4
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https://answers.launchpad.net/ultisnips/+question/206555
>
> You received this question notification because you asked the question.

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

I just merged your patch. Excellent work, thank you!

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

Thanks for merging. I cannot pull the patch from bzr yet, but it seems
to be in github...
Is it better to use github from now on?

On Thu, Sep 6, 2012 at 1:45 PM, SirVer
<email address hidden> wrote:
> Your question #206555 on UltiSnips changed:
> https://answers.launchpad.net/ultisnips/+question/206555
>
> Status: Open => Answered
>
> SirVer proposed the following answer:
> I just merged your patch. Excellent work, thank you!
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
> https://answers.launchpad.net/ultisnips/+question/206555/+confirm?answer_id=6
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https://answers.launchpad.net/ultisnips/+question/206555
>
> You received this question notification because you asked the question.

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

No, both are equal. I only prefer the git work flow that is why it lands on github first currently. The branches are synced every 4 hours, so it should be in bzr by now as well.

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.