Vim Tips Wiki
m (Fix META-keys when META-keys break out of Insert mode moved to Fix meta-keys that break out of Insert mode: Page moved by JohnBot to improve title)
(Notes on which bindings break things)
Tags: Visual edit apiedit
(13 intermediate revisions by 8 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{Tip
 
 
|id=738
 
|id=738
  +
|previous=737
|title=fix META-keys when META-keys break out of Insert mode
 
  +
|next=739
|created=June 2, 2004 2:33
+
|created=2004
 
|complexity=basic
 
|complexity=basic
 
|author=Yakov Lerner
 
|author=Yakov Lerner
 
|version=6.0
 
|version=6.0
 
|rating=28/9
 
|rating=28/9
  +
|category1=Map
|text=
 
  +
|category2=Terminals
Q: My META-keys don't work. When I press META-key in insert mode, they break me out out of insert mode and execute some normal-mode commands.
 
 
}}
  +
On Unix-based systems, the Meta (Alt) key may not work in Vim. For example, in insert mode, pressing Meta-A (Alt-A) may exit to normal mode, then execute normal-mode commands. This can occur with non-GUI Vim under some terminal emulators – those which generate escape sequences for Meta-characters. The actually generated escape-sequences are <Esc>a .. <Esc>z.
   
  +
==Solution==
A: This happens with non-GUI vim under some terminal emulators -- those which generate escape-sequences for META-characters.
 
 
You must manually configure Vim to recognize these escape-sequences as Meta-characters, see below. Terminal emulators which are known to generate these sequences for Meta-keys are: rxvt (unix), putty (PC), teraterm (PC). Vim expects characters 225-250 for Meta-keys.
   
 
Here's how to fix Meta-keys on the Vim side:
The actually generated escape-sequences are &lt;ESC&gt;a .. &lt;ESC&gt;z.
 
   
 
Check what your Meta-keys generate:
You must manually configure vim to recognize these escape-sequences as META-characters, see below. Terminal emulators which are known to generate these sequences for META-keys are: rxvt (unix), putty (PC), teraterm (PC). Vim expects characters 225-250 for META-keys.
 
  +
<pre>
 
i<press Ctrl-V><press Meta-A>
  +
</pre>
   
 
If you see <code>^[a</code> (that is, escape character followed by something), then add this snippet to your vimrc:
Here's how to fix META-keys on vim side:
 
  +
<pre>
 
" fix meta-keys which generate <Esc>a .. <Esc>z
 
let c='a'
 
while c <= 'z'
 
exec "set <M-".toupper(c).">=\e".c
 
exec "imap \e".c." <M-".toupper(c).">"
 
let c = nr2char(1+char2nr(c))
 
endw
  +
</pre>
   
 
To fix Meta-keys definitions manually key-by-key:
- check what your META-keys generate:
 
  +
<pre>
i&lt;press Ctrl-V&gt;&lt;press Meta-A&gt;
 
 
:set <M-A>=<press Ctrl-V><press Meta-A>
 
:imap <press ctrl-v><press Esc>a <M-A>
 
; repeat each Meta-key.
  +
</pre>
   
  +
Don't attempt to map <code>&lt;M-[&gt;</code> or <code>&lt;M-Leader&gt;</code> in this situation. The former will break mouse support (and possibly other things), and the latter doesn't play nicely when exiting insert mode and attempting to perform a leader-bound command.
- if you see ^[a (that is, escape character followed by something), then add this snippet to your vimrc:
 
  +
==See also==
  +
*[[VimTip1129|Get Alt key to work in terminal]]
   
 
==Comments==
" fix meta-keys which generate &lt;esc&gt;a .. &lt;esc&gt;z
 
  +
Suggested fix does not work in PuTTY on Windows XP, using Vim 6.1.5.
let c='a'
 
while c != 'z'
 
exec "set &lt;M-".toupper(c)."&gt;=\e".c
 
exec "imap \e".c." &lt;M-".toupper(c)."&gt;"
 
let c = nr2char(1+char2nr(c))
 
endw
 
   
  +
The Vim help files suggest, for example:
- To fix META-keys definitions manually key-by-key:
 
  +
<pre>
  +
:set <M-b>=^[b
  +
</pre>
   
  +
This works, but remember to set ttimeoutlen to something small!
set &lt;M-A&gt;=&lt;press Ctrl-V&gt;&lt;press Meta-A&gt;
 
:imap &lt;press ctrl-v&gt;&lt;press Esc&gt;a &lt;M-A&gt;
 
; repeat each META-key.
 
   
  +
Also note, Meta-key mappings are case-sensitive (e.g. <m-b> and <m-B> are different).
}}
 
   
  +
--[[User:Fritzophrenic|Fritzophrenic]] 18:02, 30 June 2008 (UTC)
== Comments ==
 
I'm curious why the "set" command is necessary. I've just used the map command in the past, and it works fine for me. What does the set do?
 
   
'''Anonymous'''
 
, June 3, 2004 19:05
 
 
----
 
----
  +
In rxvt I found that the <code>:set <M-a>=^]a</code> commands don't work, instead only the map commands are needed. So I used:
This tip should be upvoted. It's great information for those who use the terminal and want portable maps.
 
  +
<pre>
I still don't get the need for the loop's imaps, though.. not sure if they make sense.
 
  +
for i in range(65,90) + range(97,122)
 
  +
let c = nr2char(i)
hlen--AT--ig.com.br
 
  +
exec "map \e".c." <M-".c.">"
, September 29, 2004 14:48
 
  +
exec "map! \e".c." <M-".c.">"
----
 
  +
endfor
That tip didn't help me in rxvt/Cygwin.
 
  +
</pre>
 
no--AT--no.no
 
, December 20, 2005 2:29
 
----
 
<!-- parsed by vimtips.py in 0.611415 seconds-->
 
*
 
 
   
  +
instead of the snippet given above, and everything works fine now.
[[Category:meta keys]]
 
   
  +
[[User:Jzxu|Jzxu]] 12:20, 13 March 2009 (UTC)
[[Category: terminal]]
 
   
  +
:^] is not ^[, chłopcze
[[Category: map]]
 

Revision as of 23:42, 2 August 2015

Tip 738 Printable Monobook Previous Next

created 2004 · complexity basic · author Yakov Lerner · version 6.0


On Unix-based systems, the Meta (Alt) key may not work in Vim. For example, in insert mode, pressing Meta-A (Alt-A) may exit to normal mode, then execute normal-mode commands. This can occur with non-GUI Vim under some terminal emulators – those which generate escape sequences for Meta-characters. The actually generated escape-sequences are <Esc>a .. <Esc>z.

Solution

You must manually configure Vim to recognize these escape-sequences as Meta-characters, see below. Terminal emulators which are known to generate these sequences for Meta-keys are: rxvt (unix), putty (PC), teraterm (PC). Vim expects characters 225-250 for Meta-keys.

Here's how to fix Meta-keys on the Vim side:

Check what your Meta-keys generate:

i<press Ctrl-V><press Meta-A>

If you see ^[a (that is, escape character followed by something), then add this snippet to your vimrc:

" fix meta-keys which generate <Esc>a .. <Esc>z
let c='a'
while c <= 'z'
  exec "set <M-".toupper(c).">=\e".c
  exec "imap \e".c." <M-".toupper(c).">"
  let c = nr2char(1+char2nr(c))
endw

To fix Meta-keys definitions manually key-by-key:

:set <M-A>=<press Ctrl-V><press Meta-A>
:imap <press ctrl-v><press Esc>a <M-A>
; repeat each Meta-key.

Don't attempt to map <M-[> or <M-Leader> in this situation. The former will break mouse support (and possibly other things), and the latter doesn't play nicely when exiting insert mode and attempting to perform a leader-bound command.

See also

Comments

Suggested fix does not work in PuTTY on Windows XP, using Vim 6.1.5.

The Vim help files suggest, for example:

:set <M-b>=^[b

This works, but remember to set ttimeoutlen to something small!

Also note, Meta-key mappings are case-sensitive (e.g. <m-b> and <m-B> are different).

--Fritzophrenic 18:02, 30 June 2008 (UTC)


In rxvt I found that the :set <M-a>=^]a commands don't work, instead only the map commands are needed. So I used:

for i in range(65,90) + range(97,122)
  let c = nr2char(i)
  exec "map \e".c." <M-".c.">"
  exec "map! \e".c." <M-".c.">"
endfor

instead of the snippet given above, and everything works fine now.

Jzxu 12:20, 13 March 2009 (UTC)

^] is not ^[, chłopcze