10

I'm on OSX Lion

I running emacs inside of Terminal.app

I want command-x to result in M-x (and in general, command -> M)

I have tried the following solution and they're not what I want:

What I want, is only inside of Terminal.app (or only inside of emacs), to bind the command key to meta. How do I achieve this?

Community
  • 1
  • 1
  • 1
    If you map Cmd to Meta in Terminal.app, you won't be able to use Cmd-C, Cmd-V and other shortcuts to copy-paste text. And I think Cmd-W for closing tabs would also be disabled. – Erik Kaplun Jul 25 '13 at 11:40

5 Answers5

10

fwiw - iterm2 requires a bit of additional attention to make cmd map to meta and play well with other applications. (cmd-tab behavior, cmd-space (quicksilver), etc.) here's my config which biases towards left-handed cmd-tab operation, etc.

within iterm2

  • (preferences >> keys tab) left cmd needs to be remapped to left-option
  • global shortcut keys set the following to "do not remap modifiers"
    • cmd-space (makes quicksilver work normally)
    • ctl-cmd-space (makes my omnifocus binding work)
    • cmd-` - cycle windows
    • cmd- -> (right arrow) makes tab cycling work correctly
  • (preferences >> profiles tab >> keys subtab) i map left option-key to escape. this provides the expected behavior for most meta expecting shell applications

this results in using right-cmd for certain local iterm tasks but toggling between applications with different keybindings, etc, does the usual left-handed operational things and retraining my muscle memory was minimized.

Community
  • 1
  • 1
steve ulrich
  • 121
  • 4
  • 2
    This didn't work for me but I think it's just because the answer is out-of-date. Here's what did: in the Keys tab, set "Left Command" to be "Left Option" (and "Right Command" to "Right Option" as well if you use that one); then in the Keys tab under the Profiles tab, choose the "+Esc" options for "Left option key acts as" and "Right option key acts as" (I suspect these options may have been added just for emacs users). You will, however, lose the ability to type special characters using the option keys (but only in iTerm2, other apps including Cocoa emacs it will still work fine) – hoff2 May 28 '14 at 15:00
  • 1
    also the above is in conjunction with having the following in my emacs config: (setq mac-option-modifier 'none) (setq mac-command-modifier 'meta) (setq ns-function-modifier 'hyper) – hoff2 May 28 '14 at 15:05
4

First, in Terminal's preferences, on the Settings tab, on the Keyboard subtab, there's an checkbox "Use option as meta key". It's not exactly what you asked for, but it's built in.

I was thinking that Cocoa Key Bindings system might help here, but according to my searches, it deliberately doesn't permit remapping Command. This page has a lot of information about the customizability of Cocoa's key bindings and text defaults.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
4

FWIW: I found another way to accomplish this that's better for me (this is in iTerm2).

I wanted emacs to behave the same in the terminal as it does in a window. I have my emacs set to use the command key as the meta key via the following snippet:

;;osx keys
(setq mac-option-key-is-meta nil)
(setq mac-command-key-is-meta t)
(setq mac-command-modifier 'meta)
(setq mac-option-modifier 'alt)

So to get my command key behaving like a meta key in iTerm2, I went to preferences->profiles->->keys and added new keys. The only problem is you have to do it for each key separately, but you can say for example,

keyboard shortcut: <type command-p>
action: send escape sequence
Esc+: p

So I did that for my most frequently used meta+ keys. It would be nice if there was another set of radio buttons like there is for option/alt, where you could just say "command key acts like Esc+".

Jeff Gran
  • 1,585
  • 15
  • 17
3

I use cmd-key-happy (https://github.com/aim-stuff/cmd-key-happy) which allows you to swap alt and commmand keys for specific applications.

So in Terminal.app you can use all the meta modifiers as expected, while the Mac-specific commands (copy, paste, close, new) work with alt instead.

allyourcode
  • 21,871
  • 18
  • 78
  • 106
Juancho
  • 7,207
  • 27
  • 31
  • I've been using this for a while too, and have been happy with it :). Until recently, when my company enabled Secure Keyboard Entry for Terminal. I don't think I have a way to disable it. At least, hitting the menu option did not seem to change anything. Otherwise, I really like this soln. – allyourcode Sep 27 '12 at 05:57
-2

Why are you insistent on Terminal.app? Just install iterm2 and move on with life.

event_jr
  • 17,467
  • 4
  • 47
  • 62
  • I like this approach to the Gordian knot. –  Apr 13 '12 at 12:27
  • 2
    This doesn't answer the question about how to work with Terminal.app. It would be ok as a comment. – David J. Oct 30 '14 at 15:23
  • In a sense, it does answer the question, for those of us who did not know (or had forgotten) that there is an alternative (iterm) to Terminal, that allows what is requested here. I tried to upvote more than once, but to no avail. – davernator Apr 20 '21 at 21:56