Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 1: Introduction to Emacs |  | Previous HourNext Hour
Sections in this Hour: |  |

## Hour 1
Introduction to Emacs

You have now started your journey into the world's most powerful editor. To make you understand the power of Emacs, this hour begins by listing some of the features of Emacs; with each feature, a short description or an example is given to show the power of the given feature.

The power of Emacs is split over thousands of functions. Some of them are bound to the keys of the keyboard, whereas others are not. All modern keyboards have a row of twelve function keys, which you can use to make your own personal keybindings. This will hopefully make it possible for you to get the most out of Emacs. A customizable quick reference card, which is described in the section "The Keyboard Quick Reference Card," is shipped with the book.

This hour also discusses the basics of configuring Emacs.

Windows Notes - Windows NT and Windows 95/98 users will find that Emacs is one of the few editors that runs on both Windows and other operating systems. You will learn a lot more about Emacs on Windows, including how to install it, in Hour 2, "Using Emacs in Microsoft Windows." But don't skip ahead; there is much to learn here.

## Overview of Emacs Features

Emacs is the most powerful editor in the world. If you understand the basic ideas behind how it works, you will find that you can do almost anything from within Emacs. Some users love to do all their work from within Emacs (for example, reading and sending mail and news, managing files and directories, and--of course--editing files). Others tend to use it for a more limited set of needs. This section gives you an introduction to the capabilities of Emacs. You might find that you never use some of the features (some people do not want to take the time to learn to use Emacs for email, for example). The important thing is that you are aware of what Emacs can do in case you later have the need to use it.

### Working with Many Files in Different Windows at the Same Time

Emacs makes it possible for you to edit several files at the same time. Some of them might be visible, whereas others might be temporarily hidden. This can be seen in Figure 1.1, which shows two top-level windows.

Figure 1.1
Emacs has the capability to edit several files at once.

Editing several files is especially useful when you write computer programs that are split over several files. You can, for example, look at the definition of a function in one file while you edit its use in another file.

Editing multiple files is described in Hour 6, "Editing Several Files at Once."

### Editing Files on Different Hosts

Besides editing a file that is located on your local hard disk, Emacs also makes it possible for you to edit files that are located on other machines. This is very useful because you do not need to log in to the other machine to edit the file. (If you cannot see how this is useful, just imagine that your specially configured Emacs setup is not available on the other machine.) You can edit files that are located on other machines by downloading the file to your local hard disk using the FTP program; when you save the file, it is uploaded to the remote file again.

Another advantage of this feature is that you can FTP to your own machine as a different user (for example, as the superuser), and then edit files on your local machine as another user. You can do this without having to log in as another user and use his Emacs setup.

This process is described in Hour 4, "Basic Editing."

### Customizable Keyboards and Functions

Emacs has thousands of user-accessible functions for doing all kind of things. Many of these functions are intended for the user to invoke from the keyboard. Given the limited number of keys on the keyboard, however, not every one of these functions is accessible by pressing a few keys. But you can configure the keyboard just the way you like it. In the next section you will learn how to bind functions to the keys labeled F1-F12. In Hour 23, "Binding Keys and Creating Menus," you will learn how to bind functions to any key on the keyboard.

Another--even more important--way in which Emacs can be configured is through user options. User options are used to configure the behavior of functions in minor predefined ways. An example of this is the behavior that occurs when you press the arrow down key on the last line of a file. What does Emacs do in this situation? There are two answers to this question:

- Emacs might ring the bell to indicate that you are at the last line of the file, and that it is therefore not possible to move down one line.

- Emacs might add a blank line to your file and move down to this line.

Which answer you like is totally a matter of user preference; some might like the first solution, whereas others might like the second. Using user options, you can tell Emacs which of these solutions you like; just insert an appropriate line in the .emacs file that is located in your home directory.

Finally, Emacs can be extended by writing Lisp files. These Lisp files can vary in size from a few lines to several megabytes. The GNU news reader that is described in Hours 20, "Gnus Basics," and 21, "Advanced Gnus," is an example of the latter.

Writing extensions in Lisp is beyond the scope of this book. However, in Hour 22, "Learning Lisp Basics," you will learn the basics of Lisp. This will make you capable of configuring Emacs, but it can also serve as a step toward learning Lisp so that you can later develop your own functions.

### Lots of Additional Third-Party Extensions

