M-x all-things-emacs

Newbie Tip: y-or-n-p and Function Aliases

February 3rd, 2007 by Ryan McGeary · 3 Comments

Part of the reason we started M-x all-things-emacs was to encourage newcomers to the emacs world. The emacs learning curve is a steep one, and sometimes it’s easy for the more advanced members of the contingent to take some of the basics for granted. This is the first of a series of newbie tips where we hope to help emacs beginners climb that learning curve.

Add this to your .emacs file to improve productivity:

(fset 'yes-or-no-p 'y-or-n-p)

Several emacs actions require user confirmation before proceeding and by default, the yes-or-no-p function is used to prompt the user for a yes/no response. Typing yes RET or no RET can be a burden, but typing y or n is quicker and intuitive. The call to fset aliases the yes-or-no-p function to instead yield the results of y-or-n-p, resulting in fewer keystrokes.

I also use the fset function to alias those actions that maybe don’t warrant their own hot key combination, but perhaps deserve a shorter name for faster M-x access. For example, suppose I wanted to alias delete-trailing-whitespace to a shorter alternative. Try this:

(fset 'dtw 'delete-trailing-whitespace)

Now, I can remove trailing whitespace from each line of the current buffer by running M-x dtw.

P.S. I thought about making the first beginner tip a discussion about the built-in emacs tutorial (C-h t) and the other built-in help actions (M-x help), but that’s pretty well documented already.

Update: piyo pointed out in the comments that the dtw alias should be defined using defalias instead of fset according to the conventions in the Elisp Manual. Thanks.

(defalias 'dtw 'delete-trailing-whitespace)

Tags: newbie · tips

3 responses so far ↓

  • 1 ore // Feb 3, 2007 at 10:36 pm

    You can also enable partial-completion-mode and use shortcuts for all commands and file names.
    For example, M-x d-t-w gets expanded to M-x delete-trailing-whitespace.

  • 2 piyo // Feb 4, 2007 at 1:27 am

    For your second example, the Emacs Lisp manual recommends using the function “defalias”. Perhaps it is also easier to remember because of the name. Either way “fset” and “defalias” achieve the same thing for this example.

  • 3 Ryan McGeary // Feb 4, 2007 at 4:34 am

    ore, Yup, partial-completion-mode is another great tip, but the only downside for this particular example (as I understand it) is that you have to type the hypens.

    piyo, Here’s what the Elisp manual says about defalias and fset:

    The proper place to use defalias is where a specific function name is being defined—especially where that name appears explicitly in the source file being loaded. This is because defalias records which file defined the function, just like defun (see Unloading).

    By contrast, in programs that manipulate function definitions for other purposes, it is better to use fset, which does not keep such records.

    I think I could interpret this different ways, but nonetheless, defalias is another useful function to be aware of. After reading this, it sounds like you’re right about my alias for delete-trailing-whitespace, and fset should still be used for remapping the functionality of the yes-or-no-p function. Good call. If someone else feels like elaborating on this interpretation, please chime in.