Vim: Map ctrl+pgup and ctrl+pgdn (CTRL+Page Up/Down) key combinations
Asked Answered
L

2

8

I'm trying to map Vim commands to the ctrl+pgup and ctrl+pgdn key combinations. The vim syntax for these keys does not work (i.e., <PageUp> and <PageDown>, or <C-PageUp> and <C-PageDown>).

Since the default vim syntax doesn't work, I'm guessing that the Terminal isn't sending the character codes for ctrl+pgup and ctrl+pgdn which Vim is expecting. If that's true, I'm not sure how to find out what the literal key codes are. I'm using xfce4-terminal on Arch Linux.

Here's what I've tried:

  1. The usual method:

    map <C-PageUp> :bp<cr>
    
  2. Setting it from the command line with this answer's method: Why <C-PageUp> and <C-PageDown> not work in vim?

    :map <CTRL-V><CTRL-PAGEUP> :bp<cr>
    

    When I type the command above in the command line, nothing shows:

    map  :bp<cr>
    

    And Vim says No mapping found.

  3. This method from the Vim wiki: http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-Tutorial(Part_2)#Key_notation

    set <PageUp>=<type Ctrl-V><type PageUp> "^[[5~
    map <C-PageUp> :bp<cr>
    
  4. A modification of that answer:

    set <C-PageUp>=<type Ctrl-V><type Ctrl+PageUp> "^M
    map <C-PageUp> :bp<cr>
    
  5. After realizing that <type *> was a user command, I did these commands, and vim pasted output. I replaced <type *> with this output:

    set <PageUp>=^[[5~
    map <C-PageUp> :bp<cr>
    " Also:
    set <PageUp>=[[5~
    map <C-PageUp> :bp<cr>
    " Also:
    set <PageUp>=<^[[5~>
    map <C-PageUp> :bp<cr>
    " Also:
    set <PageUp>=<[[5~>
    map <C-PageUp> :bp<cr>
    " Also:
    set <C-PageUp>=^M
    map <C-PageUp> :bp<cr>
    " Also:
    set <C-PageUp>=<^M>
    map <C-PageUp> :bp<cr>
    
  6. Trying all of the methods in 5, but without setting an option first. E.g.:

    map <C-[[5~> :bp<cr>
    
  7. The method from this answer: https://groups.google.com/forum/#!topic/vim_use/j85-2xQkb7s

    map [5~ :bp<cr>
    
  8. The method from this answer: https://superuser.com/questions/322983/how-to-let-ctrl-page-down-switch-tabs-inside-vim-in-terminal-app

     map \033[5;5~ :bp<cr>
    
  9. Setting different term options:

     set term=xterm
     " Also:
     set term=xterm-256color
    

    My $TERM environment variable is set to xterm.

  10. Using some of the methods which this answer hints at: https://superuser.com/questions/480215/how-to-map-pagedown-and-pageup-keys-to-function-normally

     map <Esc>[5~ :bp<cr>
     map <kpp> :bp<cr>
    
  11. Trying everything above with a file under .vim/after/plugin/ instead of in .vimrc.
  12. Trying everything above with :MBEbp instead of :bp.

What else can I try?

Lamellibranch answered 11/3, 2016 at 1:16 Comment(0)
L
8

Alright, I figured it out. I was overlooking something extremely simple, which is usually the case for me.

ctrlpgup and ctrlpgdn were already keyboard shortcuts in xfce4-terminal itself (for switching Terminal tabs). In order to allow Vim to use these key combinations, they cannot be used by the Terminal itself. So this was an issue with xfce4-terminal, not Vim.

xfce4-terminal shortcuts can be unset with the method described here: https://unix.stackexchange.com/questions/159979/xfce4-terminal-disable-individual-shortcut

In short, here's the process:

  1. Open ~/.config/xfce4/terminal/accels.scm and uncomment/edit the lines:

    (gtk_accel_path "<Actions>/terminal-window/next-tab" "")
    (gtk_accel_path "<Actions>/terminal-window/prev-tab" "")
    

    (You can verify this change by opening a new window and clicking Tabs in the Menubar: The Previous Tab and Next Tab items should no longer display shortcuts to their right.)

  2. Put this command in .vimrc:

    map <C-PageUp> :bp<CR>
    map <C-PageDown> :bn<CR>
    

    Consider nmap instead, to restrict the shortcut to NORMAL mode.

Lamellibranch answered 11/3, 2016 at 2:38 Comment(1)
Thanks, had same issues with xfce terminal and Shift+PgUp. Nowadays you can clear the shortcut by going to Preferences > Shortcuts and just clearing the one you want to use in vim.Velasquez
B
0

You were probably closest with #2, but perhaps misunderstood the instructions.

Vim does not know about control-pageup, etc., because there is no conventional termcap symbol for those keys, and because they can differ from one terminal to another. While vim has some built-in termcap information, it is unlikely to have these non-conventional definitions.

Also, while ncurses provides definitions for these keys though user-definable capabilities, vim does not use that information (because it uses only the termcap interface).

Vim can map a literal sequence of characters to a result. But to do this, you have to get the characters into vim. Your terminal may send a sequence of characters beginning with the escape character, which is often echoed as ^[. When entering the special key, you would have to prevent that escape character from being eaten by vim. The usual approach for that is to first press controlV (the "literal next" character), and then the special key. If you see ^[ echoed, that's normal.

Not all terminals send interesting (or different - or anything) for modified keys. Offhand, PuTTY and Konsole for instance are less capable in this area than xterm or rxvt, often just sending nothing. The side-effect of making the key visible is finding if your terminal sends something useful.

Further reading:

Bestride answered 11/3, 2016 at 1:46 Comment(1)
When I do this method with the literal next character to find the characters for pgup, it shows ^[[5~. If I do it for ctrl+pgup, it doesn't show anything. If I press ctrl+pgup and then enter, it shows ^M, but that is the same set of characters it shows for enter by itself. Does that mean that vim can't read this key combination?Lamellibranch

© 2022 - 2024 — McMap. All rights reserved.