How can I make shift+spacebar page up in Vim?

Viewed 14522

I have an entry in my .vimrc which makes it page down the viewport when I hit the spacebar. It looks like this:

map <Space> <PageDown>

I want to create another key mapping which pages the viewport up when holding shift and hitting the spacebar. I have tried the following entries:

map <Shift><Space> <PageUp>
map <S-Space> <PageUp>

Neither work. Anybody know how to achieve this functionality?

5 Answers

You cannot. CMS's solution will work for gVim, but not in vim because terminals cannot distinguish between <Space> and <S-Space> because curses sees them the same. It might be possible in the future if vim gains libtermkey support and your terminal supports the proper <CSI> sequences (xterm does if properly configured; nothing else does yet).

Use this:

map <Space> ^D   " Pagedown when press Space
map <S-Space> ^U " Page Up when press Shift Space

To get the ^D and ^U symbol correctly just press Control-V Control-D, and Control-V Control-U

Inspired by this answer, I got the mapping done by:

  • Using a terminal that supports key mapping
  • Mapping Shift+Space to a sequence of characters that can be distinguished from Space by Vim, but still has a net effect of writting a space when you input text.

How-to:

  1. Choose one of those terminals that are capable of key mapping.
    For example, Alacritty, xterm, urxvt, or Kitty.

  2. Choose the character sequence to which Shift+Space will be mapped.
    SOME_KEY,Backspace,Space should do the trick, where SOME_KEY:

    • writes a character that can be canceled out by a Backspace when inputting text, e.g. a printable character or a tab.
    • is also a key you rarely use in Vim normal mode and/or visual mode (depending on which mode you want to use Shift+Space in). If it has already been binded to some heavily-used command, you may experience some lag (see :h timeout for details) every time you use the command.

    I am using \ as SOME_KEY (` may also be a good option).

  3. Mapping in the terminal
    I'm using Alacritty, so here is an example of mapping Shift+Space to the character sequence in Alacritty. In the config file add the following line under the "key_bindings" item: - { key: Space, mods: Shift, chars: "\x5c\x08 " }.

    To confirm the mapping works, run showkey -a in the terminal and press Shift+Space, then the character sequence should be output. For the example above, the output is:

     \^H      92 0134 0x5c  
               8 0010 0x08  
              32 0040 0x20
    
  4. Mapping in Vim
    Map the character sequence to page up in Vim config. In my case (using \ as the first key), it will be no <Bslash><C-h><Space> <C-b>. If you need the mapping in visual mode too, also add vno <Bslash><C-h><Space> <C-b>.

I have used this settings for a while and it hasn't yet broken insert mode and replace mode of Vim as well as most programs that accept text input on my system. But it's not a solution, but a workaround, which do have some flaws, e.g.:

  • In Vim if you try to replace a character under the cursor (by r by default) with Shift+Space, you won't get a space.
  • Some programs that interpret character sequences as commands like what Vim does may be affected.
Related