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

5

51

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?

Exoenzyme answered 11/11, 2008 at 3:54 Comment(0)
K
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).

Kermes answered 11/11, 2008 at 16:59 Comment(6)
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.Murine
Oops, yes, it was eating everything between less than and greater than signs. Fixed.Kermes
Can you link something that explains what "properly configured" means?Did
@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.Schlesinger
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 for (u)rxvt (and xterm in a comment).Wsan
Probably relevant iTerm issue: gitlab.com/gnachman/iterm2/issues/3519 also mentioned this issue at github.com/zeit/hyper/issues/2602#issuecomment-403312098Nordic
P
20

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.

Pernicious answered 14/8, 2013 at 6:31 Comment(3)
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.Goeselt
Fantastic workaround but I'd suggest use something other than <C-u> as that key is currently used to clear the input line.Icsh
Clarification: ga is a mapping for :as[cii], in case you have ga already mapped elsewhere.Eclair
C
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

Cl answered 11/11, 2008 at 4:9 Comment(2)
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 respectivelyExoenzyme
Doesn't work in my Arch box. Probably works only on OSX?Stoup
P
3

For OSX:

nnoremap <Space> <C-d>
nnoremap <S-Space> <C-u>
Pavid answered 9/10, 2011 at 6:14 Comment(0)
P
3

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.
Perverted answered 25/7, 2020 at 6:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.