TransWikia.com

Bind Meta key to Command on Mac Without Altering its System-wide Behaviour

Emacs Asked by aleks224 on September 2, 2021

To switch between tasks on an unaltered MacOS Mojave (OS X 10.14.6), one uses command-tab, which is convenient when done with the thumb finger. Now, in order to turn on the meta key inside Terminal (to allow e.g. skipping words with the cursor inside bash backward and forward emacs-style), I can tick the ‘use Option as Meta key’ in Terminal.app preferences.

This setup leaves me with option as meta key inside emacs, which is not ergonomic, as the key is physically too much on the left and my thumb finger needs to bend to get there.

I would like to:

  • leave the command-tab system-wide key unaltered, in order to be able to exit the emacs context into another task
  • still have the command key to be the meta key in emacs (so that, e.g., meta-f and meta-b hop over words)
  • all of this while working inside terminal emacs, not emacs with GUI variant

Some things I tried:

  • swapping the command and option buttons in System Preferences/Keyboard/Modifier keys. This results in a need to press option-tab for the task manager
  • setting the mac-command-modifier variable, as explained here, however my emacs 26.3, installed via brew install emacs, doesn’t seem to know about this variable (ctrl-
    h-v doesn’t recognize it at all and changing the variable doesn’t have any effect)

Can this be done? In other words, is there a way to have the command key be meta in emacs and yet have command-tab still switch between tasks system-wide?

One Answer

In non-windows mode, changes to ns-* variables (such as ns-alternate-modifier and ns-command-modifier) are ignored. In particular, if the -nw option is passed as argument to emacs, the logic that propagates changes to ns-* variables (implemented in src/nsterm.c) is not evaluated. See the call to init_display in the src/emacs.c in the Emacs 27.1 source code. Roughly, it'll evaluate stuff from nsterm.c only if the window system is not inhibited (controlled by the inhibit_window_system variable which is set if the -nw flag is passed) and otherwise rely on src/term.c. If the window system is inhibited, the ns-* variables will still be visible - this is because syms_of_nsterm is invoked regardless of the inhibit_window_system variable at emacs.c:1930. The variables are there but have no bearing on the actual key mappings.

As such when windows are inhibited, attempting to resolve this with ns-* is a dead end. Iterm2 and its remapping features can be of help here until there's a better way:

enter image description here

..which simply remaps the left command key to left option key.

Also, in Preferences -> Profiles -> Keys, Left option key needs to be mapped to Esc+.

Answered by aleks224 on September 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP