Is there a way to use the Apple command key in Emacs in the terminal? It would be nice to expand the set of easy to get at key bindings I can use.
I am using macOS Catalina, and iTerm2 3.3. This method probably works for several prior versions of macOS and iTerm2 but I have not researched it.
Note that when you remap the modifier keys below using iTerm2, will need to allow iTerm2 to "control" the OS (or take care of it when iTerm2 asks you to allow it) by checking iTerm under Security & Privacy->Accessibility. You can uncheck it after the procedure is done if that is an issue.
First, remap the Left Option key in your Profile to Esc+. This allows a terminal Emacs to use the Left Option (Alt) key as Meta. To do this:
a. In iTerm2 Preferences, click on Profiles, then on your default Profile, then click the button next to Esc+.
Then, while still in Preferences click Keys->Remap Modifiers. This will allow you to switch the Command key with the Alt Key. After you do this, a terminal Emacs will use the Command key to send Meta- to Emacs.
a. Remap left command key to Left Option.
b. Remap left option key to Left Command.
Note that while you are using terminal Emacs in iTerm2 you will have to use the Option key to do whatever MacOS believes is mapped to the Command key, for example, to Tab between Spaces. Once you are out of iTerm2 the Command key works normally again.
cmd-key-happy will do this for Terminal - and other applications.