DEV Community

Cover image for How to use CMD as a leader key on Spacemacs
Laura Viglioni
Laura Viglioni

Posted on

How to use CMD as a leader key on Spacemacs

Okay... Maybe the title was a little bit of a clickbait, although it is indeed possible to do that, there is a price...

The problem

In Spacemacs or Emacs, in general, is not possible to bind commands or anything to a modifier key alone like meta, control, super, shift, command (macOS) etc and sometimes is very useful to bind that one button you barely use to some important functionality.

In my case I use Spacemacs, there are two very important commands I use a lot:

M-m: the leader key, i.e., the key that opens the default spacemacs menu, SPC if you use... the evil mode urgh...
C-M-m or M-m m: the major mode leader key, that opens the major mode menu, in evil mode the default binding is ,.

You can't normally bind these functionalities to modifier keys, you can bind M-[something], but not M- alone, but there is a way around...

The trick

After some hours suffering from this problem, I've come to a solution I've found here. If you can't bind something to a modifier key, change that key so it will not be a modifier key anymore...

You can bind in your system that key, in my case, right-cmd and right-option, to unused keys like f19 and f17. You can do it with software like xmodmap for GNU/Linux or karabiner for macOS.

Example with Karabiner

Karabiner print

I've switched my right-cmd and right-option to f19 and f17. It's not the point of this text, but I really recommend that anyone who codes to switch capslock with control.

In my .spacemacs file:

(defun dotspacemacs/init ()
  ;; ...
  dotspacemacs-emacs-leader-key "<f19>"
  ;; ...
  dotspacemacs-major-mode-emacs-leader-key "<f17>"
  ;; ...
Enter fullscreen mode Exit fullscreen mode

Now I can access these two menus with one key :)

The leader key:

leader key example

The major mode leader key, in this case in org-mode:

major mode leader key example

One might say that in evil mode I'd have access to those menus in one key but you have to leave editing mode to do that, so it would cost you more than one key to achieve that.

The price

The only problem with this trick is that while in Emacs or not, you've lost those keys on your keyboard, or at least if you don't add any new binds using those f17 or f19 keys.


These changes improved a lot my life while I'm coding, what used to take me three keys to do I do in just one, and these are two of my most used shortcuts, it is not called leader key for nothing... haha.

I hope that it can help some of you as well :)

Be safe, use masks and use Emacs

Credits for the header picture
The gifs here are from Lucifer TV series

Top comments (1)

jehaby profile image
Stas Makarov • Edited

It's better to use osx-layer.

(osx :variables
          osx-swap-option-and-command t)
Enter fullscreen mode Exit fullscreen mode