Выбрать главу

Chapter 7. Simple Text Formatting and Specialized Editing

Emacs is fundamentally a text editor, rather than a word processor: it is a tool that creates files containing exactly what you see on the screen rather than a tool that makes text files look beautiful when printed. However, Emacs does give you the capability to do the following:

• Indent text using tabs and other indentation tricks.

• Center words, lines, and paragraphs of text.

• Hide and show portions of a document using outline mode, which gives you a feel for a document's overall structure. Outline mode can make it easier to go from rough outline, to detailed outline, to rough draft, to the final product.

• Edit by column rather than by line (especially helpful when you create or change tables or work with column-oriented datasets), referred to in Emacs as rectangle editing.

• Create simple pictures using keyboard characters or the mouse.

Much of this chapter, though, focuses on some fairly simple stuff: tabs and indenting text. We describe Emacs's behavior in primarily two major modes: fundamental mode and text mode. If you are a developer, you'll probably want to write code in a mode appropriate to the language you're using; see Chapter 9 for details. If you use a markup language like HTML, see Chapter 8 for additional relevant information.

7.1 Using Tabs

Tabs provide an easy way to do some simple formatting. While we were revising this book, we found that the way Emacs handles tabs has changed a great deal. This section describes first how Emacs works by default and then discusses what you can do to change the default behavior to meet your needs.

7.1.1 How Emacs 21 Handles Tabs by Default

If you open a new file in text mode, tabs are set every eight spaces by default. (Programming modes have their own indentation behavior; see Chapter 9 for details.)

Press Tab.

Pressing Tab in text mode or fundamental mode inserts a tab character that moves the cursor forward eight columns by default.

Watch what happens when we type a sentence. The default tab stops change automatically.

Type: It was the best of times Enter Tab Tab

Pressing Tab twice moves the cursor under the word was, clearly less than eight columns.

Every time you press Tab, Emacs moves the cursor under the next word. This is the behavior that many people expect when writing code. Neatly lined up code is easier to read.

As we experimented with this feature, we would tab across under each word, and press Enter. What happens next is surprising if you are not expecting it. Emacs considers that newline to be the only character you typed on the line, so pressing Tab on a subsequent line brings you nearly to the end of the line.

Press Tab repeatedly to the end of the window, press Enter, then press Tab once.

Emacs moves the cursor to the column where you pressed Enter.

If you press Enter but don't press Tab at all, the indentation level moves back to the left margin.

Changing tabs to align with each word can be helpful, if, for example, you're typing tables. However, the default tab behavior may not be helpful to you in all situations. If you are interested in changing the default behavior, read on and we'll describe how to get Emacs to do what you want it to do.