49

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?

jerodsanto
  • 9,726
  • 8
  • 29
  • 23

5 Answers5

41

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).

Zathrus
  • 9,948
  • 2
  • 25
  • 22
  • Might want to edit your answer...cannot distinguish between what? I assume you mean the terminal cannot distinguish between CTRL-SPACE and simple SPACE by itself. – m0j0 Nov 12 '08 at 15:12
  • Oops, yes, it was eating everything between less than and greater than signs. Fixed. – Zathrus Nov 12 '08 at 15:16
  • Can you link something that explains what "properly configured" means? – unode Mar 09 '11 at 22:29
  • @Unode Configuring XTerm to send a different escape sequence than the single character 0x20 when the space bar is pressed while the shift key is depressed. This is done with the "translations" resource. – graywh Feb 10 '14 at 16:48
  • 2
    It can be work around using the capability of your terminal emulator to send something else upon hitting `shift`+`space`. See @BijouTrouvaille's answer for iTerm2, or [this](http://unix.stackexchange.com/a/88592/108579) for (u)rxvt (and xterm in a comment). – Júda Ronén Sep 30 '15 at 11:09
  • Probably relevant iTerm issue: https://gitlab.com/gnachman/iterm2/issues/3519 also mentioned this issue at https://github.com/zeit/hyper/issues/2602#issuecomment-403312098 – Ben Creasy Jul 08 '18 at 21:00
19

If you are using vim inside iTerm2 you can map shift-space to ctrl+U by sending the hex key 15. Here's a screen shot:

enter image description here

To look up a hex code for a ctrl+letter combination, for example ctrl+u, you can do the following:

  • In vim enter the insert mode
  • Hit ctrl+v then ctrl+u then ctrl+c then ga
  • various numerical representations will print at the bottom

You can apply this idea to other terminal emulators that support key mapping.

Bijou Trouvaille
  • 8,794
  • 4
  • 39
  • 42
  • Do this all the time. For example, I have mapped `C-i`, `C-m`, `C-,`, and `C-.` to the `F1` to `F4` hex codes. The function keys are great for this if you otherwise never use them. – Luke Davis Jul 29 '18 at 07:35
  • Fantastic workaround but I'd suggest use something other than `` as that key is currently used to clear the input line. – Sri Kadimisetty Jul 07 '19 at 02:23
  • 1
    Clarification: `ga` is a mapping for `:as[cii]`, in case you have `ga` already mapped elsewhere. – zkokaja Aug 07 '20 at 15:13
8

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

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 5
    When doing this, it pages down when using both Space and Shift Space... I have tried this in Debian Linux and OS X. I am running VIM 7.0 and VIM 7.2 respectively – jerodsanto Nov 11 '08 at 16:07
  • Doesn't work in my Arch box. Probably works only on OSX? – Kevin Dec 18 '15 at 08:14
3

For OSX:

nnoremap <Space> <C-d>
nnoremap <S-Space> <C-u>
Steve McKinney
  • 3,183
  • 1
  • 23
  • 26
2

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.
ebk
  • 586
  • 5
  • 11