简体   繁体   中英

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

Say I want to run the key combo <Ce> 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 <Ce> somehow.

Note: I know I can change the option 'scroll' to set the number of lines <Cu> and <Cd> 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.

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<Ce> :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM