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

;; 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 ErgoEmacs Keybinding: a Ergonomics Based Keyboard Shortcut System or Emacs: Xah Fly 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")))

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]

Emacs Keybinding Tips

  1. Add Custom Keys to Enhance Productivity
  2. isearch by Arrow Keys
  3. Bind Number Pad Keys
  4. 1 Key for M-x
  5. Hotkey to Open File Fast
  6. Add Web Browser Back Key/Button to Info mode
  7. Ctrl+x and Dvorak

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp