It's too troublesome to display Japanese with Vim's python3.

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

It's too troublesome to display Japanese with Vim's python3.
How to display python Japanese with lolipop
How to enter Japanese with Python curses
[Python] How to handle Japanese characters with openCV
Python> function> Docstrings> Description to display with help () / .__ doc__
Connect to BigQuery with Python
Send Japanese email with Python3
Connect to Wikipedia with Python
Japanese morphological analysis with Python
Switch python to 2.7 with alternatives
Write to csv with Python
It's too easy to use an existing database with Django
I tried various methods to send Japanese mail with Python
Try to display various information useful for debugging with python
[Ev3dev] How to display bmp image on LCD with python
It's Halloween so I'll try to hide it with Python
Leave the troublesome processing to Python
How to not escape Japanese when dealing with json in python
Python: How to use async with
Link to get started with python
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
Output to csv file with Python
Add Python 2.7 Japanese documentation to Dash.app
MP3 to WAV conversion with Python
To do tail recursion with Python2
How to get started with Python
What to do with PYTHON release?
Unable to install Python with pyenv
Try to display google map and geospatial information authority map with python
How to use FTP with Python
Throw something to Kinesis with python and make sure it's in
Speak Japanese text with OpenJTalk + python
How to calculate date with python
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
Easily post to twitter with Python 3
I want to debug with Python
How to handle Japanese in Python
[It's not too late to learn Python from 2020] Part 3 Python Language Basic (1)
How to display legend marks in one with Python 2D plot
(Mac) How to display Japanese with Matplotlib and Seaborn At MacOS Sierra
It's more recent, but I wanted to do BMI calculation with python.
[Python] Calendar-style heat map (with holiday display)
Calculate and display standard weight with python
Try to reproduce color film with Python
Try logging in to qiita with Python
Convert memo at once with Python 2to3
Memo to ask for KPI with python
Output color characters to pretty with python
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Output Python log to console with GAE
Display Python 3 in the browser with MAMP
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Fractal to make and play with Python
Download Japanese stock price data with python
Connect to MySQL with Python within Docker
How to work with BigQuery in Python
Notes on doing Japanese OCR with Python