Impossible to pass non-UTF-8 strings from vim to Python 3
Author: mgedminCreated Sep 10, 2016Updated Oct 1, 2026
Labelsif_python
When a vim variable has a value that is not a valid UTF-8 string, :py3 vim.eval('variable') raises UnicodeDecodeError.
This is causing plugins such as UltiSnips to crash when they try to process a list of mappings that involve <A-@>, since Vim thinks <A-@> is byte 0xC0. Here's what happens in the plugin:
- it runs
vim.command('redir => _tmp_smaps | smap | redir END')to get the mappings - it tries to parse
vim.eval('_tmp_smaps').splitlines()to see what select-mode mappings there are
Now I've the following mapping in my .vimrc
map! <Esc>@ <A-@>(I've many more, but this one seems to be the one causing the problem)
When I run smap, vim shows this mapping as

but when I do
:redir => tmp | smap <Esc>@ | redir END
:echo tmpI see

and I can reproduce the crash with
:py3 import vim; vim.eval('tmp')Traceback (most recent call last):
File "<string>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 18: invalid start byteSource: vim/vim