1

I want to make separate bindings for <C-h> and <C-S-h>. For example:

noremap <C-h> <C-w>h
noremap <C-j> <C-w>j
noremap <C-k> <C-w>k
noremap <C-l> <C-w>l

noremap <C-S-h> :vertical resize -2<CR>
noremap <C-S-j> :resize -2<CR>
noremap <C-S-k> :resize +2<CR>
noremap <C-S-l> :vertical resize +2<CR>

However, with my settings, pressing <C-h> and <C-S-H> both trigger :vertical resize -2<CR> which should be mapped to only <C-S-h>.

How can I map <C-h> and <C-S-h> separately?

1 Answer 1

1

This is impossible as Vim doesn't "see" <C-S-h>.

Try the following:

Open Vim and enter insert mode. Noch press <C-v> followed by <C-h>. Vim will insert ^H (highlighted in light blue by default). So Vim got <C-h>.

Now press <C-v> followed by <C-S-h> and Vim will again insert ^H.

So from Vims point of view, <C-h> and <C-S-h> are identical.

The reason is Vims origin as a terminal based program. Those terminals only supports "simple" control-character combinations.

You can use the test described above ( <C-v> ...) with any key combination you would like to map.

PS: On my setup with Linux and GNOME-Terminal, Vim doesn't see <C-S-h> at all, as the key combination is used by GNOME-Terminal.


Updated after the comments from Christian Brabandt and user938271:

Your mapping works with GVim (tested with 8.2.869).

Depending on the terminal emulator it might be possible for Vim to get the <C-S-h>.

The XTerm terminal emulator supports a option called modifyOtherKeys. With it the terminal supports additional key combos like <C-S-h>.

When XTerm is correctly configured, the mapping you describe work as expected. Tested with Vim 8.2.869 and xterm 351.

Read about it in :h modifyOtherKeys.

Notes:

  • I don't know which other terminal emulators support modifyOtherKeys.
  • modifyOtherKeys is not supported by GNOME-Terminal (see this bug )
  • First mention of modifyOtherKeys in a Vim commit was version 8.1.2134 (Oct 2019)

Conclusion: Your mapping will work with the right terminal emulator and configuration and a up-to-date Vim, but I would not rely on this, as it is not widely portable.

6
  • 1
    That is not completely correct nowadays. With modifyOtherKeys, vim can distinguish <c-h> and <c-s-h>. However I think currently only xterm supports this (and perhaps the gui). gnome-terminal (or vte) has some wishlist bugs about supporting modifyOtherKeys May 31, 2020 at 11:41
  • @ChristianBrabandt I played around with modifyOtherKeys in xterm. It sends esc-sequences for something like <C-.> or <C-S-.>, but <C-h> and <C-S-h> both send <C-h> (tried other chars as well).
    – Ralf
    May 31, 2020 at 13:47
  • You can also distinguish the inserted presses with ga. bestasciitable.com is good too :)
    – D. Ben Knoble
    May 31, 2020 at 14:17
  • @Christian Brabandt is right. Vim can distinguish C-h from C-S-h in a terminal which supports the modifyOtherKeys feature such as xterm. xterm sends ^[[27;5;104~ for C-h and ^[[27;6;72~ for C-S-h which are different sequences. Tested on Vim 8.2.0869 and xterm 322.
    – user938271
    May 31, 2020 at 22:26
  • To be sure that modifyOtherKeys is enabled, press C-S-v followed by C-v in insert mode; if you only get a literal ^V, then the feature is not enabled. Note that C-S-v is often used in a terminal key binding to paste the clipboard; if that's the case, you may need to temporarily disable the key binding.
    – user938271
    May 31, 2020 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.