Note: The following content has only been confirmed with Kaori Ya version Vim 7.3.865 (2013/03/17 version).
In Vim's Python 3 interface, if you want the string to appear on the first line of the buffer, the string "abcde"
py3 text = "abcde"
py3 import vim
py3 vim.eval('setline(1, "{}")'.format(text))
Is fine, but if you want to display a Japanese character string, for example, "aiueo",
py3 text = "\u3042\u3044\u3046\u3048\u304A" # == "AIUEO"
py3 import vim
py3 vim_encoding = vim.eval("&encoding")
py3 vim.eval('setline(1, "{}")'.format(''.join([r"\x{:02x}".format(x) for x in text.encode(vim_encoding)])))
I had to. It's a hassle.
The reason why it takes so much time to display Japanese is due to the specification of vim.eval () of the Python 3 interface.
The Python 3 interface vim.eval () only accepts a Python string as an argument, but it is a mystery specification that encodes that string in UTF-8 and then decodes it in Vim's encoding. It is.
Therefore, if you want to pass a Japanese string, encode the Python string (eg "A") with Vim encoding (eg b'\ x82 \ xa0') and then hexadecimally escape the string. (Example:'\\ x82 \\ xa0'), then convert it to a double-quoted string and pass it to vim.eval () (Example:'setline (1, "\\ x82 \") I had to take the trouble of \ xa0 ")').
Perhaps there is an easier way, so if anyone knows, please let me know. m (__) m
Recommended Posts