57

I understand that Mac has some Emacs keybindings enabled by default (e.g. Ctrl+A move to beginning of line etc.). If I open TextEdit for example I can navigate like in Emacs.

But when I try Alt+F or Alt+b for move to next word or last word it does not work. It prints out ƒ and instead.

Is there a way to jump words instead?

I think that I did not tinker with the keybindings and I use the US layout on a german keyboard.

4
  • 1
    To clarify, you are not talking about a problem with Emacs itself, correct?
    – phils
    Nov 22, 2013 at 14:25
  • 1
    No, my Emacs setup is awesome :) I am especially talking about other apps like firefox, chrome, textedit etc.
    – ayckoster
    Nov 22, 2013 at 16:07
  • 2
    None of these worked for me. What did work is the [selected answer to this SO question][1]. [1]: stackoverflow.com/questions/21937061/…
    – garyp
    Feb 22, 2014 at 14:37
  • Ctrl+Alt+F/B will work. (at least 10.15.6)
    – bongya
    Aug 24, 2020 at 2:23

9 Answers 9

43

If you want to re-enable them for the terminal

Terminal -> Preferences -> Keyboard

Check 'Use Option as Meta key'

3
  • 1
    This was clean and awesome! Sep 2, 2017 at 9:31
  • Yup. Saved the day. Thanks.
    – gsumk
    Aug 19, 2019 at 4:02
  • Thank you, this should be the accepted answer IMO. I've also found a lot of nice configurations on Terminal Preferences, no need to install iTerm or third party terminals I think. Jul 2, 2021 at 1:04
30

my iTerm2 version is 3.1.6, profile -> keys -> set Esc+

enter image description here

1
28

Maybe one of these helps? (I don't use OSX myself, so I can't test anything).

Summary from first URL:

Create file ~/Library/Keybindings/DefaultKeyBinding.dict with the following content and restart all apps.

{ "~d" = "deleteWordForward:"; "^w" = "deleteWordBackward:"; "~f" = "moveWordForward:"; "~b" = "moveWordBackward:"; }

6
  • 1
    I found a prepared file with a couple more bindings here: github.com/fkchang/emacs-keybindings-in-osx
    – rerx
    Apr 20, 2017 at 8:26
  • 1
    @amdev, perhaps newer versions of OSX have changed the behaviour. If another answer works for you, perhaps you could add an additional comment indicating which version of OSX you are using, and pointing to the solution which worked for you.
    – phils
    Feb 8, 2018 at 13:23
  • I use High Sierra 10.13.2
    – amdev
    Feb 12, 2018 at 8:39
  • It seems that Xcode only works for moveWordBackward and deleteWordForward, I am also using High Sierra
    – code4j
    Feb 27, 2018 at 2:54
  • 1
    This still works in OSX Catalina 10.15.6. Just be sure to restart any app in which you want to use the key bindings. Sep 24, 2020 at 16:23
10

I'm a little confused if you're talking about the keys (within different apps on OS X) or specifically Emacs. On OS X the standard keys you would use to jump between words is:

option/alt + Left Arrow or Right Arrow

If it's specific to Emacs for OS X:

For Terminal.app, starting from Snow Leopard: Go to Preferences > Settings > Keyboard > Use option as meta key.

For iTerm: Go to Manage Profiles > Keyboard profiles > (your profile) > Option Key as…

If you want to use Option key for both international characters and Meta key in Emacs, see info below for iTerm 0.10.x patch and/or binary.

Alt-b and Alt-f for navigating by word also works for your shell now.


UPDATE: The link the original website providing the patch is no longer online, however I was able to retrieve the cached information from the archive.org:

OSX + terminal + Meta key + national characters

Quite a mix, right? If you’re user of one of those keyboard layouts that have hidden some non-ISO1 characters under Option-key shortcuts and you use command line – read on, I might have a treat for you.

As it stands today, if you’re OSX user, and would like to use your Option (Alt) keys for both national characters and Meta-* shortcuts, you’re in for an unpleasant surprise – you can’t. It’s one or the other: both iTerm and Terminal.app allow you to flip between Option keys role, but neither of them allows you to use Option for both. Under Linux, it’s customary to use left Alt for Meta-* and right Alt for any national characters you might require. Same goes for Windows. Of course you can just train yourself in using ESC key in a prefix manner, but that’s avoiding the real problem.

