216

In bash shell with emacs key-binding, you can use key combination like M-f, M-b to move one word forward or backward on the shell prompt respectively. Usually, the meta key is mapped to Alt key on Windows and Linux. However, in iTerm, I could not find a way to map this meta key to either Option or Command key on my MacBook Pro.

It seems that in OS X, the meta key is by default mapped to ESC key. So you can use ESC-f, ESC-b on iTerm. However, ESC key is apparently not practical to use. In addition, iTerm does have option that allow you to modifier mapping for the meta key (Bookmarks > Profiles > Keyboard Profiles > Global > Option Key as...), this setting does not seem to work at all.

Therefore, if anyone know what is the solution to this problem, please let me know.

I have upgraded to the latest release, 0.9.6.1012, and this behavior is still persist.

Edit: Some clarification to my question. The key-binding I'm talking about is for bash shell, not in emacs. It just happens that, by default, bash shell also use the same key-binding as emacs.

3
  • After messing with this issue for a while and cannot figure out a solution. I gave up and switched to Terminal now.
    – ejel
    Oct 22, 2008 at 19:51
  • 2
    don't forget that iterm2 already exists and everything is fine in there=)
    – holms
    Feb 10, 2011 at 1:33
  • 1
    Even with iterm2, from a fresh install 2021-May, on a Mac one still needs to remap left/right option to act as "+Esc" rather than "Normal"
    – jv-dev
    May 28, 2021 at 18:01

15 Answers 15

276
  1. Open "Preferences" (,) -> Profiles -> Keys -> General tab.
  2. Set Left Option key: to act as Esc+.

Works for me in Emacs, though I'm not sure if it will have any other undesirable effects.

8
  • I did try Option Key as... with all choices but none of them works for me in bash shell. It only shows "?" every time I press the meta key combination.
    – ejel
    Oct 14, 2008 at 2:40
  • 3
    It looks like your input language has to be U.S. for the +ESC option to work. +ESC didn't work for me when I had the Japanese/Romaji input method selected, but it started to work once I switched the input language to U.S. May 11, 2009 at 6:42
  • 2
    Preferences, Profile, Keys and choose Left option Key acts as: +Esc. Then you will have both left option key AND Esc key as meta key. That is what happened in my case.
    – cgl
    Oct 12, 2013 at 10:16
  • With iTerm 2 this seems to be the recommended option.
    – ejel
    Sep 19, 2017 at 20:19
  • 17
    For my external keyboard I had to set both left and right option keys to use Esc+, even though I was using the left option key it would still print weird characters. This might happen because I use the Keyboard Modifiers option in Mac Keyboard settings to change the keys.
    – grego
    Mar 20, 2018 at 0:16
132

Go to Bookmarks > Manage Profiles. Then select Keyboard Profiles > Global and choose Option Key as Meta. This works for me in version 0.9.6.1201.

5
  • 2
    I had to change the xterm (OS X) entry rather than global, but it is working now. Aug 1, 2010 at 22:39
  • I don't know why on my machine the setting is broken on 0.9.6.1201 and several other builds I tried. It works, however, with the latest 0.9.6 build and 0.10 though. So it shouldn't be an issue now.
    – ejel
    Feb 11, 2011 at 16:21
  • 12
    in iTerm2 this worked only after I re-defined the Profile Shortcut Keys for alt+-> and alt+<- to Send Escape Sequence + f/b respectively. Oct 24, 2013 at 11:19
  • 1
    I had to open a new iterm session for it to kick in
    – Skurpi
    Jun 2, 2014 at 6:28
  • @SridharRatnakumar Works for iTerm too Feb 20, 2016 at 12:08
35

In Menu bar Iterm2 => Preferences => Profiles choose the keys tab and select

left Option key act as +Esc

enter image description here

worked for me as well in irssi.

3
  • Excellent, this also works in the combination of iTerm2 + tmux May 24, 2019 at 15:25
  • In my case, left and right were swapped, I had set the Right option to Esc+ for the button at the left of my keyboard to work (Using an external keyboard)
    – Adam
    Aug 7, 2022 at 12:23
  • @Adam — wish I'd seen your comment an hour ago! :) Same issue here.
    – ebarrere
    Feb 8 at 15:14
31

For iTerm2 users this answer just partially solves the problem.

Cmd+., Profiles, Keys and choose Left option Key acts as: +Esc.

