Tip: #738 - Fix meta-keys that break out of Insert mode
Created: June 2, 2004 2:33 Complexity: basic Author: Yakov Lerner Version: 6.0 Karma: 28/9 Imported from: Tip#738
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.
A: This happens 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.
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 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.
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
This tip should be upvoted. It's great information for those who use the terminal and want portable maps. I still don't get the need for the loop's imaps, though.. not sure if they make sense.
hlen--AT--ig.com.br , September 29, 2004 14:48
That tip didn't help me in rxvt/Cygwin.
no--AT--no.no , December 20, 2005 2:29