59

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.

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
ayckoster
  • 6,707
  • 6
  • 32
  • 45
  • 1
    To clarify, you are *not* talking about a problem with Emacs itself, correct? – phils Nov 22 '13 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 '13 at 16:07
  • 2
    None of these worked for me. What did work is the [selected answer to this SO question][1]. [1]: http://stackoverflow.com/questions/21937061/custom-emacs-like-key-bindings-on-os-x-defaultkeybinding-dict-not-working-as-ex – garyp Feb 22 '14 at 14:37
  • Ctrl+Alt+F/B will work. (at least 10.15.6) – bongya Aug 24 '20 at 02:23

9 Answers9

44

If you want to re-enable them for the terminal

Terminal -> Preferences -> Keyboard

Check 'Use Option as Meta key'

Arran Cudbard-Bell
  • 5,912
  • 2
  • 26
  • 48
32

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

enter image description here

EasternXiang
  • 509
  • 6
  • 7
29

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:"; }

ayckoster
  • 6,707
  • 6
  • 32
  • 45
phils
  • 71,335
  • 11
  • 153
  • 198
  • 1
    I found a prepared file with a couple more bindings here: https://github.com/fkchang/emacs-keybindings-in-osx – rerx Apr 20 '17 at 08: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 08 '18 at 13:23
  • I use High Sierra 10.13.2 – amdev Feb 12 '18 at 08:39
  • It seems that Xcode only works for moveWordBackward and deleteWordForward, I am also using High Sierra – code4j Feb 27 '18 at 02:54
  • This works great for me outside of the terminal. Inside the terminal I already had this behaviour configured, but maybe you have to configure that separately (as documented in the other answers) if you want it there too. – achalk Aug 23 '18 at 13:30
  • 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. – Edward Anderson Sep 24 '20 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

l'L'l
  • 44,951
  • 10
  • 95
  • 146
4

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!

Edward Anderson
  • 13,591
  • 4
  • 52
  • 48
ultraGentle
  • 5,084
  • 1
  • 19
  • 45
  • 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 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 '21 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 '22 at 02:53
3

On iTerm2 3.0.15, go to:

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

and change the value to:

+Esc
user674669
  • 10,681
  • 15
  • 72
  • 105
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.

NigoroJr
  • 1,056
  • 1
  • 14
  • 31
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!

pete-may
  • 59
  • 1
  • 3
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

Graham
  • 13,165
  • 2
  • 16
  • 14