How do I make `Cmd` as `Meta` instead of `Option/Alt` key on `emacs` runs on terminal(with `-nw` option)

A lot of questions and articles about this but I can't find a one that works on my system.

Modifying emacs configuration looks working on other people's but it doesn't work for me.

like this

(setq mac-command-modifier 'meta) ; make cmd key do Meta

Is this working on your system too?

Currently I have no idea except just swap thos two keys using by third party app which I don't want to.

Please help!


Solution 1:

Programs running in the terminal are rather limited in what keys they can get. Gui apps get all the keys. Normally Cmd will go to the gui app only, because there's no ascii equivalent.

  • Other people's configurations with mac-command-modifier (or ns-command-modifier depending on which version of Emacs you have) are for the gui version of emacs, Emacs.app.
  • Inside a terminal, it's the terminal app that decides what happens with Cmd. Typically Cmd is only used for the terminal's own features. Terminal.app offers no way as far as I can see to send Cmd to emacs -nw. If you're using iTerm2.app you can tell it to send option instead of left or right cmd, and then you can have option map to meta.
  • You can globally swap the two keys in System Preferences, but this will mean Cmd/Option are swapped in all other applications too.

Related: https://emacs.stackexchange.com/questions/32074/cannot-remap-alt-cmd-on-mac-os-in-terminal and https://emacs.stackexchange.com/questions/7761/use-the-command-key-in-terminal-on-osx/32480#32480

Solution 2:

I solved this problem on macOS 10.13.6 by creating a Profile in the Terminal app that maps the Option key to Meta.

To create a new Profile, open Preferences in the Terminal app and go to the Profiles tab. Select the default profile, then click on the + button at the bottom. In the new profile (which I named Emacs), go to the Keyboard tab and check Use Option as Meta key.

To use the new Profile, select Shell > New Window > Emacs, and run Emacs in the new window.