(tip doesn't work in at least one case) |
(Notes on which bindings break things) Tags: Visual edit apiedit |
||
(8 intermediate revisions by 7 users not shown) | |||
Line 3: | Line 3: | ||
|previous=737 |
|previous=737 |
||
|next=739 |
|next=739 |
||
− | |created= |
+ | |created=2004 |
|complexity=basic |
|complexity=basic |
||
|author=Yakov Lerner |
|author=Yakov Lerner |
||
Line 11: | Line 11: | ||
|category2=Terminals |
|category2=Terminals |
||
}} |
}} |
||
+ | 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. |
||
− | 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. |
||
+ | ==Solution== |
||
− | 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. |
||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
<pre> |
<pre> |
||
− | i |
+ | i<press Ctrl-V><press Meta-A> |
</pre> |
</pre> |
||
− | If you see < |
+ | If you see <code>^[a</code> (that is, escape character followed by something), then add this snippet to your vimrc: |
− | |||
<pre> |
<pre> |
||
− | " fix meta-keys which generate |
+ | " fix meta-keys which generate <Esc>a .. <Esc>z |
let c='a' |
let c='a' |
||
while c <= 'z' |
while c <= 'z' |
||
− | exec "set |
+ | exec "set <M-".toupper(c).">=\e".c |
− | exec "imap \e".c." |
+ | exec "imap \e".c." <M-".toupper(c).">" |
let c = nr2char(1+char2nr(c)) |
let c = nr2char(1+char2nr(c)) |
||
endw |
endw |
||
</pre> |
</pre> |
||
− | To fix |
+ | To fix Meta-keys definitions manually key-by-key: |
− | |||
<pre> |
<pre> |
||
− | :set |
+ | :set <M-A>=<press Ctrl-V><press Meta-A> |
− | :imap |
+ | :imap <press ctrl-v><press Esc>a <M-A> |
− | ; repeat each |
+ | ; repeat each Meta-key. |
</pre> |
</pre> |
||
+ | |||
+ | Don't attempt to map <code><M-[></code> or <code><M-Leader></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. |
||
+ | ==See also== |
||
+ | *[[VimTip1129|Get Alt key to work in terminal]] |
||
==Comments== |
==Comments== |
||
Suggested fix does not work in PuTTY on Windows XP, using Vim 6.1.5. |
Suggested fix does not work in PuTTY on Windows XP, using Vim 6.1.5. |
||
− | The Vim help files suggest: |
+ | The Vim help files suggest, for example: |
− | |||
<pre> |
<pre> |
||
− | :set <M-b>=^ |
+ | :set <M-b>=^[b |
</pre> |
</pre> |
||
+ | This works, but remember to set ttimeoutlen to something small! |
||
− | This works, but will cause the ESC key followed by an ascii key to basically invoke a mapping. E.g. pressing <ESC> then b (to leave insert mode, then go back a word) will instead insert whatever the ascii code for ALT+b is. |
||
+ | |||
+ | Also note, Meta-key mappings are case-sensitive (e.g. <m-b> and <m-B> are different). |
||
+ | |||
⚫ | |||
⚫ | |||
---- |
---- |
||
+ | 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: |
||
+ | <pre> |
||
+ | 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 |
||
+ | </pre> |
||
+ | |||
+ | instead of the snippet given above, and everything works fine now. |
||
+ | |||
+ | [[User:Jzxu|Jzxu]] 12:20, 13 March 2009 (UTC) |
||
+ | |||
+ | :^] is not ^[, chłopcze |
Revision as of 23:42, 2 August 2015
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