Emacs Keys: Key Sequence

By Xah Lee. Date: . Last updated: .

Define Key Sequences

In emacs, you can define key sequences.

Suppose you want:

(progn
  ;; define key sequence
  (define-prefix-command 'my-keymap)
  (define-key my-keymap (kbd "<f6>") 'visual-line-mode)
  (define-key my-keymap (kbd "<f7>") 'whitespace-mode)
  (define-key my-keymap (kbd "<f8>") 'toggle-frame-fullscreen)
  (global-set-key (kbd "<f9>") my-keymap))

Press the leader key, then you can press Ctrl+h to see all available key/commands that follows.

Add other commands you use often, for example:

More Examples of Key Sequence

;; Example of single key sequence
(global-set-key (kbd "<f7>") nil) ;  good idea to put nil to the starting key
(global-set-key (kbd "<f7> <f8>") #'calendar)
(global-set-key (kbd "<f7> <f9>") #'calc)

;; Example of sequence of single or chord keys
(global-set-key (kbd "C-e") nil) ; good idea to put nil to the starting key
(global-set-key (kbd "C-e a") #'calendar)
(global-set-key (kbd "C-e SPC") #'calendar)
(global-set-key (kbd "C-e C-a") #'calendar)

Typing Symbols Without Pressing Shift

(progn
  ;; define key sequence to type symbols
  (define-prefix-command 'my-symbols-map)
  (define-key my-symbols-map (kbd "1") "!")
  (define-key my-symbols-map (kbd "2") "@")
  (define-key my-symbols-map (kbd "3") "#")
  (define-key my-symbols-map (kbd "4") "$")
  (define-key my-symbols-map (kbd "5") "%")
  (define-key my-symbols-map (kbd "6") "^")
  (define-key my-symbols-map (kbd "7") "&")
  (define-key my-symbols-map (kbd "8") "*")
  (define-key my-symbols-map (kbd "0") ")")
  (global-set-key (kbd "<f9>") my-symbols-map))

Reference