M-x all-things-emacs

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

February 3rd, 2007 by Ryan McGeary · 43 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

43 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.

  • 4 boxing trainer Seattle // Jul 22, 2015 at 12:40 am

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 5 clique aqui // Jul 22, 2015 at 1:50 pm

    ... [Trackback]

    [...] There you will find 37465 more Infos: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 6 wholesale con cret creatine // Jul 23, 2015 at 4:25 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 7 amazin gas station video // Jul 24, 2015 at 9:16 am

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 8 drunk driving defense lawyer // Jul 24, 2015 at 9:27 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 9 website // Jul 25, 2015 at 1:47 pm

    ... [Trackback]

    [...] There you will find 43851 more Infos: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 10 locksmith orangevale ca // Jul 26, 2015 at 6:18 am

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 11 Fair Oaks Locksmith // Jul 26, 2015 at 6:24 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 12 social signals google // Jul 26, 2015 at 10:12 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 13 Buy Propecia Online Ohio Cheap // Jul 27, 2015 at 9:05 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 14 marsna.com // Jul 27, 2015 at 3:04 pm

    ... [Trackback]

    [...] There you will find 86568 more Infos: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 15 cleaning agency London // Jul 27, 2015 at 6:33 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 16 Xbox Live Gold Codes // Jul 27, 2015 at 10:08 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 17 buy surfboard // Jul 28, 2015 at 6:42 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 18 buy surfboards // Jul 28, 2015 at 8:09 am

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 19 Debbie // Jul 30, 2015 at 12:48 am

    ... [Trackback]

    [...] There you will find 26155 more Infos: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 20 Insider Binary Bot // Jul 31, 2015 at 3:45 pm

    ... [Trackback]

    [...] Read More Infos here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 21 Prodaja staklene ambalaze // Aug 1, 2015 at 6:32 am

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 22 admiral security // Aug 1, 2015 at 2:33 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 23 www.lcertifiedhandymen.net // Aug 2, 2015 at 7:44 pm

    ... [Trackback]

    [...] There you will find 30534 more Infos: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 24 How can I prohibit leaking pipes? // Aug 2, 2015 at 8:25 pm

    ... [Trackback]

    [...] There you will find 95322 more Infos: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 25 http://tiptopplumbers.com // Aug 2, 2015 at 11:31 pm

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 26 tiptoproofers.net // Aug 2, 2015 at 11:31 pm

    ... [Trackback]

    [...] There you will find 79726 more Infos: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 27 ventolin inhaler // Aug 3, 2015 at 5:14 pm

    ... [Trackback]

    [...] There you will find 25190 more Infos: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 28 Clicking Here // Aug 4, 2015 at 1:10 am

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 29 glass block install // Aug 4, 2015 at 1:33 am

    ... [Trackback]

    [...] There you will find 28720 more Infos: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 30 There is a humming noise coming from my panel. What does this mean? // Aug 4, 2015 at 2:18 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 31 More hints // Aug 4, 2015 at 8:02 am

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 32 where to get social Signals // Aug 5, 2015 at 6:55 am

    ... [Trackback]

    [...] Read More Infos here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 33 http://www.selectcontractors.net // Aug 5, 2015 at 7:19 am

    ... [Trackback]

    [...] There you will find 2080 more Infos: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 34 http://www.skillfullcontracting.com // Aug 5, 2015 at 12:43 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 35 link // Aug 5, 2015 at 12:56 pm

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 36 foto bugil // Aug 7, 2015 at 8:42 am

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 37 Discover More Here // Aug 9, 2015 at 6:27 pm

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 38 Recommended Site // Aug 9, 2015 at 6:55 pm

    ... [Trackback]

    [...] There you will find 77475 more Infos: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 39 click for more info // Aug 10, 2015 at 10:14 pm

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 40 hop over to these guys // Aug 12, 2015 at 10:11 pm

    ... [Trackback]

    [...] Read More Infos here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 41 What are the requirements for licensing examination? // Aug 13, 2015 at 11:40 pm

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 42 Best brand of water heaters // Aug 13, 2015 at 11:52 pm

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]

  • 43 family dentistry company // Aug 14, 2015 at 4:03 am

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/02/03/newbie-tip-1/ [...]