Emacs is the most powerful text editor available today. Unlike most other editors (in particular, unlike the standard Unix editor, vi), Emacs is a complete working environment. No matter what you do, you can start Emacs in the morning, work all day and all night, and never leave it: you can use it to edit, rename, delete, and organize files; to compile programs; to run shell commands; and so on. Before windowing systems like X and Microsoft Windows became popular, Emacs often served as a complete windowing system of its own. All you needed was a terminal, and you could live within Emacs forever. Emacs is also infinitely flexible; you can write your own commands, change the keys that are associated with commands, and (if you are willing to take the time) do just about anything you want.
Because it does so much, Emacs has a reputation for being extremely complicated. We don't think that's warranted; we teach you Emacs from the ground up, covering first the basics and then some of the more advanced features.
In this book, we have tried to reach as broad an audience as possible: from casual users to professional writers and web authors to programmers to system administrators. No matter what you do with Emacs, you will find it's easy to learn; after one or two sessions, you'll know the basics of editing any file. After you learn the basics, you can go on to learn about more advanced topics that provide the real benefits of using Emacs. These include:
• Using multiple windows and buffers so you can work on several files at once
• Customizing keyboard commands
• Tailoring Emacs to fit your work style
• Making Emacs your work environment where you can do all your everyday tasks, such as organizing files, compiling programs, and issuing shell commands
• Creating macros to streamline repetitive tasks
• Using Emacs to support programming in many languages (including C, C++, Lisp, Java, and Perl)
• Formatting files with various markup languages, such as HTML and XML
• Using word abbreviations to avoid spelling out long phrases or to correct common misspellings
Of course, many of the topics may not apply to you; some topics may be appropriate for a second reading but not for the first. Toward the end of the preface, we'll sketch several different ways to approach the book, depending on your interests and experience.
Numerous versions of Emacs are available, offering a wide range of features, but two are in widespread use today: GNU Emacs and XEmacs. (An exhaustive list of Emacs implementations can be found at http://www.finseth.com/~fin/emacs.html.) XEmacs was originally tailored for GUI usage and has a fairly wide user base, but lacks some of the features of GNU Emacs.[1]
This book covers GNU Emacs. Since its appearance, GNU Emacs has become the most popular, powerful, and flexible Emacs, and there's no reason to believe that this situation will change. If you know GNU Emacs, you will be able to adapt to any other Emacs implementation with no trouble; it's not so easy to go in the other direction.
This book, however, isn't limited to GNU Emacs users. Because of the similarities between different Emacs implementations, this book should help you get started with any Emacs editor. The basic keyboard commands change little from one editor to another—you'll find that C-n (for Ctrl-n) almost always means "move to the next line." Emacs editors tend to differ in the more advanced commands and features, but if you are using these more advanced facilities and you aren't using GNU Emacs, you should consider making the switch.
This third edition covers GNU Emacs 21, specifically 21.3 and even more specifically 21.3.5.[2] This new edition has been completely revised and expanded to cover new features and to meet the evolving needs of Emacs users.
Here are some of the highlights of what we've changed:
• User interface changes, including the addition of an icon-based toolbar, extensive changes to menus, and a more graphical interface (Chapter 1)
• How Emacs interacts with the operating system clipboard, including specific clipboard-related commands (Chapter 2)
• Dynamic abbreviations (Chapter 3)
• Expanded coverage of the directory editor, Dired, to help you organize and work with files more efficiently (Chapter 5)
• Changes to the way Emacs handles tabs and indentation and how to get Emacs to do what you want it to (Chapter 7)
• Artist mode for drawing with the mouse (Chapter 7)
• Inserting characters from other character sets in HTML files (Chapter 8)
• Using font-lock mode for coloring text for easier editing (Chapter 9)
• Expanded Java coverage, including how to install and use the Java Development Environment for Emacs (JDEE) (Chapter 9)
• Perl support with Cperl mode (Chapter 9)
• Managing changes to large, multiple file projects more effectively using etags (Chapter 9)
• Customizing Emacs through the interactive Custom interface or through the .emacs startup file (Chapter 10)
• Expanded coverage of how version control mode connects with a variety of change control systems, including CVS, RCS, Subversion, and SCCS (Chapter 12)
• A new chapter on platform-specific considerations, including details on how to install the latest version of Emacs on Unix, Windows, and Mac OS X (Chapter 13)
You don't need to know its history to use GNU Emacs, but its origins are an interesting part of computer history. The Free Software Foundation (FSF), which maintains and distributes GNU Emacs, has become an important part of computer culture.
A long time ago (1975) at MIT, Richard Stallman wrote the first Emacs editor. According to the folklore, the original Emacs editor was a set of macros for TECO, an almost incomprehensible and now obsolete line editor. The name Emacs stands for "Editing Macros." Tradition also has it that Emacs is a play on the name of a favorite ice cream store. Much has happened since 1975. TECO has slipped into deserved obscurity, and Emacs has been rewritten as an independent program. Several commercial versions of Emacs appeared, of which Unipress Emacs and CCA Emacs were the most important. For several years, these commercial implementations were the Emacs editors you were most likely to run across outside of the academic world.
Stallman's Emacs became prominent with the birth of the Free Software Foundation (FSF) and the GNU Project in 1984. GNU stands for "GNU's Not Unix" and refers to a complete Unix-like operating system (OS) that Stallman and his associates were building.
Stallman founded the FSF to guarantee that some software would always remain free. Note that Free does not necessarily mean cheap (you may have to pay a fee to cover the cost of distribution); it most definitely does mean liberated from restrictions about how it can be used and specifically how it can be shared.
Stallman is widely recognized as the founder of the free software movement, which was an important predecessor of the open source movement. Linux is now the most prominent example of open source software, and it falls under the GNU Public License or GPL (available online at http://www.gnu.org/copyleft/gpl.html). Stallman argues that much of Linux outside the kernel itself is GNU software and so he refers to it as GNU/Linux. All controversies aside, Stallman's contribution to the open source movement cannot be overestimated. GNU software and open source software distributed under the GPL are a mainstay for developers and computer users all over the world.
The FSF was created precisely to distribute programs under terms that encourage you to share, rather than hoard, software. The GPL is designed to prevent an unfortunately common practice—namely, a company taking public domain code, making a few modifications and bug fixes, and then copyrighting the modified version. Once a company does this, the program has essentially become private property and disappears from the public domain. Stallman formed the foundation because he finds this practice abhorrent. As he explains in the GNU Manifesto, "I cannot in good conscience sign a nondisclosure agreement or a software license agreement . . . So that I can continue to use computers without dishonor, I have decided to put together a sufficient body of free software so that I will be able to get along without any software that is not free." Elsewhere in the manifesto, Stallman calls sharing software the "fundamental act of friendship among programmers." Their software is free because it can be shared and will always be shareable—without restriction. FSF software is not under restrictive copyright laws, which Stallman objects to in principle. In fact, he coined the term copyleft to describe the FSF's sharable software base.[3]
Since GNU Emacs was first released, many other pieces of the GNU operating environment have fallen into place: C and C++ compilers (gcc and g++), a very powerful debugger (gdb), substitutes for lex and yacc (called flex and bison, respectively), a Unix shell (bash, which stands for "Bourne-Again Shell"), the Gimp (a graphics tool comparable to Adobe PhotoShop), GNOME (a desktop environment for Linux), and many other programs and libraries. Many important open source projects that originally used variants of the GPL or other licensing schemes have adopted the GPL as their license, including Python, Mozilla, and Zope. Author David Wheeler argues that all open source projects should release their software under a GPL-compatible license[4] (see http://www.dwheeler.com/essays/gpl-compatible.html for his views and some statistics about GPL'd software). With Linux, GNU tools, and other GPL'd software, it's possible to have a complete operating environment consistent with the values set forth by the FSF.
This book is designed to get you started with Emacs as quickly as possible, whether you are an experienced computer user or a novice. The first two chapters give you the basics you need to know, and the rest of the book builds on these basics. After the first two chapters, you don't have to read the rest consecutively; you can skip to the topics that interest you. Additionally, the book is designed to give you just the level of hand-holding you want; you can either read the book in detail or skim it, looking for tables of commands and examples.
Here are some reading paths you could take:
If | Read |
---|---|
You are a casual user | Preface, Chapter 1-Chapter 3, Chapter 14 |
You are a programmer or system administrator | Preface, Chapter 1-Chapter 5, Chapter 9-Chapter 12 |
You are a writer or production person | Preface, Chapter 1-Chapter 3, Chapter 7, Chapter 8, Chapter 14 |
You want to customize Emacs | Chapter 10 and possibly Chapter 11 |
You write HTML or XML | Preface, Chapter 1-Chapter 3, Chapter 8 |
You want to use operating system commands in Emacs | Chapter 5 |
You use Emacs on Windows or Mac OS X | Chapter 13 |
These reading paths are offered only as a guideline. Emacs is one gigantic, functionally rich editor. We've divided it up into digestible bites for you, so you don't have to be put off by its size and scope. The best way to learn Emacs is incrementally; learn a little now, then learn more features as you get curious about them. If you need to do something and don't know how to do it in Emacs, Emacs probably already does it; if it doesn't, you can learn how to write a Lisp function to add it to Emacs (see Chapter 11 for details). The online help system is an excellent place to learn about new features on the fly; online help is discussed in Chapter 1 and in more detail in Chapter 14.
Here's a list of some features you might want to learn about on a rainy day:
• How to use multiple Emacs buffers, windows, and frames (Chapter 4)
• Word abbreviation mode (Chapter 3)
• Macros (Chapter 6)
• How to map function keys to Emacs commands (Chapter 10)
• How to issue (and edit) shell commands (Chapter 5)
• How to organize files in Emacs (Chapter 5)
• Using ediff to compare files (Chapter 12)
Here's a quick summary of what's in each chapter:
Chapter 1, Emacs Basics, tells you how to start Emacs and how to work with files. It also provides a quick introduction to the online help system.
Chapter 2, Editing, explains commands for moving around, copying and pasting text, and undoing changes. It also introduces very basic customization.
Chapter 3, Search and Replace, covers more editing features, including search and replace, word abbreviation mode, and spell checking.
Chapter 4, Using Buffers, Windows, and Frames, describes how to use multiple buffers and windows, both Emacs-style windows (that divide a single OS window) and traditional OS windows (which Emacs refers to as frames). It also discusses how to bookmark your place in large files.
Chapter 5, Emacs as a Work Environment, talks about issuing commands from within Emacs, working with files and directories, and using basic time management tools such as the calendar and diary.
Chapter 6, Writing Macros, discusses using macros to eliminate repetitive tasks.
Chapter 7, Simple Text Formatting and Specialized Editing, covers basic text formatting (such as tabs, indentation, and centering) as well as some of the more rarefied features, like outline mode and rectangle editing.
Chapter 8, Markup Language Support, describes Emacs support for HTML, XML, TEX, and LATEX.
Chapter 9, Computer Language Support, covers Emacs as a programming environment, including editing support for C, Java, Lisp, Perl, and SQL, as well as the interface to compilers and the Unix make utility. It also describes the Java Development Environment for Emacs (JDEE).
Chapter 10, Customizing Emacs, describes Emacs's customization facilities. The interactive Custom tool allows you to change variables without editing your startup file. The chapter also explains how to set up your .emacs customization file. It describes how to modify your display, keyboard commands, and editing environment as well as how to load Lisp packages for extra functionality.
Chapter 11, Emacs Lisp Programming, describes the basics of Emacs Lisp, the language you can use to further customize Emacs.
Chapter 12, Version Control, describes VC mode for version control and its interface to CVS, RCS, Subversion, and SCCS.
Chapter 13, Platform-Specific Considerations, discusses how to install Emacs on Unix, Windows, and Mac OS X. It also provides platform-specific information for Windows and Mac OS X.
Chapter 14, The Help System, describes Emacs's rich, comprehensive online help facilities.
Appendix A, Emacs Variables, lists many important Emacs variables, including all the variables mentioned in this book.
Appendix B, Emacs Lisp Packages, lists some of the most useful Lisp packages that come with Emacs.
Appendix C, Bugs and Bug Fixes, tells you how (and when) to report bugs you find in Emacs. It also describes how to contribute to the GNU Project, whether through code enhancements or monetarily.
Appendix D, Online Resources, gives a tour of some important Emacs-related web sites.
Appendix E, Quick Reference, provides brief descriptions of the most important Emacs commands discussed in this book.
The book concludes with a glossary that defines Emacs terms you'll encounter, an index, and a detachable quick reference card that summarizes important commands for easy access.
GNU Emacs is a large and powerful editor; in this book, we give you only a sample of what it does. Many features have been left out, and more features are added all the time. Some topics, however, are not covered:
Compatibility modes
GNU Emacs provides compatibility modes for vi, for example. We've left a discussion of these modes out. If you really want to use vi or another editor, do so. You're better off getting to know Emacs on its own terms rather than pretending it is something else.
Many programming language modes
In this book, we discuss editing modes for C++, Java, Lisp, Perl, and SQL. There are many modes for other languages, including rare languages like Scheme. There's no way we could discuss everything.
Advanced Lisp programming
GNU Emacs incorporates a complete Lisp interpreter. We give a very basic and brief introduction to Emacs Lisp; Chapter 11 should be enough to get you started, but it really only scratches the surface. We recommend the FSF's Emacs Lisp Reference Manual, now included in the Emacs distribution.
Using Emacs to access the Internet
When our last edition came out, it was common to use Emacs to access Internet resources or read email. Now that isn't so common; better mailers, browsers, and other tools are commonly in use on all platforms.
Unicode support
At present, Emacs is on its way to full Unicode support; that is the most important change slated for the next major release. At this writing, Unicode support is spotty.
Games and amusements
GNU Emacs includes an eclectic bunch of games and amusements, including the ability to pipe random quotations from Zippy the Pinhead into the famous "Eliza" pseudopsychoanalyst. Emacs 21 includes a Games menu under Tools with several cool ways to waste time in Emacs (and it doesn't even include Emacs's version of pong, one of our favorites). Alas, we had to draw the line somewhere.
Emacs commands consist of a modifier, such as Control, which you hold down as you would the Shift key, and a series of keystrokes. For example, Control-x Control-s saves a file.
The other modifier Emacs uses is the Meta key. Few keyboards have keys labeled Meta. Because of this, in previous editions of this book, we refused to talk about the Meta key and substituted Esc in all our instructions.
In this edition, we want you to learn where the Meta key is. Typically Meta keys are to the immediate left and right of the Space bar. On Linux and Windows keyboards, the Alt key is the Meta key. On Mac keyboards, the Apple key, often called Command is the Meta key by default.
Why learn about and use the Meta key? The reason is speed. We emphasize key bindings in this book. New users may find icons and menus helpful, but in the long run, learning how to keep your hands on the keyboard allows you to gain speed and boosts your productivity. The Meta key will help you gain that speed and make it easy for you to use Emacs help, which refers to Meta.
Depending on your style, you may still prefer to use Esc instead of Meta. Just bear in mind that with Esc you press and release the key, then press the next key.
This section covers the conventions used in this book.
Emacs commands consist of a modifier, such as Ctrl or Meta, followed by one or two characters. Commands shown in this book abbreviate Ctrl to C and Meta to M:
C-g
Hold down the Ctrl key and press g.
M-x
Hold down the Meta key and press x.
Sometimes Meta is followed by a literal hyphen character. In these cases, we spell out Meta:
Meta -
Hold down the Meta key and press -.
To complete a command you may need to press Enter. (This key may be labeled Return.)
Enter
Press the Enter key.
Esc
Can be used as an alternative to Meta. Press Esc, release it, then press the next key.
A few mouse commands use the Shift key as a modifier, often in combination with the Ctrl key. This is abbreviated as:
S-right
Hold down Shift and click the right mouse button.
C-S-right
Hold down Shift and Ctrl and click the right mouse button.
All Emacs commands, even the simplest ones, have a full name; for example, forward-word is equivalent to the keystrokes M-f, and forward-char is equivalent to C-f. This tying of a command to a keystroke combination is called a key binding. Some commands have only full names, with no corresponding key binding.
When we discuss a command, we'll give both its full name and the keystrokes (if any) that you can type to invoke it.
To find a group of commands quickly, look for tables in each section that summarize commands. These tables are formatted like this:
Keystrokes | Command name | Action |
---|---|---|
C-n | next-line | Move to the next line. |
C-x C-f File → Open File | find-file | Open a specified file. |
(none) | yow | Print ineffable wisdom from the Pinhead in the minibuffer. |
The first column shows the default key binding for the command, the second column shows the command's full name, and the third column describes what the command does. For example, pressing C-n (also known as the next-line command) moves the cursor to the next line in the file. Some commands, like C-x C-f, can also be reached through menus. If there is a menu option for a particular command, it is given in italics below the keystrokes for the command. For example, you can use the find-file command by typing C-x C-f or by selecting Open File from the File menu. Sometimes you'll see (none) in the keystrokes column, which doesn't mean you can't use the command, but rather that the command isn't bound to particular keystrokes. To use commands with no keystrokes, type M-x, followed by the command's full name, and press Enter. (Try typing M-x pong Enter sometime.)
Throughout the book, you'll find keystrokes to type, followed by a screenshot showing the results.
Type: C-x C-f myfile
Use the find-file command to open a file or create a new file.
C-x C-f is in bold, indicating that this is exactly what you type.
is shown in constant width italics because you could substitute any filename you choose and need not type exactly what you see here.myfile
Typically, these screenshots come from a Linux system. We also include screenshots taken on Mac OS X and Windows. When we show such screenshots, we include an indication of the platform in the caption for the screenshot.
Toward the end of the book, when we're discussing programming modes, customization, and Lisp programming, screenshots become rather unwieldy. We eventually use fewer of them. Instead, we may show one or two lines of text. If it's relevant, we show the cursor's position:
/* This is a c comment */
This book uses the following font conventions:
boldface
Indicates operating system commands, Emacs keystrokes, command names, and variables.
italic
Indicates filenames, URLs, and new terms when first introduced.
constant width
Indicates buffer names, Lisp code, C code, Emacs messages, and other excerpts from programs.
constant width italic
Indicates dummy parameters that you replace with an actual value. May also be shown sometimes in angle brackets (
We have tested and verified the information in this book to the best of our ability, but you may find that features have changed (or even that we have made mistakes!). Please let us know about any errors you find, as well as your suggestions for future editions, by writing to: O'Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 1-800-998-9938 (in the U.S. or Canada) 1-707-829-0515 (international/local) 1-707-829-0104 (FAX)
To ask technical questions or comment on the book, send email to:
bookquestions@oreilly.com
We have a web site for the book, where we'll list examples, errata, and any plans for future editions. You can access this page at:
http://www.oreilly.com/catalog/gnu3/
When you see a Safari® enabled icon on the cover of your favorite technology book that means the book is available online through the O'Reilly Network Safari Bookshelf.
Safari offers a solution that's better than e-Books. It's a virtual library that let's you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you nee the most accurate, current information. Try it free at http://safari.oreilly.com.
For more information about this book and others, see the O'Reilly web site:
http://www.oreilly.com
You can also send questions about Emacs and suggestions about this book to deb@oreilly.com.
Debra Cameron: First, I would like to thank Duffy Craven for introducing me to Emacs. Second, I would like to thank my coauthors. Bill Rosenblatt was a tremendous help on the first edition of this book, and Eric Raymond worked with blinding speed and brilliance on the second, providing some input on the third as well. I would especially like to thank my coauthors Jim Elliott and Marc Loy, without whom, in all honesty, this third edition would never have been finished. Their constant encouragement, support, and hard work helped make this edition a reality. I would like to thank all the readers who wrote in with their suggestions, especially Russell Harris, Seema Kumar, and Hui Oulan. I would also like to thank Eric Pement, who pointed me to the very interesting TEI Emacs add-on, as well as the authors of that extended environment for Emacs, including Sebastian Rahtz and Syd Bauman. Personally, I would like to thank my husband Jim and my kids Meg, David, Beth, and Kevin for their patience and help during the revision of this book and also my friends Irene and Jacki for their support. Most of all, I would like to thank all the developers and hackers who continue to make GNU Emacs the most amazing piece of software I have ever worked with.
James Elliott: I have to thank Deb for asking me to help people learn about Emacs. I've long admired (and relied on) the editor and its ever-growing ecosystem of tools and extensions, as well as the philosophy and results of the Free Software Foundation. They represent a distillation of what makes computing an interesting and valuable field for me, and I am honored to be part of this project. Ironically, I have to also thank Deb for letting me take a big chunk of time off when my Hibernate book came into being.
Thanks are also due to Marc, both for initially introducing me to the fine folks at O'Reilly and for his help and input on this book. He ended up contributing more than he signed up for when I got pulled away in the middle. Nor should I forget my fine colleagues at GE's Corporate Research and Development Center in Niskayuna, New York who first introduced me to the mysteries of Emacs as an intern there. I'm indebted to Joe for his love and support. And let's hear it for the cast of thousands who have grown Emacs into what it is today!
Marc Loy: I have the occasionally lazy—no, let's say overworked—staff at the University of Southern California's computer labs to thank for getting me started on Emacs. They were out of vi cheat sheets when I sat down to write my first computer program. (I won't admit to the language I had to use.) I've been grateful for that happenstance ever since. I'd also like to thank Jim and Deb for their cheery outlook on things as we finished up this latest edition. As always, my sister Amy and my partner Ron remain constant forces for good in my world and make all the silliness (like politics) surrounding the fun stuff (like writing about Emacs) tolerable.
Eric Raymond: My thanks go first to the hacker community at large, all the people who created the rich tradition of Emacs Lisp programming that takes Emacs customization from elegant theoretical possibility to practical tool. I learned what I know partly from reading code written by the likes of Olin Shivers, Jamie Zawinski, Kyle Jones, Barry Warsaw, Roland McGrath, Richard Stallman himself (of course), and many others. Secondly, my thanks and warmest love go as always to my wife Catherine, who supported me on many levels while I worked on my bits of this book. Finally, my thanks and respect to the hip, professional, and clueful people at O'Reilly. They know how to produce a good book and how to treat an author right. They care, and it shows.
Bill Rosenblatt: I would like to thank the following people: Professor Richard Martin (Princeton Classics Department), for planting the seed in me that eventually turned writing from a chore to a pleasure; Intermetrics, Inc., for giving me little enough to do that I could fritter away my workdays delving into GNU Emacs; Hal Stern, for getting me this gig; Sandy Wise, for his help; Jessica Lustig, for her love and support; and most importantly, my grad-school housemates for putting up with a tied-up phone line at all hours of the day and night.