Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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?

like image 627
Peeja Avatar asked Feb 22 '11 21:02

Peeja


People also ask

How do I use visual mode in Vim?

To get into the Vim Visual Line mode, press “Shift+V” while you are in Vim's Normal mode.

How do I select a visual block in vim?

Press v to begin character-based visual selection, or V to select whole lines, or Ctrl-v or Ctrl-q to select a block. Move the cursor to the end of the text to be cut/copied. While selecting text, you can perform searches and other advanced movement. Press d (delete) to cut, or y (yank) to copy.

How do you go into visual mode?

Entering visual mode from insert mode In insert mode, you can execute a normal mode command with Ctrl-o . While you are in this normal-mode-ready, run visual mode key v . If you notice, on the bottom left, it will say --(insert) VISUAL-- .


1 Answers

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

like image 161
jamessan Avatar answered Nov 15 '22 08:11

jamessan