4

So, in Mac OSX, I'm trying to switch to using Emacs in Terminal.app, but things like C-M-SPC and C-M-% don't work. After a bit of googling I came across this (mostly) unanswered question

https://superuser.com/questions/83166/using-c-m-to-do-a-query-replace-regexp-in-emacs-running-in-mac-terminal

Other discussions such as this one and this one point out that this is basically the terminal emulator's fault, which I can understand.

But is there really no workaround? The last answer in the superuser question https://superuser.com/a/84008/218146, seems promising, but I don't understand it fully. Can you really make Emacs read those events? Perhaps a working (if probably elaborate) hack could be developed on top of it.

Finally, I could use another terminal emulator. The only requirement is that I can keep using the "Option/Alt" key as the Meta key. Any suggestions?

EDIT: I just found out that the "esc" key works, i.e., I can input C-M-SPC perfectly with it. So a more than reasonable hack for me would be to remap Left Option to ESC in Terminal.app. Anyone know how to do that? EDIT2: I spoke too soon: I tried out Karabiner (https://pqrs.org/osx/karabiner/) and remapped LeftOption to ESC. A single C-M-SPC works alright, but multiple SPC's after that don't. So it's mostly useless... :-(

joao
  • 245
  • 1
  • 8
  • I hate to say this, but trying to solve that exact set of problems lead me to the [Emacs for OSX](https://emacsformacosx.com/) GUI, which has been about a billion times easier to get configured pleasingly. I haven't found a better answer than that myself :/ – Gastove Apr 02 '16 at 18:57
  • Did you notice that "esc" works perfectly??? If it weren't so oddly placed. I'm thinking I'm gonna mechanically hardwire a different keyboard just for using the terminal. Or maybe I'll check out Karabiner (https://pqrs.org/osx/karabiner/) – joao Apr 02 '16 at 19:01
  • Huh. I use iTerm2, and ESC still doesn't get me what I want out of this stuff. The ones that killed me were all smartparens stuff -- C-( and C-), which I could never get to work. Might check out karabiner tho! – Gastove Apr 02 '16 at 19:07
  • Just tried out Karabiner. It does what it says, but I spoke too early. Now a single C-M-SPC works, but multiple SPC's after that don't send multiple C-M-SPC's to Emacs... So mostly useless... – joao Apr 02 '16 at 19:09
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/5628) – Drew Apr 03 '16 at 01:30
  • 1
    @Joao Tavora `C-[` usually works as an alias for ESC, and it's easier to reach. – Low Powah May 09 '16 at 00:56

3 Answers3

3

So, I'm going to answer my own question. This is a reasonable workaround for me:

  1. edit Terminal.app's preference plist. it's a binary plist so you have to convert it first to text

    $ plutil -convert xml1 ~/Library/Preferences/com.apple.Terminal.plist`
    
  2. Now, find the keyMapBoundKeys section, should have stuff like this

    <key>keyMapBoundKeys</key>
        <dict>
            <key>$F702</key>
            <string>[1;2D</string>
            ...
        </dict>
    
  3. Add the keybinding you're missing in Emacs. How? See http://heisencoder.net/2008/04/fixing-up-mac-key-bindings-for-windows.html. To add C-M-SPC, I had to use <key>~^0020</key> (0020 is ASCII 32, or space, in hex. In the <string> portion, use a sequence of escapes. I used "double escape space", or <string>\033\033 </string> but perhaps something else would be smarter.

  4. In terminal emacs, your keybinding is now recognized finally, but emacs sees it as something else, in this case ESC M-SPC. No problem, I bind that to mark-sexp and done!

1-3 could have been avoided if Terminal.app provided a decent interface to add keybindings. As I write this only keybindings to function keys can be added via the Terminal.app's Preferences dialog.

Also, 4 can probably be done a lot better: Problems with keybindings when using terminal explains that one should use a special keymap for this and maybe follow some standard for escape sequences.

But for the moment, I'm good

joao
  • 245
  • 1
  • 8
3

Unfortunately my Macbook broke last year ... (and working with Emacs was a breeze)

Use iTerm

... I don't remember problems with C-M- combinations, maybe because I used iTerm (https://www.iterm2.com/). But because i found the pressing the keys together uncomfortable, i also used the following solution:

ESC remap

I used Karabiner (https://pqrs.org/osx/karabiner/) to map Caps Lock to /both/ Esc (tap) and Ctrl (hold), this makes even vi usable ;-)

You just have to enable options like "Control_L to Control_L" and "when you type Control_L only, send Escape" in Karabiner, and in the Keyboard preference pane, click "Modifier Keys…", and change Caps Lock to Control.

See also http://stevelosh.com/blog/2012/10/a-modern-space-cadet/#controlescape


btw: I hope you use a recent version of Emacs, even if you prefer the terminal, the version that comes with Mac OS is usually ancient.

You can install recent GNU emacs via homebrew http://brew.sh/index.html, standalone from https://emacsformacosx.com/, or as a os-x-ified distribution http://aquamacs.org/

Vera Johanna
  • 873
  • 6
  • 9
-2

Install evil, evil-leader, and set up your commands' key bindings to use leader key as much as possible. This solution works everywhere (mintty/cygwin on windows, xterm/rxvt on Linux, terminal.app on OSX). spacemacs uses similar leader key solution, so the easiest way is use spacemacs.

chen bin
  • 4,781
  • 18
  • 36