Learning GNU Emacs Other Resources from O’Reilly
Total Page:16
File Type:pdf, Size:1020Kb
Learning GNU Emacs Other Resources from O’Reilly Related titles Unix in a Nutshell sed and awk Learning the vi Editor Essential CVS GNU Emacs Pocket Reference Version Control with Subversion oreilly.com oreilly.com is more than a complete catalog of O’Reilly books. You’ll also find links to news, events, articles, weblogs, sample chapters, and code examples. oreillynet.com is the essential portal for developers interested in open and emerging technologies, including new platforms, pro- gramming languages, and operating systems. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in document- ing the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit con- ferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today with a free trial. THIRD EDITION Learning GNU Emacs Debra Cameron, James Elliott, Marc Loy, Eric Raymond, and Bill Rosenblatt Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo Learning GNU Emacs, Third Edition by Debra Cameron, James Elliott, Marc Loy, Eric Raymond, and Bill Rosenblatt Copyright © 2005 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/insti- tutional sales department: (800) 998-9938 or [email protected]. Editors: Debra Cameron and Mike Loukides Production Editor: Jamie Peppard Cover Designer: Edie Freedman Interior Designer: Melanie Wang Printing History: October 1991: First Edition. April 1992: Minor corrections. September 1996: Second Edition. December 2004: Third Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning GNU Emacs, the image of the gnu, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book uses RepKover™, a durable and flexible lay-flat binding. ISBN: 0-596-00648-9 ISBN13: 978-0-596-00648-8 [M] [1/08] Table of Contents Preface . ix 1. Emacs Basics . 1 Introducing Emacs! 1 Understanding Files and Buffers 3 A Word About Modes 3 Starting Emacs 5 About the Emacs Display 6 Emacs Commands 10 Opening a File 11 Saving Files 15 Leaving Emacs 15 Getting Help 16 Summary 18 2. Editing . 20 Moving the Cursor 22 Deleting Text 27 Marking Text to Delete, Move, or Copy 32 Emacs and the Clipboard 37 Editing Tricks and Shortcuts 39 Canceling Commands and Undoing Changes 41 Making Emacs Work the Way You Want 45 3. Search and Replace . 49 Different Kinds of Searches 49 Search and Replace 55 v Checking Spelling Using Ispell 64 Word Abbreviations 74 4. Using Buffers, Windows, and Frames . 82 Understanding Buffers, Windows, and Frames 82 Working with Multiple Buffers 85 Working with Windows 88 Working with Frames 93 More About Buffers 95 More About Windows 102 Holding Your Place with Bookmarks 106 5. Emacs as a Work Environment . 114 Executing Commands in Shell Buffers 114 Using Dired, the Directory Editor 123 Printing from Emacs 140 Reading Manpages in Emacs 141 Using Time Management Tools 141 6. Writing Macros . 150 Defining a Macro 151 Tips for Creating Good Macros 154 A More Complicated Macro Example 155 Editing a Macro 157 The Macro Ring 160 Binding Your Macro to a Key 160 Naming, Saving, and Executing Your Macros 161 Building More Complicated Macros 161 Executing Macros on a Region 167 Beyond Macros 168 7. Simple Text Formatting and Specialized Editing . 170 Using Tabs 170 Indenting Text 176 Centering Text 186 Using Outline Mode 187 Rectangle Editing 194 Making Simple Drawings 204 vi | Table of Contents 8. Markup Language Support . 218 Comments 219 Font-Lock Mode 220 Writing HTML 220 Writing XML 243 Marking up Text for TEX and LATEX 258 9. Computer Language Support . 263 Emacs as an IDE 264 Writing Code 266 C and C++ Support 275 Java Support 284 The Java Development Environment for Emacs (JDEE) 285 Perl Support 294 SQL Support 296 The Lisp Modes 298 10. Customizing Emacs . 306 Using Custom 307 Modifying the .emacs File Directly 326 Modifying Fonts and Colors 330 Customizing Your Key Bindings 335 Setting Emacs Variables 339 Finding Emacs Lisp Packages 340 Starting Modes via Auto-Mode Customization 341 Making Emacs Work the Way You Think It Should 342 11. Emacs Lisp Programming . 344 Introduction to Lisp 345 Lisp Primitive Functions 353 Useful Built-in Emacs Functions 358 Building an Automatic Template System 374 Programming a Major Mode 381 Customizing Existing Modes 389 Building Your Own Lisp Library 395 12. Version Control . 398 The Uses of Version Control 398 Version Control Concepts 399 Table of Contents | vii How VC Helps with Basic Operations 401 Editing Comment Buffers 403 VC Command Summary 403 VC Mode Indicators 404 Which Version Control System? 405 Individual VC Commands 405 Customizing VC 411 Extending VC 412 What VC Is Not 413 Using VC Effectively 413 Comparing with Ediff 414 13. Platform-Specific Considerations . 421 Emacs and Unix 421 Emacs and Mac OS X 427 Emacs and Windows 433 14. The Help System . 440 Using the Tutorial 440 Help Commands 441 Help with Complex Emacs Commands 445 Navigating Emacs Documentation 446 Completion 453 A. Emacs Variables . 457 B. Emacs Lisp Packages . 464 C. Bugs and Bug Fixes . 470 D. Online Resources . 472 E. Quick Reference . 475 Glossary . 487 Index . 493 viii | Table of Contents Preface 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 envi- ronment. 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 sys- tems like X and Microsoft Windows became popular, Emacs often served as a com- plete 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 will- ing to take the time) do just about anything you want. Why Read This Book? 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 webauthors to programmers to system administra- tors. 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 ix This is the Title of the Book, eMatter Edition Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved. • 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 com- mon misspellings Of course, many of the topics may not apply to you; some topics may be appropri- ate 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. Which Emacs Is Which? 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.* 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.