2.7. Making Emacs Work the Way You Want


If you've been reading straight through this book, you may have started a list of things you'd like to change about Emacs, such as

  • Hiding the toolbar

  • Changing Emacs cut and paste commands to C-x, C-c, and C-v

  • Turning on text mode and a fill mode so Emacs does word wrap

  • Changing the way some of the keys work

We're going to tell you how to give Emacs the to-do list, a list of options to turn on each time you enter Emacs. These options are defined in an initialization file called .emacs. Initialization files run automatically. Some run when you start up your computer. Others, like .emacs, run when you start up an associated software program. So .emacs runs automatically when you start Emacs and turns on whatever options the file defines. Emacs doesn't need this file to run; its only purpose is to make Emacs work the way you want it to.

The .emacs file consists of Lisp statements. If you're not a Lisp programmer, you can think of each line as an incantation that follows a certain pattern; you need to type it exactly.

Emacs now has another way to handle customization: an interactive interface called Custom that writes Lisp for you and automatically inserts it in your .emacs file. The Custom interface is discussed in Chapter 10, but we'll show you an even faster method for common options.

When you want to add a line to your .emacs file directly, take these steps:

  1. Enter Emacs (if you're not already there).

  2. Type C-x C-f ~/.emacs Enter.

  3. Type the line to be added exactly as shown in this book and press Enter.

  4. Press C-x C-s to save the .emacs file.

  5. Press C-x C-c to exit Emacs.

  6. Restart Emacs to have the line take effect.

If you make a minor typing mistake (such as forgetting a single quotation mark or a parenthesis), you are likely to get an error message that says Error in init file when you restart Emacs. Simply edit the .emacs file again, checking the line you added against the place you got it from, whether from this book or another user's .emacs file. Usually, you can find the error if you look hard enough; if not, find someone who has a .emacs file (and preferably understands Lisp) and ask for help. Make the changes, save the file, and restart Emacs.

What if you make a change that essentially keeps Emacs from being able to start? You can still exit Emacs, rename the file, edit it, then save it as .emacs and try again.

2.7.1 Hiding the Toolbar

New users may find the toolbar helpful. Others may not. It's easy to hide it by selecting Options Show/Hide Toolbar, and then Options Save Options.

When Emacs sets options for you through Custom (and this is what it is doing even when you use the Options menu), it writes your .emacs file. If you already have a .emacs file, it appends to it. Custom essentially groups all of its settings in one part of the file, and it is commented to indicate that you should not change it manually. Here's the .emacs file that we created by selecting this option:

 (custom-set-variables   ;; custom-set-variables was added by Custom.   ;; If you edit it by hand, you could mess it up, so be careful.   ;; Your init file should contain only one such instance.   ;; If there is more than one, they won't work right.  '(tool-bar-mode nil nil (tool-bar))) (custom-set-faces   ;; custom-set-faces was added by Custom.   ;; If you edit it by hand, you could mess it up, so be careful.   ;; Your init file should contain only one such instance.   ;; If there is more than one, they won't work right.  )

This may seem a bit bulky, but as we'll see in the next section, Emacs adds this section only once and then augments it when you set more options either through the options menu or directly through the Custom interface. Also note that this auto-generated Lisp is certainly less clean than Lisp statements you'll typically see in .emacs files. That's another reason not to edit Custom's work directly.

2.7.2 Turning On CUA Mode for C-x, C-c, and C-v to Cut, Copy, and Paste

If you're new to Emacs, you might be used to the Common User Access (CUA) conventions for cutting, copying, and pasting, C-x, C-c, and C-v respectively. You might reach for C-z for undo. CUA mode was once an add-on mode that you had to install separately, but it became so popular that it is now part of Emacs. It's coded in a clever way that doesn't interfere with Emacs keystrokes that are prefixed with C-x and C-c. Details on CUA mode can be found in Chapter 13.

You can turn this feature on through the Options menu to try it out. Simply choose Options C-x/C-c/C-v cut and paste (CUA). After you select this option, a check mark appears next to it on the Options menu. To keep it for subsequent sections, select Save Options from the Options menu. Emacs writes your .emacs file for you. If you turned off the toolbar and then set this option, your .emacs file would look like this (note that the line relating to CUA mode is bold so you can see the difference from the previous example):

 (custom-set-variables   ;; custom-set-variables was added by Custom.   ;; If you edit it by hand, you could mess it up, so be careful.   ;; Your init file should contain only one such instance.   ;; If there is more than one, they won't work right.  '(cua-mode t nil (cua-base))  '(tool-bar-mode nil nil (tool-bar))) (custom-set-faces   ;; custom-set-faces was added by Custom.   ;; If you edit it by hand, you could mess it up, so be careful.   ;; Your init file should contain only one such instance.   ;; If there is more than one, they won't work right.  )

Interestingly, Emacs happily writes the .emacs file even if it is open at the time. You can watch Emacs change the file if you have it open when you choose Save Options.

2.7.3 Turning On Text Mode and Auto-Fill Mode Automatically

To make text mode the default major mode and start auto-fill mode automatically each time you enter Emacs, add these lines to your .emacs file:

(setq default-major-mode 'text-mode) (add-hook 'text-mode-hook  'turn-on-auto-fill)

The first line tells Emacs to make text mode the default major mode; in other words, "Turn on text mode unless I tell you otherwise." The second line turns on auto-fill mode whenever you are in text mode. Alternatively, selecting Options Word Wrap in Text Modes, and then Options Save Options adds auto-fill mode to your .emacs file directly. It doesn't make text mode the default major mode, however.

If you prefer refill mode, replace the second line of code with this line:

(add-hook 'text-mode-hook (lambda ( ) (refill-mode 1)))

2.7.4 Remapping Keys

Another major use of the .emacs file is to redefine things about Emacs that irritate you. You may have ergonomic concerns about Emacs; more than one person has aggravated carpal tunnel syndrome using the default bindings. You may simply be used to reaching for certain keys for certain functions and would rather change Emacs than your habits. Whatever the case, this section gives a brief introduction to key remapping; for more details, see Chapter 10.

If you use the default bindings (rather than CUA mode), you may use C-x u for undo.[10] (Undo is such a common command that it's easy to type C-x C-u by mistake when you undo repeatedly. Unfortunately, C-x C-u is a disabled command for upcase-region. If you type C-x C-u, an annoying message about enabling the command pops up.

[10] You could use C-_ for undo instead and then you wouldn't need to read this section. We recommend that you read it anyway because you might find another annoying key mapping that you want to change and this section tells a bit about how to do so.

If you don't anticipate a big need for upcasing regions, you can redefine C-x C-u so that it also runs undo. To do so, add this line to your .emacs file:

(define-key global-map "\C-x\C-u" 'undo)

After making this change, typing C-x C-u runs undo, just as C-x u does.

Emacs customization is extremely powerful, and you can make Emacs work just the way you want it to. A far more extensive treatment of customization is found in Chapter 10. This brief introduction is meant to whet your appetite and to make it possible for you to add lines to your .emacs file as we mention potential customizations throughout the book.

The next chapter covers topics such as the many searches offered by Emacs, including query-replace, as well as spell checking and word abbreviation mode (often used to correct typos automatically). If you want to learn about these features, go on to the next chapter. From here on, you can take a selective approach to reading this book, picking and choosing whatever you want to learn about; you don't need to read the rest of the book sequentially.

2.7.5 Problems You May Encounter

  • You get an error message when you start Emacs after changing the .emacs file. The message appears only briefly; press M-p to view it again. Edit your .emacs file, checking the lines you added carefully against their source for minor typographical errors. Something as simple as a missing hyphen or apostrophe can cause this error. Fix the error, save the file, exit Emacs, and reenter. In extreme cases (the .emacs file is so messed up that Emacs won't even let you edit it), exit Emacs, rename the .emacs file, and then start Emacs and edit it again to fix it. Rename it back to .emacs and start again.

  • Paragraphs are not reformatted properly. This seems to relate to window size. Try resizing the window horizontally until paragraphs format properly.



Learning GNU Emacs
Learning GNU Emacs, Third Edition
ISBN: 0596006489
EAN: 2147483647
Year: 2003
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net