user621819
user621819

Reputation:

VIM: vim.command('y') works as expected but vim.command('gv') starts the GUI instead of visual select

function! Cut()
python3 << EOF
import vim

cw = vim.current.window
pos = cw.cursor
cr = vim.current.range
vim.command('y')
vim.eval('gv')
#print(cr)

EOF
endfunction

I select a bunch of lines manually. Then type M-x to cut. The above function is invoked. I run 'y' to yank lines, now i need to reselect 'gv' and then 'd' delete. Unfortunately, it's doing :gv vs plain-old gv - is there some other vim.command that I can use?

Upvotes: 0

Views: 54

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172598

It is important to observe the modes those commands apply. gv is a normal mode command, but vim.command() takes an Ex command (from command-line mode), as per the :help python-command:

vim.command(str)
        Executes the vim (ex-mode) command str.  Returns None.

So, your y is interpreted as :yank, and gv as :gvim, explaining the behavior you're seeing.

Fortunately, there's an Ex command to run normal mode commands, the aptly named :normal. So, the solution is to use

vim.command('normal! gv')

(The ! avoids considering mappings and is recommended.)

Upvotes: 1

user621819
user621819

Reputation:

vim.command(':normal! gv') (thanks to tek0 on Freenode #vim IRC)

Upvotes: 0

Related Questions