7.2 Indenting Text
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 Enter to 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 Enter and then Tab again at the beginning of subsequent paragraphs or you could press C-j (for newline-and-indent). Pressing C-j twice 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 Tab to 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-mode or M-x paragraph-indent-minor-mode respectively. If you run the major mode, Emacs displays Parindent on the mode line.
When you press Tab to 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 Tab to 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-q reformats 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-q munges 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-q in 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-paragraphs command.
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-region command, 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-\
Emacs creates a stairstep hanging indent.
So you can see that this works fine if you're indenting one level. If you try this with multiple paragraphs of different indentation levels, indent-region pulls them all to the right, aligning them with the least indented paragraph, probably not what you intended. If you write code, however, this command is great for cleaning up messy indentation.
The other option is to mark the region and type C-x Tab (for indent-rigidly). By default, this command indents only one space, so if you want to indent further, you need to give it an argument. For example, to indent the previous paragraph 15 spaces:
Mark the region then type: M-15 C-x Tab
Emacs indents the paragraph 15 spaces.
Although arguably it can be a pain to supply an argument, indent-rigidly uniformly indents text, leaving indented paragraphs indented. If you find yourself wanting to indent whole files, you may actually want to change the margin settings, as described in the next section.
7.2.3.2 Other indentation tricks
Whenever you are using indentation, you can use M-m (for back-to-indentation) to move to the first nonblank character on a line. On a line that's not indented, this command simply moves you to the beginning of the line. In other words, M-m brings you to the "logical" beginning of the line, which is what you usually mean when you type C-a.
Another indentation command is C-M-o (for split-line). You can use this command to create a stairstep effect. Move the cursor to the text that you want to put on the next line and press C-M-o. Note that there must be some text following the cursor in order for this command to work properly; if you try it at the end of a line, it does nothing.
36
Once upon a time, you had to enter indented text mode explicitly to get the behavior we describe here. Now it is on by default in text mode.