Emacs: Add Custom Keys to Enhance Productivity

Master emacs+lisp, benefit for life. Testimonials. Thank you for support.
, , …,

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

I recommend the following binding. Your hands will thank you after 10 years.

(global-set-key (kbd "<f2>") 'ergoemacs-cut-line-or-region)
(global-set-key (kbd "<f3>") 'ergoemacs-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)

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

The PC keyboard's ▤ Menu key (aka “App key”) by default calls execute-extended-commandAlt+x】 in GNU Emacs on Linux. On Microsoft Windows, you can define it yourself, like this:

;; Set the menu/apps key to do emacs's M-x, if on Windows
(cond
 ((string-equal system-type "windows-nt")
  (global-set-key (kbd "<apps>") 'execute-extended-command)
  )
 ((string-equal system-type "darwin")   ; Mac
  t )
 ((string-equal system-type "gnu/linux")
  t ) )

Note: On the Mac OS X, if you are using a PC keyboard, the ▤ Menu key will type 【Ctrl+p】 instead. You can fix by either just set a command to it like this (global-set-key (kbd "C-p") 'execute-extended-command) (if you don't use 【Ctrl+p】), or you can use PCKeyboardHack util to set the key properly. See: Mac OS X: Keyboard Layout, Keymapping, Keybinding, Software ⌨.

Open Frequently Used Files

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

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

Sample unicode template: unicode.txt. 〔☛ Emacs: Unicode Tutorial

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

(global-set-key (kbd "<f8> <f8>") (lambda () (interactive) (find-file "~/git/xah_emacs_init/xah_emacs_keybinding.el")))

(global-set-key (kbd "<f8> <f7>") (lambda () (interactive) (find-file "~/web/xahlee_info/js/blog.html")))

(global-set-key (kbd "<f8> <f6>") (lambda () (interactive) (find-file "~/web/xahlee_info/comp/blog.html")))

(global-set-key (kbd "<f8> <f5>") (lambda () (interactive) (find-file "~/web/ergoemacs_org/emacs/blog.html")))

Related emacs features for opening files are:

I use them all. But, nothing beats a simple key to open specific files. All the others you have to type lots of keys, and then eyeball for a second. Define single key to open specific file is muscle memory.

See also a single command to open frequently used files fast: Emacs Lisp: Hotkeys to Open File Fast.

Use Numberic-Keypad as Programable Function Keys

Set single key open ibuffer, bookmark, or recently opened file list.

;; some number pad keys to open file lists
(global-set-key (kbd "<kp-1>") 'recentf-open-files) ; numberic keypad 1
(global-set-key (kbd "<kp-2>") 'bookmark-bmenu-list)
(global-set-key (kbd "<kp-3>") 'ibuffer)
Apple iMac Keyboard A1243-numpad
Numpad on Apple Keyboard amazon img src

See that large useless numberic-keypad on your keyboard? It's your best friend. It adds ≈15 programable keys! 〔☛ Computer Keyboard: Using Number Keypad as Function Keys

Note: On Mac OS X, defining keys for the numeric keypad may not work. You might need KeyRemap4MacBook and or PCKeyboardHack. See: Mac OS X: Keyboard Layout, Keymapping, Keybinding, Software ⌨.

Insert Special Characters

Hotkeys 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 "<f9> <right>") "→")
(global-set-key (kbd "<f9> a") "α")
(global-set-key (kbd "<f9> b") "β")
(global-set-key (kbd "<f9> t") "θ")

See: Emacs: How to define Super & Hyper KeysEmacs: Remapping Keys Using key-translation-mapEmacs: Unicode Tutorial.

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

Template Insertion

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

(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 a systematic template system, you should use abbrev mode 〔☛ Using Emacs Abbrev Mode for Abbreviation〕 or YASnippet. 〔☛ Emacs Templates with YASnippet

Adding Web Browser Nav Keys to Info-mode

You can make your mouse's thumb buttons do backward and forward. See: Adding Web Browser Nav Keys to Info-mode.

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

Like what you read?
Buy Xah Emacs Tutorial
or share some
blog comments powered by Disqus