Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape command in a vimscrimpt

Tags:

command

vim

Can anyone help me how to put an <esc> command in a vimscript in order to switch from a insert visual mode (selection mode) to the visual mode?

This is my script:

  if a:type == "'<,'>" 
    normal! "\<esc>\<esc>"
    normal gvy
    let myvariable= @+
  endif

explication:
I have selected a piece of text. (I'm now in insert visual mode (=selection mode))
I have to select the same text in visual mode.
Without vimscript I just have to push 2 times the <esc> command and the command gv in normal mode to reselect the selection again and this time I'm in visual mode.

I tried to put the <esc> commands as in above script but it doesn't work.

like image 797
Reman Avatar asked Sep 02 '25 16:09

Reman


1 Answers

You have completely wrong syntax for invocation of :normal.

normal! "\<Esc>\<Esc>"

will execute 14 characters in normal mode, none of them will be escape character: normal does not accept expressions. You have to use

execute "normal! \e\e"

(\<Esc> also works, but \e is faster to type). I see no reason in having two normal commands here so

execute "normal! \e\egvy"

. But I am still wondering what you do to invoke this script? Without this information the above is likely to prove being completely useless.

PS: I completely agree with @romainl that using easy mode and, more, using mouse in easy mode is completely wrong way of using vim.

like image 137
ZyX Avatar answered Sep 04 '25 07:09

ZyX