Getting annoyed by this state of things, and faced with some Emacs usage, I decided to take advantage of the fact that iTerm is opensource software, and see if I can make a tiny change, to cater for my needs. Surely enough, after ~25 minutes I got a trivial patch: just changed the mask that iTerm uses for detecting that Option key is pressed down. It took few hours and 5 chapters of Cocoa book, to turn that into fully-fledged option in Keyboard Profiles section of iTerm. This was my first foray into lands of Objective C; while I came back victorious, I don’t see myself writing anything bigger in Objective C in nearest future.

Here’s the meat: ( https://sourceforge.net/p/iterm/patches/44/ )

  • “right Option is Normal” patch;
  • full patch allows you to pick which keys should be treated as Meta (both, only left, only right);

The patches were made against 0.10.x branch, at revision 1871.

Once you have the binary, launch it and go to Bookmarks > Manage profiles > Keyboard profiles. If you select a keyboard profile, you should see a new dropdown at the bottom of the keybindings list.

iTerm

The options should be pretty much self-explanatory. The configuration shown above is the one I’m using — this way I get all Meta-* shortcuts with my left Option key, and all Polish characters with right Option key. Which is the way it Should Be™ :)

I haven’t heard from iTerm authors yet, so I have no idea whether this patch is going to merged upstream or not. I’ve been using my patched version for almost week and a half now, and nothing major exploded. Yes, iTerm is slower to redraw the screen on larger terminals (say, 130×80), but there are certain features (like 256 colors support) that make up for that.

more info: http://www.emacswiki.org/emacs/EmacsForMacOS

3
  • "Use option as meta key" means losing the ability to type the hash (#) character using Alt+3, which is pretty annoying if typing comments into a Bash script or similar.
    – user82216
    Aug 27, 2014 at 17:17
  • I just get ?'s when I try this. Using I-term
    – JustGage
    Jul 1, 2015 at 18:20
  • the site you posted is no longer available. Mar 20, 2016 at 9:22
3

On iTerm2 3.0.15, go to:

Preferences -> Default -> Keys tab ->"Left option() key acts as:" 

and change the value to:

+Esc
3

Solved this at the OS level as follows,

⟶ System preferences
⟶ Keyboard
⟶ Input Sources Tab
+ at bottom left
Others in the left panel
⟶ add and select Unicode Hex Input from right panel

 ✓  Mac no longer prints anything on alt+letter|number
 ✓  enter unicode characters via alt+[code]
 ✅ the OS does not grab alt key shortcuts!

3
  • @Arg glad it helped. If this worked for you, you can upvote the answer so that other users can see that it was helpful. Feb 5, 2021 at 13:09
  • I really love that this is still an active question 7 years later! I'm not satisfied personally with this solution (I like some of the alt+letter/number combinations like adding diacriticals to vowels (eg <alt-u, o> to make ö) or alt+0 for degree symbol). I just want to punch out alt d, f, w, b for emacs functions like OP. Will update if I find the answer
    – Dick Fox
    Mar 24, 2021 at 22:29
  • Doesn't work for me. Now alt + b starts Unicode compose, so next few following keys makes a Unicode glyph. However I can use alt + Left to get this shortcut in every app other than Iterm where it's configured properly.
    – Abram
    Apr 20, 2022 at 2:53
1

You should use KeyRemap4MacBook. In the Emacs Mode section, you will find numerous options to use the Option key as Meta including Option+BF to Option+Left/Right.

However, if you're a hardcore Dvorak user like myself who wants to use key bindings such as M-b and M-f using the original Dvorak layout, there's a problem. You can only "move backward/forward one word" in QWERTY layout. I use version 8.4.0 so I don't know about the latest version for Mavericks.

0

I use BetterTouchTool. Works like this:

enter image description here

Basically remaps Option-F and Option-B to Option-Right and Option Left, and I turned this on for any apps that I want the shortcut in, e.g., Firefox, Obsidian, Slack. Works great!

0

For Hyper you can fix the option key by setting the following configuration to your ~/.hyper.js file:

module.exports = {
  config: {
    modifierKeys: { altIsMeta: true }
  }
}

source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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