snippets with unicode characters

Asked by Jakub Mikołajczyk

I'm trying to write my own snippets using python. I copied this code from help and modified one line:

global !p
def upper_right(inp):
 return (75 - 2 * len(inp))*' ' + inp.replace(u"ł", u"x")
endglobal

snippet wow
${1:Text}`!p snip.rv = upper_right(t[1])`
endsnippet

I want to replace inputted unicode character 'ł' (\u0142) with 'x'. But the replacement won't happen. It won't work with any other non-ascii character however when I use regular ascii character it works flawlessly.

First I thought it may be a python problem but that code works also in python shell. Am I doing something wrong?

Question information

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

Seems like an encoding issue. The snippet files should be utf-8, but maybe someplace something goes awry. However, the following works for me:

global !p
def upper_right(inp):
 rv = (75 - 2 * len(inp))*' ' + inp.replace(u"\u0142", u"x")
 print "%r" % rv
 return rv
endglobal

snippet wow
${1:Text}`!p snip.rv = upper_right(t[1])`
endsnippet

I replaced the literal unicode char with a unicode escape sequence that python understands and everything is working out.

Revision history for this message
Jakub Mikołajczyk (kmikolaj) said :
#2

Thanks SirVer, that solved my question.