Vim: Pressing <press Alt-3> suddenly maps to Esc (and not the character "#"

Created on 25 Aug 2019  Â·  4Comments  Â·  Source: vim/vim

I'm using VIM as text editor for a few weeks now.

I could write character dependent on hitting key alt without problem

Ex:

  • alt-3 gave the the character "#"
  • alt-5 gave the the character "["
  • alt-6 gave the the character "]"

But suddenly it changed and now when hit those shortcuts, it does not write the corresponding character and quits insert mode instead

When I'm outside Vim those shorcuts work fine

So i conclude that pressing suddenly seems to map to the command Esc (and not the character "#", How do I change that mapping

I tried the following command without success:

:unmap <Alt-3>
:unmap <Alt-3> Esc
:unmap <press Alt-3>
:unmap <press Alt-3> Esc

I want be able to write the character "#" with the shortcut Alt-3

Did I change something by mistake? What should I do to fix the problem? Should I change the ~/vimrc file

Most helpful comment

Oh, I forgot: on some systems the (left) Alt key prefixes an Esc to whatever comes afterwards. You can either set your system to add 0x80 instead, or else you can adjust the Vim timeouts, as follows:

set timeout timeoutlen=5000 ttimeoutlen=250

where:

  • timeout on means: time out on mappings and key codes
  • timeoutlen is the timeout for mappings, in milliseconds; it should be longer than your slowest typing speed. The example here sets 5 seconds, meaning if two keypresses come more than 5 seconds apart they are not part of the same mapping or abbreviation
  • ttimeoutlen is the timeout in milliseconds between bytes of a multibyte keycode; it should be faster than your fastest typing key but slower than the speed at which your keyboared line transmits successive bytes for a single "special key". The example here is 0.25 seconds meaning if two input bytes arrive less than ¼ second apart I didn't type them separately.

Best regards,
Tony.

All 4 comments

It makes a difference whether you use the Alt key left of the space bar, or the AltGr key right of the space bar — and on keyboard layouts where both Alt keys do the same, Alt+Ctrl sometimes works for AltGr.

On my keyboard, AltGr+3 gives # but Alt+3 (including Shift because on my AZERTY keyboard, 3 is Shift+") gives ³ i.e. 0xB3 i.e. 0x33 + 0x80 where 0x33 is the digit 3 and 0x80 is added to ASCII keys (i.e. keys in the range 0x20 to 0x7F) by the left Alt key.

If I hit (left) Alt with the unshifted 3 key, what I do is Alt + " i.e. 0x80 + 0x22 which is 0xA2 i.e. ¢

Adding 0x80 to ASCII keys when Alt is pressed is a feature of Vim (I think); but the workings of AltGr happen in your keyboard driver (or mine) before the key reaches Vim.

If you want to produce # by hitting 3 with either Alt or AltGr, you can use

map  <A-3> #
map! <A-3> #
map  <A-"> #
map! <A-"> #

(assuming " is unshifted 3, otherwise change as needed). Using :map both with and without ! makes the mapping apply in all modes: without means Normal and Visual, with means Insert/Replace and Command-Line.

I don't recommend it however. If AltGr+3 (using the AltGr key right of the spacebar), or Alt+Ctrl+3, gives you #, you might not need a mapping.

Best regards,
Tony.

Oh, I forgot: on some systems the (left) Alt key prefixes an Esc to whatever comes afterwards. You can either set your system to add 0x80 instead, or else you can adjust the Vim timeouts, as follows:

set timeout timeoutlen=5000 ttimeoutlen=250

where:

  • timeout on means: time out on mappings and key codes
  • timeoutlen is the timeout for mappings, in milliseconds; it should be longer than your slowest typing speed. The example here sets 5 seconds, meaning if two keypresses come more than 5 seconds apart they are not part of the same mapping or abbreviation
  • ttimeoutlen is the timeout in milliseconds between bytes of a multibyte keycode; it should be faster than your fastest typing key but slower than the speed at which your keyboared line transmits successive bytes for a single "special key". The example here is 0.25 seconds meaning if two input bytes arrive less than ¼ second apart I didn't type them separately.

Best regards,
Tony.

How vim receives depends on your terminal (or are you using the gui?) You did not give enough information and did not fill out the issue template. If you are using Vim in the terminal, you most likely can configure how the alt key works. xterm e.g. can be configured using the metaSendsEscape configuration.

Anyway, this here is for Vim bugs only. We cannot offer individual help here. Closing.

Thank you for your anwers to both of you

I'm using Vim in the terminal as well as iTerm under MacOS 10.14.6. The problem only appears actually when using the left alt key on iTerm (and not the right one on iTerm and neither one on terminal

@tonymec
I tried the following commands but it unfortunately didn't solve the problem

map  <A-3> #
map! <A-3> #
map  <A-"> #
map! <A-"> #
set timeout timeoutlen=5000 ttimeoutlen=250

@chrisbra
Sorry that i didn't fill the form and I understand that this is not a bug. Thank you for your time anyway and for maintaining this great open source software

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomtom picture tomtom  Â·  4Comments

GianniGi picture GianniGi  Â·  3Comments

DasOhmoff picture DasOhmoff  Â·  4Comments

j0ker70 picture j0ker70  Â·  3Comments

mgedmin picture mgedmin  Â·  5Comments