I would like to insert some Python-generated text at the current cursor position in insert mode using the Vim Python interface. The closest I have so far is:

imap <c-x><c-r> <c-o>:python vim.current.line += "trivial example"

This will append "trivial example" rather than put it at the current position. How can I insert the text at the cursor instead?


2 Answers 2


It is generally easier to use pyeval (py3eval, pyxeval) and the expression register:

imap <c-x><c-r> <c-r>=pyxeval('python expression')<cr>

if you want to use : py, you probably should use vim.command

function dosomething()
     :py import vim 
     :py vim.command("let tt ='text' ")
     return tt

and call it by <C-R>=dosomething()


 function dosomething()
     :py import vim 
     :py vim.command("norm i"+ 'TEXT' )

and call it by <esc>:call dosomething()

Your Answer

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

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