As was mentioned previously, Emacs can be extended using Lisp functions. Many people have done so, and they have shared the functions with the rest of us. This means that there are thousands of extensions for Emacs. Some merely change a bit about Emacs's behavior in certain situations, whereas others add brand new features to Emacs. Examples include new major modes for editing specific files (such as HTML), on-the-fly spell-checking, loading templates for new files, and interfacing to different programs (such as diff). Many of these extensions are described throughout the book, and several of them are located on the CD. In Hour 24, "Installing Emacs Add-Ons," you will learn how to install new packages.

### Undo and Recovery

Emacs has functions that make it capable of erasing large chunks of text with just a few keystrokes. Therefore, it is of significant importance to note that Emacs has a very powerful undo mechanism. Like some of the more modern editors and word processors, Emacs is not limited to undoing only the latest command; instead, you can undo many hours of work, step by step.

Although Emacs very seldom crashes (this book is written in Emacs, and it has not crashed even once during this writing!), other circumstances make it necessary for Emacs to have a high level of security. This makes it possible for you to recover when your window manager, your operating system, or something else crashes.

Emacs works with two levels of security:

- Whenever you start editing a new file, Emacs saves the original to a backup file; therefore, you can return to the file as it was before you started editing it.

- Emacs saves the files that you are editing, at regular intervals, to a copy that you can recover in case a crash occurs.

The undo and recovery mechanism is described in Hour 6.

### Editing Modes

Emacs has several predefined modes for editing specific types of files (called major modes); examples of these include major modes for C, C++, Java, LaTeX, Perl, Python, Lisp, HTML, SGML, and many more.

These major modes configure Emacs to know a bit about the type of text that you edit. This can help you indent your text in fancy ways (mostly for programming modes), highlight keywords and other constructions using colors, and move around (for example, you can easily go to the beginning of a function or to the beginning of the sentence, depending on the type of text you are editing). Editing modes for LaTeX, HTML, C, C++, and Java are described in Hours 17, "Editing LaTeX and HTML Files," and 18, "Editing C, C++, and Java Files." These hours will provide you with enough understanding to make it easy for you to learn the major modes for the language that you most often use.

### Making the Text More Readable Using Colors

Today's monitors have the capability to show text in different colors, fonts, and shapes. Emacs uses this capability to make the text more readable. From the major mode you are using, Emacs knows enough about your text to show keywords in one face, comments in another, text literals in a third, and so on. An example of this can be seen in Figure 1.2.

Figure 1.2
Emacs highlights text so that you can get an better overview.

In Hour 12, "Visible Editing Utilities," this is described in detail.

### Spell-Checking

One of the major advantages of a computer is its capability to help you spell-check your documents. Emacs, of course, can also do this. There are two different ways in which you can do this, depending on your personal preferences:

- You can spell-check the entire document when you are finished writing it.

- You can spell-check it on-the-fly. Then, whenever you have written a word, it is spell-checked; if it is misspelled, the word is highlighted.

In Figure 1.3 you can see the Emacs interface to spell-checking (when it is done for a whole document). In the beginning, you might think that this interface seems old-fashioned; as you get used to Emacs, however, you will find that it is very pleasant that you do not have to use the mouse for spell-checking.

Figure 1.3
The Emacs interface to spell-checking.

Furthermore, you can ask Emacs to replace some misspelled words for you as you type them. For example, you can have teh replaced with the automatically. Emacs does not do this unless you ask for it, however. It is important to realize that Emacs never does such things behind your back.

The tools that are used for spell-checking are described in Hour 11, "Editing Utilities."

### Search and Search-and-Replace Capabilities

If you think carefully, you might realize that you often search for things in files. For example, you might find yourself

- Searching for a literal string in your current file.

- Searching for a given function in any of the C files of your current programming project.

- Searching for all header lines in your current HTML file.

- Searching for the file in which the words Dotfile Generator appear.

The Emacs function for searching that is the most frequently used is the one that searches for text in your current file (incremental search). You invoke it by pressing C-s and then typing the text for which you want to search. Emacs then advances through the text, looking for each character you type--therefore, it is always located at a match for the text that you have typed so far. This is a very powerful way of searching a file because you seldom need to type many characters of the words for which you are searching before you arrive at the correct location. Figure 1.4 shows an example of an incremental search.

Figure 1.4
Incremental search.

Besides searching for text in a single file, Emacs can also search in all the files of a given project, or even in all the files in a given directory tree.

Apart from searching for ordinary text strings, Emacs can also search using regular expressions. A regular expression is a kind of a pattern that describes some properties for which to search. For example, using a regular expression you can search for

- Lines starting with a space, or empty lines

- Occurrences of the word the, but not then or aesthetic

