7

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.

0

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I was going to ask you, after your comment, why I had to use execute for it to work. I see you were way ahead of me :)
For anyone interested, you can find the doc on escaping special keys using \ on :h expr-quote inside Vim.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.