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 9for details.)
Press Tab.

Pressing Tabin 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 Tabtwice 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 Tabon a subsequent line brings you nearly to the end of the line.
Press Tabrepeatedly to the end of the window, press Enter, then press Tabonce.

Emacs moves the cursor to the column where you pressed Enter.
If you press Enterbut don't press Tabat 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.
By default (and if text is not lining up with some previous line of text), tabs are set every eight characters. Emacs allows you to change the positions of the tab stops. To change the tab stops, type M-x edit-tab-stops. A *Tab Stops*
buffer appears.
Type: M-x edit-tab-stops

You now see a tab stop ruler; colons show the locations of tab stops.
The colons in the first line of the display show you where tab stops are currently located. The next two lines form a ruler that shows each character position on the line. To insert a tab, use C-fto move to the desired column, and then type a colon (:). To delete a tab, move to the desired tab, and press Space. The *Tab Stops*
buffer is in overwrite mode, so these changes won't change the position of other tabs. Make sure that you do all your editing in the first line of the display. Changes made to the other lines won't have any effect.
When you're satisfied with the tab stops, press C-c C-cto install them. If you don't make any changes, press C-c C-cto exit the buffer. If you make some changes and then decide you don't want them after all, kill the buffer by typing C-x k Enter. The default tab stops remain in effect.
If you press C-c C-cto install them, the new tab settings affect all buffers that you create but remain in effect for this Emacs session only.
Again, it may well appear to you that this feature doesn't work as you would expect. Because Emacs's default behavior tries to align with preceding lines, changing tab stops really affects only the first line of any buffer.
In this example, we set the first tab at column 51, pressed C-c C-cto install the tab stops, and started a new buffer. Pressing Tabat the beginning of the buffer moves the cursor immediately to column 51. That works fine.
Press Tabonce.

Cursor moves to column 51.
Now we press Taba few more times, followed by Enterto move to a new line.
When we press Tabon the second line, Emacs views the newline as the only item on the last line. Pressing Tabmoves us right to the end of the line.
Press Tabon the next line.

Emacs moves to the end of the line.
As you can see, changing tab stops in this way is of limited efficacy if you're going to add blank lines between rows of your table or whatever you're typing. You'd have to work around this by adding blank lines after typing the whole table, perhaps using a macro as described in Chapter 6.
7.1.3 What if You Want Literal Tabs?
Let's say that all this tab finery is getting on your nerves. You don't want context-sensitive indenting; you don't even want to change tab stops. There is a way to make Emacs treat tabs just like a regular old typewriter did, moving over eight characters at a time. [35]
To insert rigid, typewriter-style tabs, press C-q Tab. In theory, this should insert a tab character into the file, which would look like ^I. In practice, it moves the cursor forward rigidly eight columns.
Type: C-q Tab

The cursor moves eight columns forward and does not align with the text in the previous line.
C-q Tabdoes in fact insert a tab character in the file. You can check that by erasing it with a single press of the Delkey.
One problem with tabs is that there is no universal definition of what a tab means. In vi , the default tab width is four columns versus eight columns in Emacs. Further, Unix generally favors eight columns for tabs while some operating systems tend to use four spaces. Emacs uses eight columns by default no matter what platform it's running on. If you view another user's file in Emacs, Emacs interprets the tabs as eight columns each, throwing things off. For this reason, you might want to set your tab default to four columns by adding this line to your .emacs file:
(setq-default tab-width 4)
You have to press C-q Tabto have the modified tab width take effect.
Another characteristic of Emacs's default behavior is the fact that it may insert a combination of tabs and spaces when you press Tab. Try to erase a few "tabs" and you'll see that often it isn't one character, but the equivalent number of spaces or a combination of tabs and spaces. Of course, this largely depends on the tab stops compared to setting of the tab-width
variable. If you set tab stops that are multiples of six while you have a tab-width
of 4 or 8, Emacs is going to have to use a combination of tabs and spaces to achieve the desired tab stops.
Читать дальше