Become an Emacs Super User with Global Keys

Resources: https://emacslife.com/how-to-read-emacs-lisp.html#orgheadline29

Introduction

global-set-key "binds" a key sequence to a command globally across Emacs. It means that no matter what mode you are in, pressing the specified key sequence will invoke the associated command, unless the mode has a specific overriding binding for that key sequence.

(global-set-key (kbd "C-c a") 'org-agenda)

This means essentially: Globally bind the following converted (kbd) key(s) to function X.

Setting a key interactively

M-x global-set-key

  • the keyboard shortcut you would like to set
  • the name of the function you would like Emacs to call

Note that the function must be interactive, that is, it must be something you can call with M-x and it should include (interactive …) in its definition.

For example, try M-x global-set-key, then press <f10>, and assign it to save-buffer. After you do that, you should be able to press <f10> to save the current buffer.

If you like a key-binding that you've interactively set, use C-x ESC ESC (repeat-complex-command) to see the Emacs Lisp code for it. Copy and paste that into your configuration.

How to type certain characters

In addition, there are some special characters: RET, SPC, TAB, ESC must be written in uppercase to mean the special keys they refer to.

Use angle brackets to refer to <return>, <up>, <down>, <left>, <right>.

Function keys are enclosed in angle brackets: <f1>.

There are several different ways to specify keys:

(global-set-key (kbd "M-/") 'hippie-expand) kbd notation
(global-set-key (kbd "^s") 'isearch-forward-regexp) or kbd + ^ where ^ equals "Control"

(global-set-key "\M-/" 'hippie-expand) string notation (does not allow spaces)
(global-set-key "\C-s" 'isearch-forward-regexp)

(global-set-key [?\M-/] 'hippie-expand) vector notation
(global-set-key [f10] (quote save-buffer))

Vector notation is preferred if you want to set your keyboard shortcuts interactively.

Otherwise use kbd for ease of use.

This makes shortcuts easily understandable and is also forgiving when it comes to whitespace.

It will accept "C-x C-s".

Use multiple keystrokes and save keys

(global-set-key (kbd "<f9> a") 'read-aloud-from-cursor)
(global-set-key (kbd "<f9> b") 'stop-reading-aloud)

Shortcut keys assigned to users

The prefix C-c followed by a lowercase or uppercase letter is reserved for users, as are the function keys <f5> to <f9>. Other keyboard shortcuts are likely to already be defined by modes. You can override them if you want.

Boilerplate snippets

In addition to setting keyboard shortcuts to functions, you can also define keys to expand to a longer sequence of keystrokes. You can use this for inserting strings or calling keyboard macros.

(global-set-key (kbd "<f7> g") "(global-set-key (kbd \"C-c a\") 'org-agenda)")
(global-set-key (kbd "<f7> s") "#+STARTUP: showall")

Note: If this gives you an error then f7 is already assigned to another function. One cannot assign a multi-key shortcut if the key is already assigned to a function.

If it is a function you have defined in dot Emacs, remove it. If it is a build in assigning, one "unsets" the key.

To unset a key, bind it to nil or use the function global-unset-key.

(global-unset-key (kbd "C-z"))
(global-unset-key (kbd "<insert>"))

In Emacs 29

Starting with Emacs 29, global-set-key is declared legacy and supposed to be replaced with keymap-global-set.

(keymap-global-unset "<f1>") ; unset first to use as prefix key
(keymap-global-set "<f1> f" 'counsel-describe-function)
(keymap-global-set "S-<f4>" 'query-replace-regexp)

Return to Home