Emacs: Use Alias for Fast M-x

By Xah Lee. Date: . Last updated: .

In emacs, you can use alias to shorten commands. For example, make frequently used commands just 1 or 2 letters.

Examples of useful alias:

(defalias 'yes-or-no-p 'y-or-n-p) ; y or n is enough
(defalias 'list-buffers 'ibuffer) ; always use ibuffer
;; make frequently used commands short
(defalias 'qrr 'query-replace-regexp)
(defalias 'lml 'list-matching-lines)
(defalias 'dml 'delete-matching-lines)
(defalias 'dnml 'delete-non-matching-lines)
(defalias 'dtw 'delete-trailing-whitespace)
(defalias 'sl 'sort-lines)
(defalias 'rr 'reverse-region)
(defalias 'rs 'replace-string)

(defalias 'g 'grep)
(defalias 'gf 'grep-find)
(defalias 'fd 'find-dired)

(defalias 'rb 'revert-buffer)

(defalias 'sh 'shell)
(defalias 'fb 'flyspell-buffer)
(defalias 'sbc 'set-background-color)
(defalias 'rof 'recentf-open-files)
(defalias 'lcd 'list-colors-display)
(defalias 'cc 'calc)

; elisp
(defalias 'eb 'eval-buffer)
(defalias 'er 'eval-region)
(defalias 'ed 'eval-defun)
(defalias 'eis 'elisp-index-search)
(defalias 'lf 'load-file)

; major modes
(defalias 'hm 'html-mode)
(defalias 'tm 'text-mode)
(defalias 'elm 'emacs-lisp-mode)
(defalias 'om 'org-mode)
(defalias 'ssm 'shell-script-mode)

; minor modes
(defalias 'wsm 'whitespace-mode)
(defalias 'gwsm 'global-whitespace-mode)
(defalias 'vlm 'visual-line-mode)
(defalias 'glm 'global-linum-mode)

Save the above in file and name it my-alias.el, then put it in your ~/.emacs.d/ directory. Then, in your emacs init file, add:

(load "my-alias")

Single Key for M-x

The default key for execute-extended-command is 【Alt+x】.

On Linux, ▤ Menu key does execute-extended-command by default.

(Menu/App key is the key with a menu icon, usually on the right side of Space bar.)

You should set F8 or ▤ Menu key for it. Single key is easier to type than 【Alt+x】.

For how, see: Emacs: How to Define Menu Key.

If you want other keys, see: Emacs: How to Define Keys.

Turn On icomplete-mode

When Alt+x execute-extended-command, you can press the Tab key to show a list of suggestions.

You can turn on icomplete-mode. It'll show a list of completions automatically. Put this in your emacs init:

;; auto show completions for execute-extended-command
(icomplete-mode 1)
Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs