How to use CMD as a leader key on Spacemacs
Tags: emacs
Published: 2021-01-17 01:34:55

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

<img width="70%" style="width:100%" src="https://media.giphy.com/media/xT9IgDk29h0Vye0jAs/giphy.gif">

# 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... 
And
`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
<img width="70%" style="width:100%" src="https://media.giphy.com/media/3o75271dzgbEbQKFe8/giphy.gif">

After some hours suffering from this problem, I've come to a solution I've found [here](https://emacs.stackexchange.com/questions/21892/bind-a-modifier-key-like-right-shift-to-a-command). If you can't bind something to a modifier key, change that key so it will not be a modifier key anymore...

<img width="70%" style="width:100%" src=https://media.giphy.com/media/l3vR84p7VZfRSx8kg/giphy.gif>



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](https://wiki.archlinux.org/index.php/xmodmap) or [karabiner for macOS](https://pqrs.org/osx/karabiner/). 

### Example with Karabiner

![Karabiner print](https://dev-to-uploads.s3.amazonaws.com/i/badkygvq54lbs58kzqxt.png)

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:

```elisp
(defun dotspacemacs/init ()
  ;; ...
  dotspacemacs-emacs-leader-key "<f19>"
  ;; ...
  dotspacemacs-major-mode-emacs-leader-key "<f17>"
  ;; ...
)
```

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

#### The leader key:
![leader key example](https://dev-to-uploads.s3.amazonaws.com/i/z78oyeneetoiy1tym57c.png)

#### The major mode leader key, in this case in `org-mode`:
![major mode leader key example](https://dev-to-uploads.s3.amazonaws.com/i/rxtzzb9kpymh0ezf8zy4.png)

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

<img width="70%" style="width:100%" src=https://media.giphy.com/media/l4KhTT9w5LlsOWxVu/giphy.gif>

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.

# Conclusion 

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
Xoxo 

<small>[Credits for the header picture](https://www.pexels.com/photo/close-up-photo-of-vintage-typewriter-1425146/)</small>
<small> The gifs here are from [Lucifer TV series](https://en.wikipedia.org/wiki/Lucifer_(TV_series))</small>