Learning GNU Emacs, 2nd Editionby Debra Cameron, Bill Rosenblatt, Eric S. Raymond
GNU Emacs is the most popular and widespread of the Emacs family of editors. It is also the most powerful and flexible. Unlike all other text editors, GNU Emacs is a complete working environment you can stay within Emacs all day without leaving. This book tells you how to get started with the GNU Emacs editor. It will also "grow" with you: as you become
GNU Emacs is the most popular and widespread of the Emacs family of editors. It is also the most powerful and flexible. Unlike all other text editors, GNU Emacs is a complete working environment you can stay within Emacs all day without leaving. This book tells you how to get started with the GNU Emacs editor. It will also "grow" with you: as you become more proficient, this book will help you learn how to use Emacs more effectively. It takes you from basic Emacs usage (simple text editing) to moderately complicated customization and programming.The second edition of Learning GNU Emacs describes all of the new features of GNU Emacs 19.30, including fonts and colors, pull-down menus, scroll bars, enhanced X Window support, and correct bindings for most standard keys. GNUS, a Usenet newsreader, and ange-ftp mode, a transparent interface to the file transfer protocol, are also described.Learning GNU Emacs, second edition, covers:
- Using Emacs as an Internet Toolkit (to use electronic mail and Usenet news, telnet to other computers, retrieve files using FTP, browse the World Wide Web, and author Web documents)
- Emacs' rich, comprehensive online help facilities
- How to edit files with Emacs
- Using Emacs as a "shell environment"
- How to take advantage of "built-in" formatting features
- How to use multiple buffers, Emacs windows, and X Windows
- Customizing Emacs
- The Emacs interface to the X Window System, which allows you to use a mouse and pop-up menus
- Whys and hows of writing macros to circumvent repetitious tasks
- Emacs as a programming environment
- The basics of Emacs LISP
- How to get Emacs
Read an Excerpt
Chapter 3: Search and Replace OperationsThe 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 for serious document preparation 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 search/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 this function is a kind of replacement (errors are sought and replaced with corrections). Finally, we cover word abbreviation mode; this feature is a kind of automatic replacement that can be a real time saver.
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:
You give Emacs a text string (called a search string), and it finds the next occurrence. You will find this search in almost any editor.
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.
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 bat, 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 that may vary slightly, 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. However, regular expressions can be extremely complex. We'll give a brief introduction to this topic in this chapter; it is discussed more fully in Chapter 13.
Incremental regular expression search
This search procedure is a combination of an incremental search and a regular expression search.
You can search forward or backward. Searches can be either case-sensitive, meaning that Emacs considers upper- and lowercase letters to be different (i.e., the words This and this are different); or case-insensitive, in which upper- and lowercase are not differentiated (i.e., This and this are the same). By default, searches are case-insensitive, with upper- and lowercase letters considered to be the same. One exception: if you type any uppercase letters, Emacs makes the whole search string case-sensitive, because it assumes you are looking for something precise since you've made the extra effort to type some letters uppercase.
Replacement operations are closely related to searches. As with searches, Emacs offers you several different flavors:
Simple search and replace
In this procedure, Emacs replaces all occurrences of one string with another. Usually, this is too radical a solution and can have unintended results. Try query-replace instead.
With query-replace, Emacs conditionally replaces a string throughout a file. Emacs finds all occurrences of the search string, and for each one it asks you whether or not to perform the replacement. This type of replacement is useful if you need to change some but not all instances of a word or phrase throughout a file.
Regular expression replace
Regular expression replacement uses UNIX's powerful "regular expressions" facility (discussed later in this chapter) to find the replacement string.
So now you know what you'll be looking at. Don't be intimidated by the wealth of different searches that are available. In practice, you'll probably settle on one search command and one replace command and use these for 99 percent of your work. For example, we use incremental search and query-replace most of the time. If you're a writer, you may use word search all the time; if you're a programmer, you might want a regular expression search. If you're just beginning, you may want to learn incremental search and 'read the rest of this chapter later. However, if you know what's available, you'll be able to make use of the other search commands when they become useful.
Incremental search starts to work from the moment you type the first character of the search string. For the beginner, it isn't the easiest kind of search to use, but it has lots of advantages. Many users like the efficiency of incremental searches; but if you're inclined to make typos, you may find incremental search rather frustrating and may want to use a simple search instead. However, we'll start with incremental search because Emacs clearly wants you to use it and offers some perks for doing so.
To start an incremental search, type C-s and then type the text you want to find. Emacs temporarily enters Isearch mode. (This doesn't affect you much; we just thought you might want to know about it.) Notice how this search works: Emacs looks for each character as soon as you type it. For example, if you are searching for the word meter, in an incremental search Emacs finds the next m as soon as you type the m; it finds the next me as soon as you type the e; it finds the met as soon as you type the t; and so on. Sooner or later, you either find what you want, or Emacs is unable to find anything. If you find what you want, press RETURN; doing so stops the search at the current place in the file. If Emacs can't find anything that matches your search string, it prints the message Search failed at the bottom of your screen and then it beeps.
Here's what happens when we search for the word meter; the numbers show how the cursor moves with each new letter in the search string....
Meet the Author
Debra Cameron is president of Cameron Consulting. In addition to her love for Emacs, Deb researches and writes about emerging technologies and their applications. Her latest book, Optical Networking: A Wiley Tech Brief, published in 2002 by John Wiley & Sons, covers the practical applications of optical networking and was written in the hope that true broadband will be more widely deployed. Deb also edits OReilly titles, including DNS and Bind, DNS on Windows 2000, TCP/IP Network Administration, HTML and XHTML: The Definitive Guide, Java Security, Java Swing, Learning Java, and Java Performance Tuning. She has presented numerous videos for WatchIT.com, covering security and networking as well as e-business topics. She has moderated roundtables on PlanetIT on advanced networking and intranet design. Deb resides in Gaithersburg, Maryland with her husband Jim and their three children, Meg, David, and Bethany.
Bill Rosenblatt is president of GiantSteps/Media Technology Strategies, a consulting firm in New York City. Before founding GiantSteps, Bill was CTO of Fathom, an online content and education company associated with Columbia University and other scholarly institutions. He has been a technology executive at McGraw-Hill and Times Mirror, and head of strategic marketing for media and publishing at Sun Microsystems. Bill was also one of the architects of the Digital Object Identifier (DOI), a standard for online content identification and DRM.
Most Helpful Customer Reviews
See all customer reviews