3

My keybindings are consistent and work across different operating systems, terminal emulators, browsers, tmux, etc. There are no clashes, but this sometimes means remapping things in some instances.

Therefore my vim-tmux-navigator mappings are probably a little unusual:

nnoremap <silent> <C-M-h> :TmuxNavigateLeft<CR>
nnoremap <silent> <C-M-j> :TmuxNavigateDown<CR>
nnoremap <silent> <C-M-k> :TmuxNavigateUp<CR>
nnoremap <silent> <C-M-l> :TmuxNavigateRight<CR>

They work in nvim but I have never got them to work in vim, but since I got this answer a while ago, I now understand why.

To get the correct mappings that work in vim in that question for <M-S-j>, you can have to enter the code by typing C-v and then <M-S-j>, resulting in something that looks like ^[J (typing those characters does not result in the same code and they get highlighted in different colours).

This is not possible when the Ctrl key is being used in the mapping. Is there a way of getting this code somehow?

I have:

  • nvim on Linux

  • vim on Linux and Windows

  • gvim on Linux and Windows

...available for input. It is only vim on Linux that this problem applies to, as there is no tmux used elsewhere other than with nvim on Linux, but the problem does not occur there as nvim accepts the syntax above.

5
  • The mapping of special modifier keys and letter highly depend on the OS (Windows, Linus, Unix), the flavor (neovim or vim), if you use the terminal version or the GUI version and of course the version number and patch number of neovim/vim. Would it be possible to add that information to your question. It will help us to help you :-) Oct 12, 2022 at 9:24
  • @VivianDeSmedt This is only a problem on Vim for Linux, but I have added more information.
    – paradroid
    Oct 12, 2022 at 17:47
  • Can you tell us if you are using the console version (vim) or the GUI version (gVim)? Could you tell us which version of vim you are using (8.2, 9.0)? I'm also interested to know the patch version :-) Oct 12, 2022 at 18:11
  • @VivianDeSmedt I said above that this is to do with tmux, so in the terminal. Tmux runs on Linux so it's just Vim on Linux in the terminal. I don't think anything else is relevant, as I think what I need are the ANSI escape codes for the above key combinations and the way to input them to the .vimrc.
    – paradroid
    Oct 12, 2022 at 22:19

1 Answer 1

4

The problem

For historical reasons, the Alt key can be configured to work in two different ways. When pressing a character with Alt held down, your terminal can either:

  1. Set the 8th bit of that character, (traditional Meta behaviour)
  2. First send the ESC character, and then the pressed character (Alt key meta emulation).

You have your terminal configured to use the second method, but Vim is expecting the first.

The answer you received to your other question was therefore slightly misleading. It is not true that you cannot ever use <A- mappings, it's just that your current configuration doesn't allow it.

There are various ways you can deal with this:

Reconfigure your terminal

You could change your terminal settings to treat Alt as a Meta key. Vim's documentation has some suggestions for how to do this, universally or just for Vim, at :help :map-alt-keys.

But if you don't want to touch your terminal configuration, there are ways to handle this inside of Vim:

Use 'modifyOtherKeys'

If your terminal supports it, you can switch on its modifyOtherKeys mode. This should be handled automatically by your terminfo, but if that's not set up properly there are some instructions at :help modifyOtherKeys for how to override the settings in your Vim config.

Otherwise, you can:

Tell Vim what to expect

If you don't want to reconfigure your terminal, you can explicitly inform Vim what keycodes to expect for your key presses:

" First tell Vim what keycodes to expect
" We use :execute so we don't have to enter literal character codes with <C-V>
execute "set <C-M-j>=\<Esc>\<C-j>"

" Then map them
nnoremap <C-M-j> :TmuxNavigateDown<CR>

There are a couple of big N.B.s here, though:

  1. You have to set up all the keycodes you want to use, either with a Vimscript loop or one-at-a-time,
  2. Specifically for <C-M- mappings, although the mapping fires correctly, the original behaviour of j is also triggered: the cursor moves down. I'm not sure why this occurs.

Bearing the above in mind, there is a fourth option:

Just map the keycodes directly

This is the solution you already tried. As you've discovered, entering the raw keycodes via Ctrl-V doesn't work for <C-M- keystrokes. But we know what keycodes you're going to receive, so there's an alternative that would be preferable anyway as it doesn't require adding literal control characters into your vimrc:

nnoremap <Esc><C-j> :TmuxNavigateDown<CR>
3
  • I have found often in the past that a mapping with <Esc> in it causes other problems, so use caution with that last option.
    – D. Ben Knoble
    Oct 13, 2022 at 12:44
  • @D.BenKnoble That sounds vaguely familiar to me, although the only one I'm actively aware of is if you map a bare <Esc> by itself. The OP is already using mappings containing ESC (presumably without issue), but one thing to try if problems occur is only setting up the mapping after the rest of Vim startup.
    – Rich
    Oct 14, 2022 at 9:46
  • The last method seems to be working fine so far. I actually map a lot of keys with ESC on gvim and vim for Windows so that I do not need to use the ESC key to leave Insert Mode, like in terminal nvim/vim. Instead I use Alt at the same time as the first Normal mode key. I hardly ever use the ESC key.
    – paradroid
    Oct 14, 2022 at 10:49

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.