In addition to that, you have to go to Preferences (Cmd+,) ➔ ProfilesKeys and in the Key Mappings find these Key Combination's: enter image description here and enter image description here. Double click on each mapping and change their Action and Esc+ to the following:

enter image description here

enter image description here

3
  • This is really helpful. Thanks a lot bro! Mar 28, 2020 at 1:07
  • for some reason I have to press CMD + ALT + -> .... in my app with that..
    – nicolas
    Jan 3, 2022 at 15:59
  • @nicolas this thread is for macos
    – Nick D
    Jan 4, 2022 at 1:19
12

Go to your iTerm preferences select "profiles" then "key" and change your presets in "Natural Text Editing"

iTerm Preferences

It should work immediately after.

It works with iTerm version 3.3.12

1
  • I used Terminal.app compatibility and it worked.
    – dresh
    Feb 18, 2021 at 10:36
4

Going all zombie resurrection here, but for sake of posterity --

This post got it working as you desire for me, for M-b and M-f, while keeping 'Option Key as Meta' set so you don't sacrifice a modifier (I have vim mappings using meta, personally).

In summary, in your Global Keyboard Profile, add two new mappings with settings:

  1. Key as hex code, value of 62 for b, 66 for f (man ascii)
  2. Modifier as Option
  3. Action as send escape sequence, value b or f

The only thing I still miss from Terminal.app is Opt-Backspace to delete by word. There are other workarounds for that, but I'm just trying to train myself to use Ctrl-w.

1
  • May be a new feature since this answer, but you can map a Global Key Shortcut to send Ctrl-w when Opt-Backspace is pressed. Feb 21, 2014 at 21:59
4

It's easier in iTerm2. Go to preferences->bookmark->keyboard and select Option sends +ESC. In iTerm2 you can configure the left and right option keys separately, so remember to change both if that's what you want. Link for iTerm2: http://sites.google.com/site/iterm2home/

3

Go to Bookmarks > Manage Profiles. Then select Keyboard Profiles > Global and choose Option Key as +ESC.

Works in iTerm Build 0.9.5.0611 & Build 0.9.6.20090415

1
  • Hmm. The setting seems to be broken on my machine with these two versions as well. However, the latest 0.9.6.20090928 and 0.10 work as expected though.
    – ejel
    Feb 11, 2011 at 16:11
2

I found some solution on stackoverflow also, just go to keyboard profile and in "Global" change "Option key as" +ESC

that works for me perfectly =) btw some iterm patch exists also look here: http://www.emacswiki.org/emacs/EmacsForMacOS

0

I don't know about iTerm, but you can do this in Terminal. Instructions can be found here:

Emacs on Mac OS X Leopard key bindings

1
  • Thank you. I am aware that I can do that in Terminal. I just want to achieve the same result in iTerm. By the way, what I'm looking for is the key-binding in bash shell, not in emacs. It just happens that bash shell also uses emacs key binding.
    – ejel
    Oct 14, 2008 at 2:42
0

Here is what worked for me: Bookmarks > Manage Profiles > Keyboard Profiles > Global/xterm > Option Key as +Esc. With this, I can use Option+Enter in Midnight Commander and the currently selected file/directory name shows up in the command string, as it should. Option Key as Meta did not work for me. My build of iTerm is 0.10.

0

I was struggling with Meta + Left or Meta + Right. The context was for keystroke in Emacs utilized in an iTerm2 session.

I tried Esc+ and Meta for the left Option key with no luck. I tried with and with out the hex mappings. No luck.

But then I tried "Report modifiers using CSI u". (Documentation here). That allowed me to use the meta + Left and meta + right keys in Emacs, which is the behavior I have on my Linux machine.

0

The common answers of setting the options key to ESC+, didn't work for me out of the box. At some point I realized that it doesn't work only when opening the floating iterm terminal (using F12). The trick to fix it was setting the options key to ESC+ also for the "Hotkey Window" profile.

enter image description here

0

This tiny note in the "Apps can change this" mouseover made all the difference for me:

enter image description here

After trying all the options above for what seemed like hours, it was as simple as setting the "Right Option key" to act as Esc+ (though I was pressing Left).

-2

Try "export LANG=C". I'm not emacs expert, but I found this enables emacs on OS X to recognize the Option key as the Meta key in iTerm,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.