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) |
3 responses so far ↓
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.
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.
ore, Yup,
partial-completion-modeis 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
defaliasandfset:I think I could interpret this different ways, but nonetheless,
defaliasis another useful function to be aware of. After reading this, it sounds like you’re right about my alias fordelete-trailing-whitespace, andfsetshould still be used for remapping the functionality of theyes-or-no-pfunction. Good call. If someone else feels like elaborating on this interpretation, please chime in.Leave a Comment