Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I manually execute a key combo from a Vim script file?

Tags:

vim

Say I want to run the key combo <C-e> five times from a Vim script. Is that possible? If so, how is it done?


Specifically, I need this because I want to map a key to nudge the viewport one fourth of the screen height in a direction. For that I need to combine the function winheight(".") with the key combo <C-e> somehow.

Note: I know I can change the option 'scroll' to set the number of lines <C-u> and <C-d> scroll, but that key combo also moves the cursor. Additionally, I don't know how to set the 'scroll' option to scroll a portion of the window height, except that it scrolls half the window height when I set it to 0.

like image 961
Hubro Avatar asked Jan 16 '13 00:01

Hubro


1 Answers

For simple commands, :norm[al][!] is sufficient. For example, to yank a line:

norm! yy

For special characters, use :exe[cute] "norm[al][!]". For example, to do 5<C-e>:

exe "norm! 5\<C-e>"

With exe, other code can be inserted by using multiple arguments:

exe "norm!" winheight(".")/4 "\<C-e>"

However, arguments are joined with spaces, which are then interpreted literally. To avoid this, use . to join arguments. Thus, for the desired effect:

exe "norm!" winheight(".")/4 . "\<C-e>"

See :help norm and :help exe for more information.

like image 109
Nikita Kouevda Avatar answered Oct 10 '22 17:10

Nikita Kouevda