3

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 Answers2

4

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

imap <c-x><c-r> <c-r>=pyxeval('python expression')<cr>
Mass
  • 14,080
  • 1
  • 22
  • 47
1

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
endfunction

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

or

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

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

eyal karni
  • 1,106
  • 9
  • 33