This question is very basic, but somehow absurdly difficult to find an answer for, because everything you find explains how to save the buffer, and I'm not interested in making new buffers or saving the current buffer.

I had been using (for years!) some pretty ugly code that requires vim to be built with python in order to work:

    if has('python')
        python << EOF
from os.path import expanduser
f = open('/path/to/file', 'w')
# print f
w = vim.eval("varname")

That snippet uses the simple-to-use python file api to write the contents of vim variable varname into /path/to/file.

1 Answer 1


writefile() is the ticket, but has some quirks you have to account for, such as splitting on newlines.

An example:

call writefile(split(varname, "\n", 1), glob('/path/to/file'), 'b')

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.