0

I am trying to remap <PageUp> key to call this function:

function! PageUp()
    let l:line = line('.')
    if(l:line != 1)
        if(l:line != winline())
            :set syntax=off
            <PageUp>
            :set syntax=on
        else
            normal! 1G
        endif
    endif
endfunction

but gvim complains that I cannot do it.

So what is the proper way to use the keys in a function ?

3

First of all, you use the PageUp key in Normal mode so the underlying functionality is a Normal mode command. You can't use such commands directly while in an Ex command-line or function/script. You need to use the Ex command :norm for this.

Further, to use "key codes" (:h key-codes) of non-printing characters like <PageUp> you need to construct the :norm command as an expression and pass it to the :exe command (see last paragraph of :h :norm).

That gives us:

:exe "norm \<PageUp>"

The double quotes are required as is the \ to escape the keycode and indicate that you want the special meaning not the literal string "<PageUp>".

(Note: Usually we want to use norm! instead of norm in order to avoid conflicts with mappings but that's not a critical element of this answer so omitted.)

1

To send special key to :normal, you need to get it's raw code by using "\<key>" notation.

exec "norm! \<PageUp>"

You can also use raw code directly if you want(not recommended, hard to read):

norm! <80>kP
        ^----------<80> is 0x80

<80>kP is raw code of <PageUp>, you can enter it like this in normal mode:

"="\<PageUp>"<cr>p
              ^----------press carriage return

If raw code of the key doesn't start with 0x80, it's the same as terminal code (not sure), you can use the :h i_CTRL-V to insert terminal code:

norm! <c-v><c-p>
       ^------------press ctrl-v ctrl-p
  • :h :exec
  • :h quote=
  • :h string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.