Vim Tips Wiki

Python script utf8 encoding

1,610pages on
this wiki
Revision as of 16:54, March 17, 2013 by (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

When doing python scripts with vim, one can encounter really easily the following error, when giving utf-8 encoded strings from python to vim:

:py print u'echo "\u2026"'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in
position 6: ordinal not in range(128) 

From a vim mailing list's post, a solution exists that just solves this issue:

python << EOS
uniStr = u"\u2026"
str = uniStr.encode( vim.eval("&encoding") )
print str

or the following one liner:

:py print u"\u2026".encode( vim.eval("&encoding") )

Don't hesitate reading the whole thread to understand better how vim deals with encoding, and also read :he encoding

Advertisement | Your ad here

Around Wikia's network

Random Wiki