Today I looked at my ~/.emacs file and I saw that I defined a lot of aliases for commands I frequently use. Most of the time the aliases are words that are built by taking each first char of a word in the command, e.g. nsn for newsticker-show-news, eb for emms-browser or omm for outline-minor-mode. But what should I do when I want to create an alias for ediff-buffers since eb is already used?
My solution was to write a function that calls a command by its abbreviation.
(defun th-execute-abbreviated-command (prefixarg) "Queries for a command abbreviation like "mbm" and calculates a list of all commands of the form "m[^-]*-b[^-]*-m[^-]*$". If this list has only one item, this command will be called interactively. If there a more choices, the user will be queried which one to call. The PREFIXARG is passed on to the invoked command." (interactive "P") (let* ((abbrev (read-from-minibuffer "Command Abbrev: ")) (regexp (let ((char-list (append abbrev nil)) ;; string => list of chars (str "^")) (dolist (c char-list) (setq str (concat str (list c) "[^-]*-"))) (concat (substring str 0 (1- (length str))) "$"))) (commands (remove-if-not (lambda (string) (string-match regexp string)) (let (c) (mapatoms (lambda (a) (if (commandp a) (push (symbol-name a) c)))) c)))) (cond ((> (length commands) 1) (call-interactively (intern (if ido-mode (ido-completing-read "Command: " commands) (completing-read "Command: " commands))) t)) ((= (length commands) 1) (call-interactively (intern (car commands)) t)) (t (message "No such command.")))))
To make it quickly accessible I’ve bound it to C-x x.
(global-set-key (kbd "C-x x") 'th-execute-abbreviated-command)
Alex Schröder pointed me to the builtin partial-completion-mode which allows nearly the same. It requires that you type the separator (- or _), too, so you need a few keystrokes more when you know the commands name. On the other hand it completes not only function names, but also files, e.g. f_b.c will expand to foo_bar.c.
The version above uses ido-completing-read when ido-mode is enabled.