20

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]
Dan
  • 3,490
  • 2
  • 22
  • 27
  • 2
    Sometimes people use the term "Meta" with a very narrow interpretation to mean "an ASCII character with the high bit set"; however, more generally "Meta" can mean either using the high bit or prefixing with ESC. I assume from the wording that "Option as Meta" specifically means "set the high bit", which is incompatible with using a UTF-8 (or other 8-bit) encoding. Modern terminal emulators generally default to UTF-8 and require using the ESC prefix for "Meta". Mac OS X Terminal's "option as meta key" option only means "prefix with ESC". – Chris Page Sep 24 '12 at 13:11
  • 1
    http://stackoverflow.com/a/15399297/2355112 has the solution on mapping "Option+char" in Mac. – oozzal Sep 18 '14 at 04:38

2 Answers2

9

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
romainl
  • 186,200
  • 21
  • 280
  • 313
  • 1
    Precisely. Vim never gets any sort of `` combination. The problem is that there are several plugins I use that have `M-` bindings, and it's inconvenient to mess with the plugins. Maybe there's a vim patch to trick it into converting a really fast `Esc+key` into ``? – Dan Dec 30 '11 at 09:59
  • 2
    Also I'd like to map something like `` and `` to do what `Alt+(up|down)` does in Eclipse (i.e. move line/lines past the line above/below). If I were to map `Esc+(j|k)` instead, then it would be a pain when I exit insert mode and genuinely press one of those movement keys... – Dan Dec 30 '11 at 10:03
  • Could you list the plugins that need ``? – romainl Dec 30 '11 at 10:35
  • I have `nnoremap j` and `nnoremap k` in my `~/.vimrc`. Vim is not Eclipse or Notepad++ or TextMate: you can't just import all your habits like that. Your muscle memory has to be re-trained to work effectively with it. It sure has a cost. – romainl Dec 30 '11 at 10:41
  • LaTeX-Suite (http://www.vim.org/scripts/script.php?script_id=475) for instance. Sure, but the problem is that stuff that should work doesn't. Why can bash and emacs use Meta, but not vim? – Dan Jan 02 '12 at 01:07
  • 3
    I'm not sure, see `:help map-alt-keys`: it looks like Vim may be a little limited on that front. – romainl Jan 02 '12 at 09:33
5

I left my option key to act as Normal and discovered that Vim saw them as <T- bindings. So, for example, I have this mapping setup in my .vimrc to move to the end of a word when in Insert mode:

noremap! <T-Right> <C-o><Right>;
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
bfad
  • 309
  • 2
  • 3