UltiSnips shell/python interpolation and ${VISUAL}

225 Views Asked by At

I really can not figure how to do this.

I am trying to create a snippet that will read a file (the name I will get it by selecting it so it will be in the ${VISUAL}) and then using grep extract a line from it.

What I tried is something like

!v cat ${VISUAL}  | grep "some text"

but of course that in shell ${VISUAL} has a different value.

I tried something in python, but I am lost. All I could get was

!p snip.rv=snip.v.text

that will give me the value for ${VISUAL}

Thank you for your help.

2

There are 2 best solutions below

0
On

Maybe for your specific case you could use the vim builtin :read function ?

0
On

It's not quite grep, but if you're okay with Python-style regex, you can do this:

`!p 
import re
from pathlib import Path
path = Path(snip.v.text)
content = path.read_text()
snip.rv = '\n'.join(re.findall(r'foo.*bar', content))
`