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

;; undo
(global-set-key (kbd "C-z") #'undo)
;; Mac style redo. (undo-redo is new in emacs 28)
(global-set-key (kbd "C-Z") #'undo-redo)

;; Windows style redo
(global-set-key (kbd "C-y") #'undo-redo)

Note: use Microsoft Windows style redo key. That's better. Less key strokes.

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

(global-set-key (kbd "<f1>") #'undo)
(global-set-key (kbd "<f2>") #'mark-whole-buffer)
(global-set-key (kbd "<f3>") #'xah-copy-line-or-region)
(global-set-key (kbd "<f4>") #'yank) ; paste

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

Mac Style Home/End Keys

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

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, use Emacs: Xah Fly Keys

Open Frequently Used Files

Here's examples of defining keys to open frequently used files. ( for more than 10 files, better use Emacs: Bookmark)

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

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

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

Template Insertion

Define keys to insert text you use frequently. Header, footer, signature, copyright template, etc. If you want a systematic template system, better is abbrev mode. [see Emacs: Abbrev Mode by Lisp Code]

(global-set-key (kbd "<f5> h") #'my-insert-header)

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