1.2 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.
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 Enterat 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-1lists 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 Chapter 5. Emacs as a Work Environment Many of the everyday things you do from a command prompt can be done from within Emacs. You can execute commands, work with directories, and print files—all without leaving Emacs. Changing tasks is as simple as jumping between buffers. What's important about this? Of course, it's nice to be able to move between tasks easily. What's even more important is that you have the same editing environment no matter what you're doing: you can use all of the Emacs editing commands to work on a file, give shell commands, then start up Dired, the directory editor, to do some file maintenance. It is simple to move text from one window to another. You can execute a command and then use Emacs commands to cut and paste the results into a file. If you're trying to compile a program and keep getting error messages, you can save the interactive session as a file and confer with someone about the problem. Despite the many advantages of modern window systems, Emacs often provides the best way to integrate the many kinds of work you do daily. Much of the information in this chapter involves integration between Emacs and the operating system. Emacs is most commonly a Unix editor, so forgive us for a bias in that direction. But we are happy to report that for users of GNU Emacs on other platforms, integration with the operating system is still available; you can use shell mode to run commands and can edit directories with Dired. There's no reason to leave Emacs no matter what your platform is. ) |
Outline mode |
For writing outlines ( Chapter 7) |
Indented text mode |
For indenting text automatically ( Chapter 7) |
Paragraph indent text mode |
For indenting the first line of each paragraph ( Chapter 7) |
Picture mode |
For creating ASCII drawings using the keyboard ( Chapter 7) |
HTML mode |
For writing HTML ( Chapter 8) |
SGML mode |
For writing SGML and XML ( Chapter 8) |
LaTeX mode |
For formatting files for TEX and LATEX ( Chapter 8) |
Compilation mode |
For compiling programs ( Chapter 9) |
cc mode |
For writing C, C++, and Java programs ( Chapter 9) |
Java mode |
For writing Java programs ( Chapter 9) |
Perl mode and Cperl mode |
For writing Perl programs ( Chapter 9) |
SQL mode |
For interacting with databases using SQL ( Chapter 9) |
Emacs Lisp mode |
For writing Emacs Lisp functions ( Chapter 9and Chapter 11) |
Lisp mode |
For writing Lisp programs ( Chapter 9and Chapter 11) |
Lisp interaction mode |
For writing and evaluating Lisp expressions ( Chapter 9and Chapter 11) |
Whenever you edit a file, Emacs attempts to put you into the correct major mode for what you're going to edit. If you edit a file that ends in .c , it puts you into cc mode. If you edit a file that ends in .el , it puts you in Lisp mode. Sometimes it looks at the contents of the file rather than just its name. If you edit a file formatted for TEX, Emacs puts you in LaTeX mode. If it cannot tell what mode you should be in, it puts you in fundamental mode, the most general of all. Because Emacs is extensible, add-in modes are also available; we talk about some in this book, though we do not list them in Table 1-1.
In addition to major modes there are also minor modes . These define a particular aspect of Emacs's behavior and can be turned on and off within a major mode. For example, auto-fill mode means that Emacs should do word wrap; when you type a long line, it should automatically make an appropriate line break. Table 1-2lists some minor modes, what they do, and where they're covered in this book.
Table 1-2. Minor modes
Mode |
Function |
Auto-fill mode |
Enables word wrap ( Chapter 2). |
Overwrite mode |
Replaces characters as you type instead of inserting them ( Chapter 2). |
Auto-save mode |
Saves your file automatically every so often in a special auto-save file ( Chapter 2). |
Isearch mode |
For searching ( Chapter 3 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. ). |
Flyspell mode |
For flyspell spell-checker ( Chapter 3 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. ). |
Flyspell prog mode |
For spell-checking programs with flyspell ( Chapter 3 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. ). |
Abbrev mode |
Allows you to use word abbreviations ( Chapter 3 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. ). |
Paragraph indent text mode |
For indenting the first line of each paragraph ( Chapter 7). |
Refill mode |
A mode in which Emacs attempts to fill paragraphs as you edit them (a bit experimental; mentioned in Chapter 2). |
Artist mode |
For creating ASCII drawings using the mouse ( Chapter 7). |
Outline mode |
For writing outlines ( Chapter 7). |
SGML name entity mode |
For inserting special characters in HTML, SGML, and XML documents ( Chapter 8). |
ISO accents mode |
For inserting accented characters in text files. |
Font lock mode |
For highlighting text in colors and fonts to improve readability (separating, for example, comments from code visually) ( Chapter 9). |
Compilation mode |
For compiling programs ( Chapter 9). |
Enriched mode |
For saving text attributes ( Chapter 10). |
VC mode |
For using various version control systems under Emacs ( Chapter 12). |
Info mode |
A mode for reading Emacs's own documentation ( Chapter 14 Chapter 14. The Help System Emacs has the most comprehensive help facility of any text editor—and one of the best such facilities of any program at all. In fact, the Emacs help facilities probably cut down the time it took for us to write this book by an order of magnitude, and they can help you immeasurably in your ongoing quest to learn more about Emacs. In this chapter, we describe Emacs help in the following areas: • The tutorial. • The help key ( C-h ) and Help menu, which allow you to get help on a wide variety of topics. • The help facilities of complex commands like query-replace and dired . • Navigating Emacs manuals and using the info documentation reader. • Completion , in which Emacs helps you finish typing names of functions, variables, filenames, and more. Completion not only saves you time and helps you complete names of functions you know about but can help you discover new commands and variables. ). |
You may have noticed that several modes, including paragraph indent text mode, outline mode, and compilation mode, are both major and minor modes. Each can be used alone—as a major mode—or with another major mode as a minor mode.
Читать дальше