Buy this Book
Print Book $39.95 Read it Now!
Print Book £28.50
Add to UK Cart
Reprint Licensing

Learning GNU Emacs
Learning GNU Emacs, Third Edition

By Debra Cameron, James Elliott, Marc Loy, Eric S. Raymond, Bill Rosenblatt
Price: $39.95 USD
£28.50 GBP

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Emacs Basics
Some of you out there are probably dying to get your hands on the keyboard and start typing. We won't try to stop you; turn to the section called "Starting Emacs" and you can go ahead. But do read the beginning of this chapter later when you're ready for a break. Emacs is much easier to learn if you understand some of the basic concepts involved, which we discuss in the following introduction.
GNU Emacs is one of the most commonly used text editors in the world today. Many users prefer Emacs to vi (Unix's standard editor) or to other GUI text editors. Why is Emacs so popular? It isn't the newest tool, and it's certainly not the prettiest. But it may well be the most useful tool you'll ever learn. We want to present what you need to know about Emacs to do useful work, in a way that lets you use it effectively. This book is a guide for Emacs users; it tries to satisfy the needs of many readers, ranging from casual users to programmers.
Our approach therefore isn't to tell you absolutely everything that Emacs does. It has many features and commands that this book doesn't describe. We don't think that's a problem; Emacs has a comprehensive online help facility that helps you figure out what these are. We focus our attention on describing how to get useful work done. After covering basic editing in the first three chapters, we describe how to use Emacs as a comprehensive working environment: how to boost productivity with multiple buffers and windows, how to give commands without leaving the editor, how to take advantage of special editing modes, how to use Emacs for editing special types of files (source files for various programming languages), and so on. We cover the most important commands and the most important editing modes. However, you should always keep one principle in mind: Emacs does many things well, but it isn't important for that reason. Emacs is important because of the integration of different things you need to do.
What does integration mean? A simple example will help. Assume that someone sends you a mail message describing a special command for accessing a new printer. You can fire up an Emacs shell, paste the command into Emacs, and execute it directly. If it works, you can edit your startup file to create an alias for the command. You can do all this without leaving the editor and without having to retype the command once. That's why Emacs is so powerful. It's more than just an editor; it's a complete environment that can change the way you work.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Introducing Emacs!
GNU Emacs is one of the most commonly used text editors in the world today. Many users prefer Emacs to vi (Unix's standard editor) or to other GUI text editors. Why is Emacs so popular? It isn't the newest tool, and it's certainly not the prettiest. But it may well be the most useful tool you'll ever learn. We want to present what you need to know about Emacs to do useful work, in a way that lets you use it effectively. This book is a guide for Emacs users; it tries to satisfy the needs of many readers, ranging from casual users to programmers.
Our approach therefore isn't to tell you absolutely everything that Emacs does. It has many features and commands that this book doesn't describe. We don't think that's a problem; Emacs has a comprehensive online help facility that helps you figure out what these are. We focus our attention on describing how to get useful work done. After covering basic editing in the first three chapters, we describe how to use Emacs as a comprehensive working environment: how to boost productivity with multiple buffers and windows, how to give commands without leaving the editor, how to take advantage of special editing modes, how to use Emacs for editing special types of files (source files for various programming languages), and so on. We cover the most important commands and the most important editing modes. However, you should always keep one principle in mind: Emacs does many things well, but it isn't important for that reason. Emacs is important because of the integration of different things you need to do.
What does integration mean? A simple example will help. Assume that someone sends you a mail message describing a special command for accessing a new printer. You can fire up an Emacs shell, paste the command into Emacs, and execute it directly. If it works, you can edit your startup file to create an alias for the command. You can do all this without leaving the editor and without having to retype the command once. That's why Emacs is so powerful. It's more than just an editor; it's a complete environment that can change the way you work.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Understanding Files and Buffers
You don't really edit files with Emacs. Instead, Emacs copies the contents of a file into a temporary buffer and you edit that. The file on disk doesn't change until you save the buffer. Like files, Emacs buffers have names. The name of a buffer is usually the same as the name of the file that you're editing. There are a few exceptions. Some buffers don't have associated files—for example, *scratch* is just a temporary practice buffer, like a scratchpad; the help facility displays help messages in a buffer named *Help*, which also isn't connected to a file.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
A Word About Modes
Emacs achieves some of its famed versatility by having various editing modes in which it behaves slightly differently. The word mode may sound technical, but what it really means is that Emacs becomes sensitive to the task at hand. When you're writing, you often want features like word wrap so that you don't have to press Enter at the end of every line. When you're programming, the code must be formatted correctly depending on the language. For writing, there's text mode; for programming, there are modes for different languages, including C, Java, and Perl. Modes, then, allow Emacs to be the kind of editor you want for different tasks.
Text mode and Java mode are major modes. A buffer can be in only one major mode at a time; to exit a major mode, you have to enter another one. Table 1-1 lists some of the major modes, what they do, and where they're covered in this book.
Table 1-1: Major modes
Mode
Function
Fundamental mode
The default mode (Chapter 6)
Text mode
For writing text (Chapter 2)
View mode
For viewing files but not editing (Chapter 4)
Shell mode
For running a shell within Emacs (Chapter 5)
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Starting Emacs
To start Emacs, simply click on the Emacs icon or type emacs on the command line and press Enter.
Click on the Emacs icon or, from the command line, type: emacs Enter
Starting Emacs.
You'll see a short message describing a few important menu items and the version of Emacs that you're running. It may appear as a graphical splash screen (like the one shown here) or a text splash screen. This message disappears as soon as you type the first character. Emacs then puts you in an (almost) empty buffer called *scratch*, an ideal place for you to experiment.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
About the Emacs Display
When you enter Emacs, you see a large workspace near the top of the window where you do your editing. (See Figure 1-1.)
Figure 1-1: Understanding the Emacs display
A cursor marks your position. The cursor is also called point, particularly among people who are more familiar with Emacs and in the online help system; therefore, it's useful to remember this term.
You don't have to do anything special before you start typing. As long as you type alphanumeric characters and punctuation, Emacs inserts them into your buffer. The cursor indicates where Emacs inserts the new characters; it moves as you type. Unlike many editors (particularly vi), Emacs does not have separate modes for inserting text and giving commands. Try typing something right now, and you'll begin to see how easy Emacs is to use. (If you get stuck for any reason, just press C-g.)
The toolbar is a new feature in Emacs 21. Its basic icons and their functions are listed in Table 1-3. Note that the toolbar is context sensitive; in some modes, such as the Info mode for reading the Emacs manual, the toolbar changes to provide browsing help. We'll discuss those icons when we cover the relevant modes.
Table 1-3: Icons on the Emacs toolbar
Icon
Function
Where to learn more
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Emacs Commands
You're about to start learning some Emacs commands, so let's discuss them a bit first. How do you give commands? Each command has a formal name, which (if you're fastidious) is the name of a Lisp routine. Some command names are quite long; you usually wouldn't want to type the whole thing. As a result, we need some way to abbreviate commands.
Emacs ties a command name to a short sequence of keystrokes. This tying of commands to keystrokes is known as binding. Even things you don't normally think about as commands, such as inserting the characters that you type, are handled through the binding mechanism. Keys like "A" are bound to the Emacs command self-insert-command, which inserts them into the buffer you are editing. Most actions that you would normally think of as editor commands are bound to keystroke sequences starting with Ctrl or Meta. Emacs also binds some commands to mouse clicks (alone or modified by Shift or Ctrl) and to options on menus.
The authors of Emacs try to bind the most frequently used commands to the key sequences that are the easiest to reach. Here are the varieties of key sequences you'll encounter:
  • The most commonly used commands (such as cursor movement commands) are bound to C- n (where n is any character). To press C- n, press and hold the Ctrl key and press n, then release both keys.
  • Slightly less commonly used commands are bound to M- n. To press M- n, press and hold the Meta key (usually next to the space bar), then press n.
  • Other commonly used commands are bound to C-x something (C-x followed by something else—one or more characters or another control sequence). Among other types of commands, file manipulation commands, like the ones you are about to learn, are generally bound to C-x
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Opening a File
You can open a file by specifying the filename when you start Emacs from the command line or by typing C-x C-f (the long command name is find-file).
The paper icon on the toolbar also runs this command. In some applications, a similar icon simply creates a new, unnamed file (e.g., Document1 in Word). Emacs expects you to provide a filename, as we'll see in a moment.
Press: C-x C-f
Emacs prompts you for a filename.
To press C-x C-f, hold down Ctrl, press x and then press f. Now release Ctrl.
After you press C-x C-f, Emacs uses the minibuffer to ask you for the filename. Whenever Emacs wants input from you, it puts the cursor in the minibuffer. When you're done typing in the minibuffer, press Enter.
Type: newfile Enter
Emacs starts another buffer with the new file in it.
What if you try to read the same file twice? Instead of creating a new buffer, Emacs just moves you to the buffer the file is in.
You can also open a file in Emacs by dragging and dropping it on an Emacs window or on the Emacs icon.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Saving Files
To save the file you are editing, type C-x C-s. Emacs writes the file. To let you know that the file was saved, it puts the message Wrote filename in the minibuffer. If you haven't made any changes to the file, Emacs puts the message No changes need to be saved in the minibuffer. You can also get to this option by pressing the diskette on the toolbar or choosing Save (current buffer) from the File menu.
If you decide to save something you've typed in the *scratch* buffer by typing C-x C-s, Emacs asks you for a filename. After you give it a filename, Emacs changes the mode line accordingly.
A related command is write-file (C-x C-w). It is the Emacs equivalent of the Save As option found on many applications' File menus. The write-file command asks you to type a new filename in the minibuffer. However, if you just press Enter instead of typing a new filename, write-file saves the file with its old name—just as C-x C-s would have done. (It does ask if you want to replace the current file with the one in this buffer, however.)
The write-file command is useful for editing files that you do not have permission to change. Use the find-file command to get the file you want into a buffer, and then use write-file to create your own private version, with a different name or path. This maneuver allows you to copy the file to one that you own and can change. Of course, the original file is not affected.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Leaving Emacs
To quit Emacs, type C-x C-c or close it like you would any other application. If you have made changes to a buffer, Emacs asks you if you want to save them. If you type y, Emacs writes the file, then exits. If you type n, Emacs asks you to confirm that you want to abandon the changes you made by typing yes or no in full. If you type no, your normal Emacs session continues just as if you never attempted to exit. If you type yes, you exit Emacs and the changes you made during this session do not become permanent. Leaving without saving changes can be useful if you make changes you didn't intend to make.
By the way, Emacs is picky about whether you type y or yes. Sometimes it wants one, sometimes the other. If it asks for a y, you can sometimes get away with typing yes but not vice versa. If it beeps and displays, Please answer yes or no, you didn't enter the whole word and it wants you to.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Getting Help
Emacs has extensive online help, which is discussed further in Chapter 14. You can enter help through the lifesaver icon on the toolbar or through the Help menu. Either method will show you a help menu, described later in this section. To enter help using the keyboard, press C-h. Pressing C-h ? gives you a list of options. Pressing C-h t starts a tutorial that is an excellent introduction to Emacs.
To get information about the meaning of a keystroke combination, press C-h k for describe-key. For example, if you type C-h k C-x i, Emacs displays a description of the insert-file command, which is bound to C-x i. Pressing C-h f (for describe-function) asks Emacs to describe a function (really just a command name, such as find-file). Essentially, C-h k and C-h f give you the same information; the difference is that with C-h k, you press a key whereas with C-h f, you type a command name.
Assume you want to find out about what C-x i does.
Type: C-h k
Asking for help about a keyboard command.
Type: C-x i
Emacs splits the screen to display help.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Summary
Now you know the basic commands for starting and stopping Emacs and for working with files. Chapter 2 builds on these commands to give you the skills you need for editing with Emacs. Table 1-4 summarizes the commands we covered in this chapter.
Table 1-4: File handling commands
Keystrokes
Command name
Action
C-x C-f File Open File
find-file
Find file and read it in a new buffer.
C-x C-v
find-alternate-file
Read an alternate file, replacing the one read with C-x C-f.
C-x iFile Insert File
insert-file
Insert file at cursor position.
C-x C-sFile Save (current buffer)
save-buffer
Save file.
C-x C-wFile Save Buffer As
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Editing
Now that you know how to enter and exit Emacs as well as the basics of working with files, it's time to learn how to move around in and edit files. Emacs offers lots of ways to move around in files. At first, you might find it confusing that there are so many ways to do the same thing. Be patient—as you learn, the confusion will lessen, and you'll begin to appreciate the variety of Emacs commands. The more ways you learn, the fewer keystrokes you'll need to get to the part of the file you want to edit.
If you want to practice commands while you're reading—which will help you learn faster—start by typing a page or two from anything you happen to have handy; the newspaper is fine. That will give you some text to work with as you learn the editing skills described in this chapter. Don't worry if you make mistakes; just keep on typing. You can correct any mistakes after you learn the basic editing skills outlined here. Learning any editor is primarily a matter of forming certain finger habits rather than memorizing what the book says. You will learn the right finger habits only if you start typing.
When you are typing and you get to the right side of the display, you have two options. You can press Enter to go to the next line, or you can keep typing. If you type a long line and don't press Enter, Emacs waits until you reach the end of the display. Then it puts a curved arrow at the end of the line and one at the beginning of the next line as a visual indication that the next line is a continuation of the previous line (see Figure 2-1). If Emacs is run in a nongraphical environment, a backslash (\) is used instead.
Figure 2-1: Graphical versions of Emacs use curved arrows to indicate that a line is continued; terminal versions use backslashes
Refill mode is a minor mode that keeps paragraphs neat as you edit them. It is not on by default. Look at the mode line. If the word Refill appears, you are in refill mode already. If not, you can turn it on for this buffer only by typing
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Moving the Cursor
The easiest way to move the cursor is to click the left button on your mouse or to press the arrow keys. However, it's a hassle to reach for a mouse all the time. Learn to use keyboard commands to move around so that you will ultimately achieve blinding speed and maximum productivity in Emacs.
To use Emacs commands to move the cursor forward one space, type C-f (f for "forward"). As you might guess, C-b moves the cursor backward. To move up, type C-p (for previous-line), and to move down, type C-n (for next-line). It's easier to memorize commands if you remember what the letters stand for.
Figure 2-2 illustrates how to move up, down, left, and right using Emacs commands.
Figure 2-2: Basic cursor motion
If you're at the end of a line, C-f moves to the first character on the next line. Likewise, if you're at the beginning of a line, C-b moves to the last character of the previous line. If there's no place to go, Emacs beeps and displays the message Beginning of buffer or End of buffer.
Now we'll learn some more advanced ways to move the cursor. One common way is moving forward and backward by word: M-f moves forward a word; M-b moves backward a word. You can also move to the beginning or end of the line. C-a moves you to the beginning of the line (just like a is the beginning of the alphabet). C-e moves you to the end of the line. To move backward one sentence, type M-a; to move forward one sentence, type M-e. To move forward a whole paragraph at a time, type M-}; to move backward a paragraph, type M-{. If you're in the middle of a sentence or paragraph, moving back a sentence or paragraph actually takes you to the beginning of the current sentence or paragraph.
Figure 2-3 uses a few paragraphs of Victor Hugo's Les Misérables to show how you can move the cursor more than one character at a time.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Deleting Text
Before you start practicing deletion commands, you might want to know the undo command, which is discussed fully later in this chapter. Typing C-_ or C-x u undoes your last edit; typing undo again undoes the edit before that one, and so on.
Emacs provides many ways to delete text. The simplest way to delete text is to press the Del key, which deletes the character immediately to the left of the cursor. See Figure 2-4 for possible locations of the Del key on your keyboard. It is sometimes referred to as the Backspace key. Del is easiest to define by what it does: it deletes the previous character. If you're typing and you decide to erase the last character you typed, what key do you reach for? That's the key Emacs refers to as Del.
Emacs provides a number of other deletion commands—perhaps too many for your taste, although you'll eventually find a reason to use most of them. For example, C-d (for delete-character) deletes the character under the cursor. The command for deleting the next word is M-d (for kill-word). Once again, note how the Meta key augments the command: C-d operates on a character, and M-d operates on a word.
Emacs has commands to delete the next or previous word, sentence, and paragraph. By their names, you can guess what they do when you're between words, sentences, or paragraphs. If you're in the middle of an entity, however, they do something a little surprising: they delete a portion of the current word, sentence, or paragraph, backward or forward depending on whether the command deletes previous or next. For example, here's how M-d acts differently depending on where the cursor is.
If the cursor is here:
M-d makes this edit:
It was the worst of times
It was the w of times
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Marking Text to Delete, Move, or Copy
What if the text you want to delete is just a phrase? Or half a paragraph? Or several paragraphs? In Emacs, you select text by defining an area called a region. You can mark regions with the mouse or by using the keyboard. What happens with the mouse is a bit complicated, so we describe it later in this chapter, following our discussion of the system clipboard.
To define a region using the keyboard, you use a secondary pointer called a mark. Some versions of Emacs display the mark on the screen; unfortunately, in GNU Emacs, the mark is invisible.
You set the mark at one end of the region by pressing C-Space or C-@, then move the cursor to the other end of the region. (The cursor is sometimes also referred to as point. There is one minor but important difference between the cursor and the point, however. The cursor is on top of a character; in Emacs, the point is actually in between the character the cursor is on and the previous character. As we said, this difference is minor, but it helps you to visualize where the cursor should be when you mark a region.) Figure 2-5 illustrates point, mark, and region.
Figure 2-5: Point, mark, and region
Let's mark a sample region. In this example, we remove the phrase "it was the worst of times." First, we find the beginning of the phrase. Then we set the mark, move forward to the end of the phrase, and cut.
Move to the beginning of "it" and press C-Space.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Emacs and the Clipboard
Emacs 21 plays well with the clipboard, though it still may not do what you want it to in some cases. Let's dig into this in a little more detail.
By default, text that you cut or copy using icons on the toolbar or options on the Edit menu is placed on the clipboard and is accessible to other applications.
Unfortunately, Emacs diverges by platform on this issue. Normally we save platform-specific issues for Chapter 13, but cutting and pasting is such a vital operation that we must describe the differences here.
On Windows and Mac OS X (but not on Linux) any text you cut or copy using C-w or M-w is also copied to the clipboard.
On Windows and Mac OS X, simply selecting text with the mouse places it on the clipboard. (This doesn't work on Linux.). Most applications require you to highlight text, then issue a copy command. Emacs doesn't. Table 2-6 shows how this works on various platforms.
Table 2-6: Selecting text with the mouse
Linux
Windows
Mac OS X graphical
Mac OS X terminal
Sends to clipboard?
no
yes
yes
no
Sends to kill ring?
yes
yes
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Editing Tricks and Shortcuts
Now that you've learned the basics of editing—moving the cursor to the right position, deleting, copying, and moving text—you can learn some tricks that make editing easier.
The most common typo involves the transposition of two letters, and most typos are noticed immediately after you make them. Pressing C-t transposes two letters, to put them in the right order:
Before C-t
After C-t
the best of timse, it
                              the best of times  
                              it
                           
To transpose two letters, put the cursor on the second of the two letters to be transposed. Press C-t. (If you often transpose letters, word abbreviation mode, discussed in Chapter 3, cleans up typos automatically.)
You can also transpose two words, lines, paragraphs, or sentences. To transpose two words, put the cursor between the two words and press M-t. After Emacs has finished, the cursor follows the second of the two (transposed) words:
Before M-t
After M-t
one three two
                              one two three 
                           
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Canceling Commands and Undoing Changes
Sometimes you start a command by accident or change your mind about it. Don't worry: with Emacs, you can quit in the middle or undo it.
When you want to cancel any command that's in progress, press C-g. The word Quit appears in the command area. This command is helpful when you are stuck in the minibuffer and didn't really mean to go there. Depending on what you were doing, you may have to press C-g a few times.
What happens if you make a mistake while you're editing? You can undo your changes by pressing C-_ or C-x u (for undo; conveniently, the toolbar also has an undo icon, a curved left arrow). By typing undo repeatedly, you can gradually work your way back to a point before your mistake. Although the undo command is very powerful, saving your file frequently, if not compulsively, is nevertheless a good idea. We usually save a file whenever we stop typing—even if only for a few seconds. Train your fingers to press C-x C-s whenever you pause; it's a good habit to form.
If you're used to typing C-z to undo, you can easily change Emacs's behavior to match your habits. See "Making Emacs Work the Way You Want" at the end of this chapter for information on CUA mode.
What if you'd like to redo a command after you type undo? There is no formal redo command, but you can use undo in the following way. Just move the cursor in any direction, and type C-_ or C-x u again. Emacs redoes the last command you undid. You can repeat it to redo previous undos.
Although undo is an important command, it can be slow if you want to undo a large number of changes. Table 2-11 summarizes three methods for undoing changes and circumstances in which you might want to use them.
Table 2-11: Methods for undoing changes
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
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.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 3: Search and Replace
The commands we discussed in the first two chapters are enough to get you started, but they're certainly not enough to do any serious editing. If you're using Emacs for anything longer than a few paragraphs, you'll want the support this chapter describes. In this chapter, we cover the various ways that Emacs lets you search for and replace text. Emacs provides the traditional search and replace facilities you would expect in any editor; it also provides several important variants, including incremental searches, regular expression searches, and query-replace. We also cover spell-checking here, because it is a type of replacement (errors are sought and replaced with corrections). Finally, we cover word abbreviation mode; this feature is a type of automatic replacement that can be a real timesaver.
While you're editing, you frequently want to find something you've already typed. Rather than hunt through the file trying to find what you're looking for, virtually all editors provide some kind of search feature that lets you look for a particular text string. Emacs is no exception to the rule. It supplies a search command—in fact, it provides a dizzying array of search commands. Here's a quick summary of the different kinds of searches that are available:
Simple search
You give Emacs a search string, and it finds the next occurrence. You will find this search in almost any editor.
Incremental search
With incremental search, Emacs starts to search the file as soon as you type the first character of a search string. It continues to search as you type more characters.
Word search
A word search is like a simple search, except that Emacs searches only for full words and phrases. For example, if you are searching for the word
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Different Kinds of Searches
While you're editing, you frequently want to find something you've already typed. Rather than hunt through the file trying to find what you're looking for, virtually all editors provide some kind of search feature that lets you look for a particular text string. Emacs is no exception to the rule. It supplies a search command—in fact, it provides a dizzying array of search commands. Here's a quick summary of the different kinds of searches that are available:
Simple search
You give Emacs a search string, and it finds the next occurrence. You will find this search in almost any editor.
Incremental search
With incremental search, Emacs starts to search the file as soon as you type the first character of a search string. It continues to search as you type more characters.
Word search
A word search is like a simple search, except that Emacs searches only for full words and phrases. For example, if you are searching for the word hat, you don't have to worry about finding the word that. A word search is also useful when you need to find a phrase that is spread across two lines.
Regular expression search
To search for patterns, you can use a regular expression search. For example, if you wanted to find all instances of B1 and B2, you could search for them using the regular expression B[12]. However, regular expressions can be extremely complex. We'll give a brief introduction to this topic here; it is discussed more fully in Chapter 11.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Search and Replace
Search and replace definitely go together, like coffee and cream. Let's say you're working on a new software application and at the last possible moment, the Marketing Department decides to change the product's name.
Tere's a press release for Whirligig, an email service that periodically reminds you to make healthy lifestyle changes like exercising, drinking water, and taking vitamins. The level of harassment or, as the marketing department says, encouragement, can be set by the user. Whirligig isn't really the most descriptive name, so at the last minute the Marketing Department changes it to HealthBug.
Assume you're in the situation we just described. You want to replace every occurrence of one string with another. You know that Whirligig is never correct, and there is absolutely no ambiguity about how you want to replace it. When you want to replace every instance of a given string, you can use a simple command that tells Emacs to do just that. Type M-x replace-string Enter, then type the search string and press Enter. Now type the replacement string and press Enter again. Emacs replaces all occurrences in the file from the cursor position onward. If you want to search and replace throughout the file, press M-< to go to the beginning of the file before typing this command. Here's a quick example of using replace-string.
Initial state:
Whirligig appears four times, but the cursor is positioned after the first instance.
Now we'll do the replacement.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Checking Spelling Using Ispell
Emacs includes two spell-checking interfaces: to the Unix spell checker, <