3

I want to make a Ultisnips snippet for auto completion of (), "", {}, etc. I have Tab as my expand and jump forward key. My snippet is:

snippet ( "parenthesis" iA
(${1:${VISUAL}})$0
endsnippet

And it works great most of the times, but I sometimes have to write parenthesis or quotes without any text.

In that case I type ( and then it expands as (), but when I want to go out of the parenthesis by pressing Tab, it expands the snippet again and I'm left with ()).

I have no idea how to change this based on the documentation; is there an option to configure this behavior? Or how should I configure this snippet?

6
  • 1
    Welcome to Vi and Vim! When you want to go you out of the parenthesis, do you type ) or some other combination? With the $0 placeholder, I would expect you to be able to jump out using whatever keys jump between placeholders.
    – D. Ben Knoble
    Commented Feb 11, 2021 at 17:28
  • So sorry, i did put in in the question, but it didn't appear. I use tab for expanding and jumping forward. I think this is the problem because ultisnips triggers the snippet instead of going to $0 , but it is really convenient for me and i would like to know if there is a way to continue using it without changing the trigger key.
    – jfab
    Commented Feb 11, 2021 at 17:39
  • Ah, the duplication there could be an issue indeed.
    – D. Ben Knoble
    Commented Feb 11, 2021 at 17:46
  • Maybe there is a way to configure the snippet so that it only expands when there is no ) right next to (. Could I implement that?
    – jfab
    Commented Feb 11, 2021 at 18:03
  • If regex is allowed in the snippet, you could probably get to a negative lookahead
    – D. Ben Knoble
    Commented Feb 11, 2021 at 18:13

1 Answer 1

4

I got it to work:

    global !p
    def mycontext(char):
        x = vim.current.window.buffer
        y = vim.current.window.cursor[0]
        z = vim.current.window.cursor[1]
        try:
            return x[y-1][z-1] + x[y-1][z] != char 
        except:
            return True 
    endglobal
    
    context "mycontext('()')"
    snippet ( "parenthesis" iA
    (${1:${VISUAL}})$0
    endsnippet
3
  • Are you by chance jfab, the OP? You should be able to use the same account if you wish.
    – D. Ben Knoble
    Commented Feb 12, 2021 at 15:14
  • I entered as a guest and now I cannot access the other account haha, sorry.
    – jfab
    Commented Feb 12, 2021 at 15:34
  • I'm pretty sure you can merge the accounts, if you wish. vi.stackexchange.com/help/merging-accounts
    – D. Ben Knoble
    Commented Feb 12, 2021 at 16:24

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.