SirVer / ultisnips

UltiSnips - The ultimate snippet solution for Vim. Send pull requests to SirVer/ultisnips!
GNU General Public License v3.0
7.5k stars 687 forks source link

Ultsnips transformation not working as desired #1555

Open kontrapunktstefan opened 1 month ago

kontrapunktstefan commented 1 month ago

I have written the following snippet for UltiSnips in Vim. With this snippet I want to add an apostrophe to all lilypond pitches:

snippet 1 "lilypond pitches with apostophes"
$1
${1/\w+\s*/$0'/g}
endsnippet

When I execute it, however, the text appears twice:

c d e
c' d' e'

I just want the 2nd line

I've tried snippet 1 "lilypond pitches with apostophes"

snippet 1
${1/\w+\s*/$0'/g}
endsnippet

but this gives an error:

UltiSnips Error:

Tabstop 1 is not known but is used by a Transformation
SirVer commented 1 month ago

Hey, you cannot change what you typed yourself, that is a limitation of Vim, unfortunately. The best you can do is something like this:

snippet apo "lilypond pitches with apostophes"
`!p
lines = []
for idx, line in enumerate(snip.v.text.splitlines()):
    lines.append(" ".join(f"{a}'" for a in line.split()))
snip.rv = "\n".join(lines)
`
endsnippet

You use it like this:

  1. type out your line c d e
  2. Visual select the line (i.e. in normal mode, V)
  3. expand (not sure what your trigger is, maybe <tab>), this will remove the text
  4. type apo<tab> or whatever your expand trigger is
  5. output line is c' d' e'.

Hope that helps