Terminal Emacs - enable the super key (and more)!

Hello!

I really enjoyed the “Making the Most of Terminal Emacs” stream.

My main problem with terminal emacs was always not being able to use the super key as my “leader key” for personal keybindings.

But there is a way! (besides waiting for ghostty, which might support everything out of the box)

The basic setup is really easy.

  1. Use a terminal emulator that supports the Kitty Keyboard Protocol (e.g. kitty, foot, wezterm, alacritty)

  2. Install the kkp package in emacs.

(use-package kkp
  :config
  (global-kkp-mode 1))

I haven’t dug into all the customization of kkp in emacs yet. But at the very least, it already lets me use the super key. It probably enables a lot of other special modifiers/combinations that normally don’t work in terminal emacs.

Shortlist of the other most useful things for me in terminal emacs:

;; enable terminal emacs yank/paste to use system clipboard
(use-package xclip
  :config
  (xclip-mode 1))

;; if using vertico+consult, use the minibuffer for completion in region
;; (terminal emacs doesn't support child frames)
(setq completion-in-region-function
      (lambda (&rest args)
        (apply (if vertico-mode
                   #'consult-completion-in-region
                 #'completion--in-region)
               args)))

~ fj

2 Likes

I’m experiencing an issue where the Meta key doesn’t function with Emacs in certain terminals.

I tried the ‘kkp’ package, it do enabled the Meta key in the following terminals:

  • iTerm on macOS
  • ssh to Termux on my Android phone using iTerm on macOS

I have only configured the Alt key to function as the Meta key in the terminal settings.

Ensuring Emacs operates efficiently in the terminal is of importance to me. At work, I’ve encountered several situations where I have to run Emacs in the terminal.

An important note:

Most terminal emulators (at least of the ones that support kkp) have a whole bunch of default keybindings that do obscure things, that will prevent the keypress from ever being sent to the terminal application (emacs).

Ctrl+Shift+letter combinations are especially common for terminal emulators to use for default keybindings.

Of course, you will need to disable/remap all of the terminal emulator bindings that you want to be able to use for emacs bindings.

(some of these terminal emulator bindings only do something visible in certain situations, so it can easily look like nothing is happening at all, and that the terminal emulator doesn’t even recognize the binding…so it is safest to check the default bindings that are being used, you can’t always use the command (e.g. C-v) to view the raw keypress that the emulator is receiving. For some reason, if it is an internal command, it doesn’t seem to always report it for the C-v raw output.)

I don’t use emacs on macOS, so I can’t test that setup.

I didn’t configure the meta/alt keys with kkp, or with my terminal emulator.

Did you use this setting in kkp? Emacs and the terminal may be out of sync in how they are using alt and meta?

(setq kkp-alt-modifier 'alt) ;; use this if you want to map the Alt keyboard modifier to Alt in Emacs (and not to Meta)

What about a TTY?

  • Does this works within a TTY? (I suppose it doesn’t, but it doesn’t make any harm to ask).
  • And possibly more important: Is there an alternative to get this functionality on a TTY?

I don’t have this setting in my init.el
(setq kkp-alt-modifier 'alt)
I will keep observing and do some experitment to understand kkp when I have time.

I don’t know how that would be possible…the TTY doesn’t support the KKP. You might have to write your own TTY by rewriting all of the low level keyboard input events to somehow recognize all of the modifier combinations as unique keyboard events.