Defalias: a quick guide to making an alias in Emacs

Watch the Video Here

defalias in Emacs Lisp is a function that allows you to define an alias for an existing command or function.

It is helpful when you run out of keyboard shortcuts or when you want to make it easier to remember and type commands, especially those you use them frequently.

Why use it?

Remembering and typing long function names can be tedious.

M-x package-refresh-contents

Is it refresh-package-contents or [package-refresh-contents

defalias lets you create short, memorable names, and makes commands feel more intuitive.

Basic usage

(defalias 'alias 'existing-function)

New-name is the alias you want to create, and existing-function is the function you want to alias.

Examples

(defalias 'cz 'customize)
(defalias 'lp 'list-packages)

Now, instead of typing these commands or creating shortcuts for them you simply type

M-x + alias.

Maximizing usage

  • Group related aliases together in your dot emacs file for better organization.
  • Comment aliases so you can quickly understand their purpose later.

Mode specific aliases

Note: aliases are global by default. If you need a command to be aliased only in a specific mode, use a mode hook to define it.

(defun my-python-mode-setup ()
  (defalias 'cm 'comment-region))
(add-hook 'python-mode-hook 'my-python-mode-setup)

This will create a custom shortcut specifically for commenting code, but only when editing Python files.

My aliases

(defalias 'ct 'customize-themes)
(defalias 'cz 'customize)
(defalias 'ddl 'delete-duplicate-lines)
(defalias 'dga 'define-global-abbrev)
(defalias 'ddl 'delete-duplicate-lines)
(defalias 'dga 'define-global-abbrev)
(defalias 'dma 'define-mode-abbrev)
(defalias 'ea 'edit-abbrevs)
(defalias 'ff 'flip-frame)
(defalias 'fl 'flush-lines)
(defalias 'fnb 'find-name-dired)
(defalias 'fnd 'find-name-dired)
(defalias 'klm 'kill-matching-lines) ;; move lines with string elsewhere
(defalias 'lc 'langtool-check)
(defalias 'lcb 'langtool-correct-buffer)
(defalias 'lp 'list-packages)
(defalias 'pcr 'package-refresh-contents)
(defalias 'pi 'package-install)
(defalias 'pug 'package-upgrade-all)
(defalias 'qr 'query-replace)
(defalias 'rg 'rgrep)
(defalias 'rsv 'replace-smart-quotes)
(defalias 'sl 'sort-lines)
(defalias 'slo 'single-lines-only)
(defalias 'spe 'ispell-region)
(defalias 'udd 'package-upgrade-all)
(defalias 'ugg 'package-upgrade-all)
(defalias 'wr 'write-region)
(defalias 'yes-or-no-p 'y-or-n-p)

Return to Home