Emacs Custom Keybinding to Enhance Productivity

Perm url with updates: http://xahlee.org/emacs/emacs_useful_user_keybinding.html

Emacs Custom Keybinding to Enhance Productivity

Xah Lee, 2005, ..., 2011-02-24

Here are some practical emacs keybinding suggestions to enhance productivity. If you don't know the basics of how to define keys, see: Emacs: How to Define Keyboard Shortcuts.

Insert Special Characters

You can create a shortcut to insert frequently used unicode characters. You can add few chars, or a systematic character set.

;; example of systematic keys to insert math symbols
(global-set-key (kbd "<kp-6>") "→") ; numeral keypad 6
(global-set-key (kbd "M-i a") "α") ; Alt+i a
(global-set-key (kbd "M-i b") "β")
(global-set-key (kbd "M-i t") "θ")
;; some Hyper keys to insert common unicode chars
(global-set-key (kbd "H-3") (lambda () (interactive) (insert "•"))) ; bullet
(global-set-key (kbd "H-4") (lambda () (interactive) (insert "◇"))) ; white diamond
(global-set-key (kbd "H-$") (lambda () (interactive) (insert "◆"))) ; black diamond
(global-set-key (kbd "H-5") (lambda () (interactive) (insert "†"))) ; dagger
(global-set-key (kbd "H-%") (lambda () (interactive) (insert "‡"))) ; double dagger
(global-set-key (kbd "H-6") (lambda () (interactive) (insert "▸"))) ; small right pointing triangle

If you do math a lot, you might try a more complete system. See: Emacs Unicode Math Symbols Input Mode (xmsi-mode).

See also: Emacs and Unicode Tips.

To define Hyper and Super key, see: Emacs: How to define Hyper & Super Keys.

Remap Frequently Used Shortcuts

;; make cursor movement keys under right hand's home-row.
(global-set-key (kbd "M-i") 'previous-line) ; was tab-to-tab-stop
(global-set-key (kbd "M-j") 'backward-char) ; was indent-new-comment-line
(global-set-key (kbd "M-k") 'next-line) ; was kill-sentence
(global-set-key (kbd "M-l") 'forward-char)  ; was downcase-word

(global-set-key (kbd "M-SPC") 'set-mark-command) ; was just-one-space
;; type parens in pairs with Hyper and right hands's home-row
(global-set-key (kbd "H-j") (lambda () (interactive) (insert "{}") (backward-char 1)))
(global-set-key (kbd "H-k") (lambda () (interactive) (insert "()") (backward-char 1)))
(global-set-key (kbd "H-l") (lambda () (interactive) (insert "[]") (backward-char 1)))

For a more systematic change, see: ErgoEmacs Keybinding.

Template Insertion

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

(global-set-key (kbd "<f5>") 'insert-my-header)
(global-set-key (kbd "<f6>") 'insert-my-footer)
(global-set-key (kbd "<f7>") 'insert-signature)

;; example of defining a template insertion command
(defun 'insert-paragraph-tag ()
  "Insert <p></p> at cursor point."
  (interactive)
  (insert "<p></p>")
  (backward-char 4))

If you want elaborate template system for programing, you should use YASnippet instead. See: Emacs Templates with YASnippet.

Adding Web Browser Keys to Emacs

You can make your mouse's 4th and 5th button do backward and forward. See: Adding Web Browser Keys to Emacs.

Open Frequently Used Files

(global-set-key (kbd "<f5>") 'open-unicode-template)

(defun open-unicode-template ()
 "Open a file containing frequently used unicode chars"
 (interactive)
 (find-file "~/web/emacs/unicode.txt"))

I press 1 button on number pad to open ibuffer, bookmark, recently opened file list.

;; some number pad keys to open some files
(global-set-key (kbd "<kp-5>") 'recentf-open-files) ; numpad5
(global-set-key (kbd "<kp-7>") 'bookmark-bmenu-list)
(global-set-key (kbd "<kp-8>") 'ibuffer)

Here's some hotkey i use to open specific files i use few times a hour.

(global-set-key (kbd "<C-kp-9>") 'open-twitter) ; Ctrl+ numpad9
(global-set-key (kbd "<C-kp-6>") 'open-todo)

Note: depending on what OS and what emacs distro you are using, defining keys for the numeric keypad may not work.

You can see all my personal keybinding here: xah_emacs_keybinding.el (they change about weekly).

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs