Emacs: Add Custom Keys to Enhance Productivity

By Xah Lee. Date: . Last updated: .

Here are some practical emacs keybinding suggestions to enhance productivity.

Undo, Redo

(global-set-key (kbd "C-z") 'undo) ; 【Ctrl+z】

;; Mac style redo
(global-set-key (kbd "C-Z") 'redo) ; 【Ctrl+Shift+z】

;; Windows style redo
(global-set-key (kbd "C-y") 'redo) ; 【Ctrl+y】

For redo, you'll need to install a redo mode. [see Emacs: Best Undo/Redo Mode]

The following single key shortcuts are good for avoiding Repetitive Strain Injury.

(global-set-key (kbd "<f2>") 'xah-cut-line-or-region)
(global-set-key (kbd "<f3>") 'xah-copy-line-or-region)
(global-set-key (kbd "<f4>") 'yank)       ; paste
(global-set-key (kbd "<C-f4>") 'yank-pop) ; paste previous
(global-set-key (kbd "<f5>") 'undo)
(global-set-key (kbd "<C-f5>") 'redo)

For the copy/cut commands, see: Emacs: Copy/Cut Current Line If No Selection.

Mac Style Home/End Keys

;; Linux/Windows  style
(global-set-key (kbd "<home>") 'move-beginning-of-line)
(global-set-key (kbd "<end>") 'move-end-of-line)

;; Mac style
(global-set-key (kbd "<home>") 'beginning-of-buffer)
(global-set-key (kbd "<end>") 'end-of-buffer)

Remap Cursor Movement Keys

The cursor movement commands are the most frequently used commands. [see Emacs's Command Frequency]. Make them easier to type.

;; make cursor movement keys under right hand's home-row.
(global-set-key (kbd "M-i") 'previous-line)
(global-set-key (kbd "M-j") 'backward-char)
(global-set-key (kbd "M-k") 'next-line)
(global-set-key (kbd "M-l") 'forward-char)

(global-set-key (kbd "M-u") 'backward-word)
(global-set-key (kbd "M-o") 'forward-word)

(global-set-key (kbd "M-SPC") 'set-mark-command)

For a more systematic change, see: ErgoEmacs Keybinding.

Set Key for M-x

Emacs: How to Define Menu Key

isearch by Arrow Keys

Emacs: isearch by Arrow Keys

Open Frequently Used Files

Here's examples of defining keys to open frequently used files.

 (kbd "<f8> <f8>")
 (lambda ()
   (find-file "~/.emacs.d/my-keybinding.el")))

 (kbd "<f8> <f7>")
 (lambda ()
   (find-file "~/web/my-unicode-template.html")))

 (kbd "<f8> <f6>")
 (lambda ()
   (find-file "~/todo.org")))

Other ways to open file fast:

  1. Emacs: Open File Fast
  2. Bookmark
  3. Open File Fast: recentf-mode

Insert Unicode Characters

Insert frequently used Unicode characters. π λ

See: Emacs: Remapping Keys Using key-translation-map

If you do math a lot, you might try a more complete system. See: Emacs: Xah Math Input Mode.

Template Insertion

Define keys to insert text you use frequently. Header, footer, signature, copyright template, etc.

(global-set-key (kbd "<f5> h") 'my-insert-header)
(global-set-key (kbd "<f5> f") 'my-insert-footer)
(global-set-key (kbd "<f5> s") 'my-insert-signature)

;; example. template insertion command
(defun my-insert-header ()
  "Insert copyright header."
  (insert ";; This program is free software: you can redistribute it and/or modify ..."))

If you want a systematic template system, you should use abbrev mode. [see Emacs: Abbrev Mode Tutorial]

Adding Web Browser Nav Keys to Info-mode

Emacs: Add Web Browser Back Key/Button to Info mode

For general advice on defining keys, see: Emacs: How to Define Keys.

Use Numberpad as Programable Function Keys

Emacs: Bind Number Pad Keys

Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

Ask me question on patreon