如何在Vim中将shift+空格键设置为向上翻页?

50

我在我的 .vimrc 文件中添加了一个条目,使得按下空格键时它会向下翻页。代码如下:

map <space> <c-d>
map <Space> <PageDown>

我想创建另一个按键映射,当按住 Shift 键并敲击空格键时,将视口向上翻页。我已经尝试了以下条目:


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>

两个都不起作用。有人知道如何实现这个功能吗?

5个回答

41

无法实现。对于gVim,CMS的解决方案是可行的,但在vim中不行,因为终端无法区分<Space>和<S-Space>,因为curses将它们视为相同。如果vim获得了libtermkey支持并且你的终端支持正确的<CSI>序列(如果正确配置,则xterm支持;其他终端尚未支持),那么将来可能会有可能。


您可能需要编辑您的答案...无法区分什么?我猜您的意思是终端本身无法区分CTRL-SPACE和普通空格。 - m0j0
哎呀,是的,它正在吞噬小于号和大于号之间的所有内容。已修复。 - Zathrus
你能否提供一个解释“properly configured”(正确配置)含义的链接? - unode
@Unode 配置XTerm,使其在按下Shift键时按下空格键时发送不同于单个字符0x20的转义序列。这可以通过“translations”资源完成。 - graywh
2
可以通过利用终端仿真器的功能,在按下 shift+space 时发送其他内容来解决此问题。请参考 @BijouTrouvaille 的 iTerm2 回答,或者查看 此处 了解 (u)rxvt(以及评论中的 xterm)的解决方法。 - Júda Ronén
可能与iTerm有关的问题:https://gitlab.com/gnachman/iterm2/issues/3519也在https://github.com/zeit/hyper/issues/2602#issuecomment-403312098中提到了这个问题。 - Ben Creasy

19
如果你在 iTerm2 中使用 vim,你可以通过发送十六进制键值为15的按键来将shift-space映射为ctrl+U。以下是屏幕截图: enter image description here 若要查找 ctrl+字母组合的十六进制代码(例如 ctrl+u),你可以执行以下步骤:
1. 进入 vim 的插入模式 2. 按下 ctrl+v,然后依次按下 ctrl+u、ctrl+c 和 ga 3. 各种数字表示将会打印在底部
你可以将这个想法应用到支持按键映射的其他终端仿真器中。

一直这样做。例如,我已将 C-iC-mC-,C-. 映射到 F1F4 的十六进制代码。如果你平时不使用功能键,它们非常适合这个用途。 - Luke Davis
非常棒的解决方法,但我建议使用除<C-u>以外的其他按键,因为该按键目前用于清除输入行。 - Sri Kadimisetty
1
澄清:ga:as[cii] 的映射,如果您已经在其他地方映射了 ga,请注意。 - zkokaja

8
请使用以下内容:
map <Space> ^D   " Pagedown when press Space
map <S-Space> ^U " Page Up when press Shift Space

要正确地获取^D和^U符号,只需按Control-V Control-D和Control-V Control-U即可。

5
在执行此操作时,使用空格键和Shift+空格键时会向下翻页...我已经在Debian Linux和OS X上尝试过。分别运行的是VIM 7.0和VIM 7.2。 - jerodsanto
在我的 Arch 系统上无法运行。可能只能在 OSX 上运行? - Kevin

3

对于OSX:

nnoremap <Space> <C-d>
nnoremap <S-Space> <C-u>

2

this answer的启发,我通过以下方式完成了映射:

  • 使用支持键映射的终端
  • Shift+Space 映射为一系列字符,这些字符可以被 Vim 区分出来,但在输入文本时仍具有写入空格的净效果。

操作步骤:

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

我已经使用这些设置一段时间了,它还没有破坏我的Vim的插入模式和替换模式以及大多数接受文本输入的程序。但这不是一个解决方案,而是一个解决方法,它确实有一些缺陷,例如:

  • 在Vim中,如果你尝试用Shift+Space替换光标下的字符(默认情况下使用r),你将得不到一个空格。
  • 一些像Vim一样解释字符序列为命令的程序可能会受到影响。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接