Peeja
Peeja

Reputation: 14264

How do I execute Visual mode commands from a Vim function?

I have a function which takes a string of commands to execute and makes sure 'paste' is on before it runs them. What I'm looking for is akin to the following:

vmap <silent> <C-K> :<C-U>call InPasteMode("<Plug>ReplaceVisual")<CR>
function! InPasteMode(command)
  let oldpaste = &l:paste
  try
    set paste
    execute "normal" a:command
  finally
    let &l:paste = oldpaste
  endtry
endfunction

but the command, "<Plug>ReplaceVisual", needs to run in Visual mode, not Normal mode.

Is there a command like :normal which runs keystrokes in Visual mode?

Upvotes: 9

Views: 2671

Answers (1)

jamessan
jamessan

Reputation: 42667

gv restores the last visual selection. So, something like execute "normal gv" . a:command should work.

Upvotes: 14

Related Questions