Aaron S. Hawley (aaronhawley) wrote,
Aaron S. Hawley
aaronhawley

Emacs custom global key bindings

The only place reserved for users to put custom global key bindings in Emacs is `C-c LETTER'. (Obviously, all Emacs key bindings can be changed at ones own peril and people often do.) Here's some examples.

(global-set-key (kbd "C-c r") 'query-replace-regexp)
(global-set-key (kbd "C-c i") 'insert-buffer)
(global-set-key (kbd "C-c w") 'write-region)

I also used to like having short aliases for `M-x'. Here's some popular ones used for working with Emacs Lisp.

(defalias 'tdoe 'toggle-debug-on-error)
(defalias 'eval- 'eval-region) ;; M-x eval
(defalias 'load- 'load-file) ;; M-x load
(defalias 'ff 'find-function)
(defalias 'fv 'find-variable)
(defalias 'fl 'find-library)
(defalias 'll 'load-library)

I've come across the strategy of using `C-c' and short words or phrases for key bindings rather than single keys.

(global-set-key (kbd "C-c now")
                (lambda ()
                  "Insert current universal time."
                  (interactive "*")
                  (insert
                   (format-time-string "%Y-%m-%d %H:%M UTC"
                                       (current-time) 'universal))))

(global-set-key (kbd "C-c file") 
                (lambda (file &optional args)
                  "Insert name of FILE into buffer after point."
                  (interactive "*fInsert file name: \nP")
                  (cond ((eq '- args)
                         (insert (file-relative-name file)))
                        ((not (null args))
                         (insert (expand-file-name file)))
                        (t
                         (insert file)))))

(global-set-key (kbd "C-c fill") 'fill-region)
(global-set-key (kbd "C-c delete") 'delete-region)
(global-set-key (kbd "C-c svn") 'svn-status)
(global-set-key (kbd "C-c git") 'magit-status)
(global-set-key (kbd "C-c compile") 'compile)
;; Same as M-x recompile
(global-set-key (kbd "C-c recompile") 'recompile) 

(global-set-key (kbd "C-c copy SPC line") ;; Repeat with `C-x z'
                (lambda ()
                  (interactive)
                  (forward-line 1)
                  (open-line 1)
                  (copy-from-above-command))) ;; From misc.el
;; Same as M-x copy above RET
(global-set-key "\C-ccopy above" 'copy-from-above-command)

For the seeing Emacs user, display the keys you've pressed earlier by modifying this variable to something closer to the speed of the human eye. By default, Emacs only shows the keys you've entered in the echo area after every one second.

(setq echo-keystrokes 0.02)

It's nice how Emacs automatically creates prefix key maps for these long key sequences. For instance, `C-c compile' and `C-c copy line' share the `C-c c' and `C-c co' prefixes. However, there is no way to use both single key bindings and the new longer ones simultaneously should they share letters. When they conflict, the shorter will get stomped by the longer version. For example, the previous single key binding for `C-c r' will not work since it gets shadowed by `C-c recompile'.

I'm using a mix of both short and long key bindings since there are some editing commands that I need to keep on a short key binding but some less frequently used commands on single key bindings that I couldn't remember easily. Although, it is a delicate dance of picking useful short phrases that don't conflict with single key bindings and vice versa. I'm not planning to get carried away with it all. I'm a satisfied user of M-x.

Tags: emacs, free software, howto
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments