Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

osx lion - "M-" bindings in vim on iTerm2/Terminal don't work

I noticed that iTerm2 offers the very convenient feature of using "Option key as +ESC" (or as meta, but apparently that's obsolete).

I tried them both and the option key works as expected in Bash (set -o emacs mode) and Emacs, but not in Vim. Thus, I can't make use of any "M-" bindings. What I've found is that:

  • if I set option to "+ESC", vim just understands ESC+key, and has no idea I actually meant Meta-key.
  • if I set option to "meta", keys modified with "option" behave just as without a modifier (don't know how to formally test what vim understands from a key combination that I'm typing).

"M-" mappings work perfectly in MacVim (7.3.53), but that's outside of a console and not in the scope of this question.

I'm interested in how to make those mappings work in Vim, under iTerm2 or Terminal.

My specs:

  • MAC OS X Lion 10.7.2
  • iTerm2 1.0.0.20111020
  • vim @7.3.107_0+python26 [though MacPorts]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've downloaded the latest iTerm2 and tried to see what it sent to/what was printed by Vim (i<C-v><M-a>) with the following settings:

  • Option as Option:

    Vim prints ? which is normal and expected on my french keyboard

  • Option as Meta:

    Vim stays there, waiting for something to happen. Nothing is printed. If I press Option and a in sequence I just obtain a. Pressing Option and a in some random order may print á, which is weird and totally unexpected.

  • Option as +ESC:

    Vim prints ^[a which means "Escape character followed by the character a".

From these tests it appears that Vim will never ever receive <M-> without some hypothetical black magic.

If you stick with "Option as +ESC", it seems that you will have to change all your custom <M-something> mappings to <Esc>something. This may work but it will make writing any kind of prose in any non-english language a pain.

What I do: I leave the Option key as it is so that I can type characters like ?…?? easily and I use <Leader> (mapped to ,, see :help mapleader) for all my custom mappings.

Some people here like to reserve it for plugins and advocate a somewhat simpler and potentially safer approach.

inoremap <leader>, <C-x><C-o> "my way (copied elsewhere)
inoremap ,, <C-x><C-o>        "another way

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...