Do Vim users use Alt key for mappings? If yes, how exactly?
As it is known, there are 2 common ways of handling Alt key in CLI software: either <Esc>-prefix or 8-bit. It seems like Vim chooses the later way while a lot of other CLI software(e.g. Neovim, bash, tmux) chooses the former one. From intro.txt:

 4.  "<key>" means the special key typed.  This is the notation explained in
     the table above.  A few examples:
           <Esc>                Escape key
           <M-a>                Meta- a  ('a' with bit 8 set)
           <M-A>                Meta- A  ('A' with bit 8 set)

Given my terminal emulator is configured to emit <Esc>-prefix(and it is proven by showkey -a), does it mean that instead of nnoremap <M-y> :echo hello<cr> I should map nnoremap <Esc>y: echo hello<cr>? Or there is some option which I missed or trick that I don't know about?

  • From my experience, you should be able to map <esc>x in your case. In your terminal, with <c-v><a-x>, you will be able to check the sequence that is received. That's what you want in your map in vim. The ^[ character represents a <esc>, I recommend using the latter.
    – Biggybi
    Nov 12 '21 at 14:43
  • OFC I am able to map with <esc>, but since I am gonna use these mappings with Alt mapping with <M-..> seems more straightforward. So I would like to know if it is possible and how Vim folks usually deal with Alt-mappings
    – vatosarmat
    Nov 12 '21 at 14:51
  • Thanks for te clarification. I think you'll be able to do that with both eightbitinput and metasendsescape unset in your terminal. Enabling the former should allow mappings like <A-x>.
    – Biggybi
    Nov 12 '21 at 14:55

No, I cannot. While in practice it usually works well enough, Vim creator thinks it is a bad idea.
So, either <Esc> prefix should be mapped explicitly

nnoremap <Esc>y :echo 'hello'<cr>

or special :set-termcap option should be set in before:

execute "set <M-y>=\ey"
nnoremap <M-y> :echo 'hello'<cr>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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