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.
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.
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.
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.
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-f to 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-c to install them. If you don't make any changes, press C-c C-c to 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-c to 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-c to install the tab stops, and started a new buffer. Pressing Tab at the beginning of the buffer moves the cursor immediately to column 51. That works fine.
Now we press Tab a few more times, followed by Enter to move to a new line.
When we press Tab on the second line, Emacs views the newline as the only item on the last line. Pressing Tab moves us right 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.
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.[1]
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 Tab does in fact insert a tab character in the file. You can check that by erasing it with a single press of the Del key.
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 Tab to 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.
If you want Emacs to insert spaces for indentation rather than tab characters, add this line to your .emacs file:
(setq-default indent-tabs-mode nil)
With this setting, Emacs inserts only spaces when you press Tab. Pressing C-q Tab instead inserts a literal tab character. It's safe to say you won't enter tab characters accidentally with this setting.
We've just talked about a way to make sure that Emacs inserts spaces instead of tabs. But what if you inherit a file and it has tabs that you want to change to spaces?
Emacs provides a command to banish tabs from your files. You can use tabs for editing and then convert all of the tabs to the appropriate number of spaces so that the appearance of your file doesn't change. Unlike tabs, a space is almost always well defined. The command for eliminating tabs is M-x untabify. There's a corresponding command to convert spaces into tabs: tabify. However, we trust that you'll take our advice and forget about it.
The untabify command works on a region. Therefore, to use it, you must put the mark somewhere in the buffer (preferably at the beginning), move to some other place in the buffer (preferably the end), and type M-x untabify Enter. The command C-x h (for mark-whole-buffer) automatically puts the cursor at the beginning of the buffer and the mark at the end. It makes untabification a bit easier because you can do it all at once with the simple sequence C-x h M-x untabify Enter.
Table 7-1 shows the tab commands we've covered in this section.
Table 7-1. Tab commands
Keystrokes |
Command name |
Action |
---|---|---|
(none) |
edit-tab-stops |
Open a buffer called |
(none) |
untabify |
Change all tabs into the equivalent number of spaces. |
(none) |
tabify |
Change groups of three or more spaces to tabs where possible without affecting the text placement. |
[1] You can't change tab stops with this method, but you can change tab width. We'll cover this shortly.