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 Tabinstead inserts a literal tab character. It's safe to say you won't enter tab characters accidentally with this setting.
7.1.6 Changing Tabs to Spaces (and Vice Versa)
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 untabifycommand 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-1shows 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 *Tab Stops* where you can change the tab settings. |
( 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. |
Emacs provides the ability to indent paragraphs, like a block quote in a paper. It also allows you to use a paragraph style that indents just the first line of a paragraph. This section describes indentation-related commands, including how to change the margins for the current session.
Before we start, make sure you're in text mode. Look at the mode line and, if the word Text
is displayed, you are in text mode. If not, type M-x text-mode Enterto enter text mode.
7.2.1 Indenting Paragraphs
Let's say you're writing a paper and want to include some indented block quotes. Emacs's default behavior makes this a no-brainer. [36]After you finish your first paragraph, use tabs or spaces to indent to the desired level and start typing the quote. Emacs automatically fills the paragraph and the quote correctly, as shown in the following screen.
Some indented text:

Emacs indents the text properly and fills it correctly in auto-fill mode.
What if an indented quote has multiple paragraphs? You could just press Enterand then Tabagain at the beginning of subsequent paragraphs or you could press C-j(for newline-and-indent). Pressing C-jtwice gives you a blank line between paragraphs.
7.2.2 Indenting the First Line of a Paragraph
Some people prefer paragraphs in which the first line is indented. Knowing about the intricacies of tabs, you might be concerned that pressing Tabto indent the opening line of your paragraph will incite Emacs to indent the whole paragraph as you continue typing. And it would, to be honest.
Emacs provides a special mode for this purpose: paragraph indent text mode. It's also available as a minor mode. Enter either M-x paragraph-indent-text-modeor M-x paragraph-indent-minor-moderespectively. If you run the major mode, Emacs displays Parindent
on the mode line.
When you press Tabto start a paragraph, Emacs inserts a tab's worth of space. When you start a new paragraph, you don't have to skip a line in between and pressing Tabto start that second paragraph yields again a tab's worth of space, not aligning with the second word of the previous line as Emacs would do in text mode or fundamental mode.
Pressing M-qreformats paragraphs without mushing them all together. If you prefer indented paragraphs, this mode is exactly what you want. When you need to indent a block quote, you may want to temporarily enter text mode to make it easier and add your paragraph indentations manually.
7.2.3 Filling Indented Paragraphs
Let's say you've got a paper with paragraphs indented at various levels. What if you edit them and need to fill them again? Especially if there are no blank lines in between paragraphs, M-qmunges all the text into one big (nonindented) paragraph. Instead of M-q, mark the region in question and use a special fill command: M-x fill-individual-paragraphs. Emacs preserves each paragraph's indentation.
Let's contrast these two commands with an example. We'll use our previous Henry James example, but delete the lines between paragraphs to show what happens if you use M-qin this case. These paragraphs need to be reformatted.
Initial state:

Some sample paragraphs from Henry James, in need of reformatting.
Type: M-q

Emacs munges it all into one large paragraph.
We'll undo that command, mark the buffer as a region, and use the fill-individual-paragraphscommand.
Type: C- _ C-x h M-x fill-individual-paragraphs Enter

Emacs refills the paragraphs properly.
7.2.3.1 Indenting regions
What if you have already typed your text without indentation and want to indent it later? Two commands can handle this, depending on how far you want to indent the region.
The indent-regioncommand, bound to C-M-\, can indent a region one level easily. If you want to indent two levels, it is unpredictable. (This command is designed for indenting code.)
Here's an example. The second paragraph is marked as a region.
Type: C-M-\

Emacs indents the paragraph one level.
You decide that's not far enough.
Type: C-M-\

Читать дальше