Emacs Aliases

Xah Lee has a nice post on using Emacs aliases to increase productivity. He uses aliases for two purposes:

  1. To change the behavior of Emacs
    For example, he has the alias
    (defalias 'yes-or-no-p 'y-or-n-p)

    This gets rid of the annoying “Please answer yes or no” silliness that Emacs insists on for some questions. This was one of the first things I did when setting up my .emacs file and I've never been sorry.

  2. As shortcuts for longer, harder-to-remember commands
    For example he has
    (defalias 'dtw 'delete-trailing-whitespace)

    In this vein, I have long used

    (defalias 'qrr 'query-replace-regexp)

    because I can never remember if it's query-regexp-replace or query-replace-regexp. Besides, it's a lot shorter and easier to type. Another example of this sort that Lee uses is to make alias shortcuts for the different modes that he uses. Thus, he has such shortcuts as

    (defalias 'hm 'html-mode)
    (defalias 'tm 'text-mode)
    (defalias 'wsm 'whitespace-mode)

    and so on.

You might think that the mode changing aliases aren't that useful given that they are usually invoked automatically by the file name extension of the file you are editing but sometimes you want to temporarily turn off a mode. For example, as I've mentioned previously, it's sometimes convenient to turn off paredit mode if you have some major restructuring to do. It's a major pain to have to type 【Meta+xparedit-mode to turn it off and then again to turn it back on. It's much easier and faster to just type 【Meta +xpm.

Lee keeps all his aliases in a separate file, which makes them easy to find and maintain. I like to keep mine in my init.el file near whatever they are redefining as that makes more sense to me but I can see the advantages to a separate file. What do you like to do?

This entry was posted in General and tagged . Bookmark the permalink.
  • Mathias

    I hate to repeat myself and if I had used aliases more I would have used something like this (just wrote this, probably not bug free):

    (defun define-aliases ()
    "Define aliases using `defalias' from aliases defined in ~/.defaliases.
    The ~/.defaliases file should consist of pairs of names in the form
    ALIAS DEFINITION, where ALIAS is the name of the alias and DEFINITION
    is the name of the thing to alias. For example \"hm html-mode\". Define
    one alias per line in this file and run this command from your .emacs."
    (dolist (alias (mapcar (lambda (x)
    (split-string x " "))
    (split-string (with-temp-buffer
    (insert-file-contents "~/.defaliases")
    (buffer-substring (point-min) (point-max))) "\n")))
    (when (not (string-equal "" (car alias)))
    (defalias (intern (car alias)) (intern (cadr alias))))))


  • Mathias

    Also, if you didn't know about partial-completion-mode, check it out. It solves part of the problem with long command names. For example, I never type paredit-mode, I type something like M-x pa-mo TAB and partial completion mode will figure out what I mean.

    And, of course, for really common commands, I set up special keybindings under the C-c keymap.

    • jcs

      Yup. That's what I do too. The commands that I use all the time I map to C-c [something]. The ones I don't use as often I just alias. I know about partial-completion-mode but don't use it. I'm not really sure why. Probably I just got use to using aliases before I learned about it.

    • jpkotta

      I use smex, and no aliases. I'd never remember the aliases I created, and probably mistype them; smex let's me be sloppy and forgetful.

  • It's a nice feature, but I tend to prefer using the long form, all this yes-or-no this-or-other-mode command. Neither has a benefit in term of recalling speed, and if anything, remembering what's the difference between hm and tm is harder than differentiating html-mode and text-mode, what with the (eventual) abundance of cm, bm, dm, and om that are sure to come. And hitting the odd 'y' for a y-or-n question is more likely than accidentally typing "y-e-s". (And the whole "this saves x keystrokes" is probably the stupidest argument I've ever heard for or against anything).

    • jcs

      Lee, I suspect, would strongly disagree. I'm more in the middle: I have a few aliases and use them consistently. For instance, I find pm just as easy to remember as paredit-mode so I don't have the problem of trying to remember the aliases. Of course the fact that I don't have a slew of them helps too. I really hate the yes-or-no business so I'm happy to have a way of disabling it. That's the great thing about Emacs: People with widely divergent opinions and work habits can use it without forcing their decisions on each other.

  • dim

    Well, use smex which brings ido to M-x, and that's about it.

    Install it easily with el-get, of course :)