- The word UserX, UserY, or UserZ, and nothing else (for example not UserA).

Searching for text in a single file is described in Hour 7, "Searching for Text in a Buffer," searching for text in several files is described in Hour 8, "Searching for Text in Multiple Files," and regular expressions are described in Hour 9, "Regular Expressions."

Having said searching, one must also say search-and-replace. The Emacs capability to perform a search-and-replace is as powerful as its capability to search. Search-and-replace is also described in Hours 7-9.

### Compiling and Debugging Programs from Within Emacs

The Emacs editor has been used for many years by programmers; it is therefore especially useful for programming. From within Emacs you can compile and debug programs. Therefore, Emacs can be seen as a fully capable programming environment. The main advantage of this is that you have the same capabilities when compiling and debugging as you have when you write your programs, or even when you write letters to your uncle. (That is, you use the same tools to search for text, insert text, cut and paste, and so on.)

In Figure 1.5 you can see Emacs at work, compiling a program; Figure 1.6 shows a debugging session.

Figure 1.5
Pressing the Enter key while the insertion cursor is on the given line causes Emacs to jump to the position with the error in the input file.

Figure 1.6
A debugging session.

### Powerful Macros

Emacs is very good at making you use your energy to write text rather than to perform trivial, monotonous editing tasks. Emacs accomplishes this through the use of powerful macros. Think of a macro as a recording mechanism, where you can tell Emacs to record your coming keystrokes; you can later execute these keystrokes simply by telling Emacs to retype them. This, however, is not the whole truth about macros...

Although the concept of macros might sound like no big deal, it most certainly is a big deal. If you are aware of your typing, you will find many places in which you perform the same editing tasks over and over again.

The following examples show instances in which a macro might speed up things up. If you don't think that any of the tasks are made much easier with a macro, just imagine that you have to do them several times in a row, or hundreds of times each and every day:

- Removing ^M at the end of every line in the whole file.

- Changing the dictionary that is used for spelling to British.

- Inserting a template for letter heads, and querying for each entry.

Macros are described in Hour 13, "Macros."

### Folding and Hiding Text

When you are working with huge files that contain documents or even programs, you are often faced with the need to have an overview. If writing a book, for example, you might need to see which chapters exist; when you have seen that, you might want to focus on one of the chapters, and then see which sections are included in that particular chapter. Emacs can help you get such an overview by collapsing all the text of a chapter into three dots and showing only the chapter headings. Likewise, it might show only the section headers for a chapter, and so on. In Figure 1.7 you can see the file for this chapter, where all the subsections are hidden except for this section.

Figure 1.7
Getting an overview of the text.

Overviews are discussed in Hour 15, "Getting an Overview of a File."

### Reading/Composing Mail and News

I am assuming that you are not new to computers, so you are probably used to writing email. After you have learned to love Emacs for its editing capabilities, you might find that you often say to yourself, "I wish my mail program did the following editing task as well as Emacs does," or "Gosh, I wish I had Emacs's powerful macros at hand right now." You can, of course, tell most email programs to use Emacs to edit your messages--but that is not a very pleasant solution because it takes some time for Emacs to start up, and the interaction will never be the way that you want it. Well, how about switching to Emacs, and using it for all your mailing needs? The mail reader that is presented in this book is called Gnus. It is, without a doubt, the most powerful mail reader available; and if that is not enough, think about having all Emacs's features (that is, incremental search, macros, and on-the-fly spell-checking) available with your mail program. Furthermore, Gnus is also a news reader, so you can use the same interface for handling news that you use for handling email. Well, what are you waiting for?!

Gnus is described in Hours 20 and 21.

### Extra Help Using the Info System

Built into Emacs is a hyper-reference system that is similar to the World Wide Web, but that is intended for manuals. This system is called the info system. There are two main differences between the info system and the World Wide Web, however. First, no references point outside your computer; you are only referred inside the document or to other documents that are located on your system. Therefore, you do not have to be online to read these manuals. The second difference is that because all the text resides on your local computer, Emacs is capable of searching through all the pages that belong to a single document.

Much information can be found in the info pages. The two most important ones are the Emacs reference manual (which contains much of the same information that is found in this book, but that is in the form of a reference manual and that doesn't have many of the extensions) and the Emacs Lisp reference manual. This manual describes the Emacs Lisp programming language, in which extensions to Emacs are written. Figure 1.8 shows a sample info page.

Figure 1.8
An info page.

Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 1: Introduction to Emacs |  | Previous HourNext Hour
Sections in this Hour: |  |

© Copyright Macmillan USA. All rights reserved.