Emacs: Add Custom Keys to Enhance Productivity

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

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. 〔➤ 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. 〔➤ 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 Menu Key to M-x

Emacs: How to Define Menu/App Key

Open Frequently Used Files

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

(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")))

Other ways to open file fast:

  1. Bookmark
  2. Switch Buffers, Ido
  3. Open File Fast: recentf-mode
  4. List Buffers, ibuffer

Also: Emacs: Hotkey to Open File Fast.

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.el.

Template Insertion

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

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

;; example. template insertion command
(defun 'my-insert-copyright ()
  "Insert copyright header."
  (interactive)
  (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. 〔➤ Using Emacs Abbrev Mode for Abbreviation

Adding Web Browser Nav Keys to Info-mode

Emacs: Info-Mode Keys, Adding Web Browser Backspace Key, Mouse Back Button

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

Use Numberpad as Programable Function Keys

Emacs: Using Number Pad Keys

Like it?
Buy Xah Emacs Tutorial
or share
blog comments powered by Disqus