1

So I have been working with vim-tex and ultisnips lately.
Let us say the snippet is of the form (\d)pder. It is a 5 character snippet and the first character can be accessed by match.group(1).

But now I want to access substrings of the snippet with unspecified number of characters using certain pattern. For example, if pderxaybzc I want to extract the substring xaybzc by removing pder, so that each character of the output substring can be used within the snippet definition in different places.
I have tried looking for solutions and haven't found any.

Is this feature present in Ultisnips and can the above mentioned be done?

(Thank you in advance)

3
  • 1
    Welcome to Vi and Vim! I wonder if extending the snippet like (\d)pder(\w*) would work?
    – D. Ben Knoble
    Jan 24, 2021 at 14:40
  • It perfectly works! Thank you! Jan 24, 2021 at 14:51
  • Feel free to self-answer :) answers belong in Answers, not comments, but you know better than I what worked
    – D. Ben Knoble
    Jan 24, 2021 at 14:52

2 Answers 2

2

The following snippet worked out for me:

snippet `pder(\w*)` "Description" rw
\\frac{\\partial^{`!p snip.rv=findSum(match.group(1))`}}{`!p snip.rv=printd("\\partial",match.group(1))`} $0
endsnippet

with

global !p
def findSum(str1): 
    # A temporary str1ing 
    temp = 0
  
    # holds sum of all numbers 
    # present in the str1ing 
    Sum = 0
    icount=0
    # read each character in input string 
    for ch in str1: 
  
        # if current character is a digit 
        if (ch.isdigit()): 
            Sum += int(ch) 
            icount=icount+1
    Sum=Sum+len(str1)-2*icount
    # atoi(temp.c_str1()) takes care 
    # of trailing numbers 
    return Sum

endglobal

I should at that * here would mean that, in the group () it would check for the zero,single or multiple instance of the previous occurring character. So here it checks for multiple alphanumeric, which is just a string.

Also one can use + in place of * if they want at least one character to be present for the expansion of snippet. Like (\w+) would require the snippet to have at least one character to expand.

Edit: The limitation of the code is that the derivative can have only natural number powers of 1 digit.

1

Simply extend the snippet to (\d)pder(\w*) and handle the second submatch appropriately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.