Here is a Python list that I'm trying to assign to some Vim variable.

lines = ['"""Something foo\'s thing."""', '', 'foo = "asdfasdf" \\', 'bar']
command = 'let l:corrected_lines = {lines}'.format(lines=lines)

And then in a caller function, I use l:corrected_lines to do some extra work.

The problem is that there are 2 issues with the string above

  • The single quote foo's apparently needs to be escaped
  • The \ also needs to be escaped

Apparently Vim escapes single-quotes by placing two of them side-by-side, like this ''. So I've experimented with running replace("\\'", "''") and other combinations of methods. Also Vim changes the \ in the source-code to \\ so I've tried adding replace('\\\\', '\\') and that works. The problem is that these replacements work in isolation but don't work together.

I feel like there's probably a simpler way to escape this line but I haven't had success with Python or using Vim's :escape() command. Can someone please suggest a solution?

Note: Just to make lines make a bit more sense visually, this is how lines looks like printed out

"""Something foo's thing."""

foo = "asdfasdf" \

1 Answer 1


Use pyeval(), py3eval(), or pyxeval() to read python variables into vim:


x = [1,2,3]
vim.command('let X = pyeval("x")')


py x = [1,2,3]
let X = pyeval('x')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.