1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985-1987, 1993-1995, 1997, 2001-2011
3 @c Free Software Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5 @node Indentation, Text, Modes, Top
9 @cindex columns (indentation)
11 This chapter describes the Emacs commands that add, remove, or
16 Indent the current line appropriately, in a mode-dependent fashion.
18 Perform @key{RET} followed by @key{TAB} (@code{newline-and-indent}).
20 Merge the previous and the current line (@code{delete-indentation}).
21 This would cancel the effect of a preceding @kbd{C-j}.
23 Split the current line at point; text on the line after point becomes a
24 new line indented to the same column where point is located
27 Move (forward or back) to the first nonblank character on the current
28 line (@code{back-to-indentation}).
30 Indent lines in the region to the same column (@code{indent-region}).
32 Shift lines in the region rigidly right or left (@code{indent-rigidly}).
34 Indent from point to the next prespecified tab stop column
35 (@code{tab-to-tab-stop}).
36 @item M-x indent-relative
37 Indent from point to under an indentation point in the previous line.
41 The @key{TAB} key runs @code{indent-for-tab-command} in most major
42 modes (in C and related modes, @key{TAB} runs a separate command,
43 @code{c-indent-line-or-region}, which behaves similarly). The major
44 mode determines just what this entails.
46 In text modes, @key{TAB} inserts some combination of space and tab
47 characters to advance point to the next tab stop (@pxref{Tab Stops}).
48 If the region is active and spans multiple lines, it advances the
49 first character of each of those lines to the next tab stop
50 (@pxref{Using Region}). For the purposes of this command, the
51 position of the first non-whitespace character on the preceding line
52 is treated as an additional tab stop. Thus, you can use @key{TAB} to
53 ``align'' point with the preceding line.
55 In programming modes, @key{TAB} adds or removes some combination of
56 space and tab characters at the start of the line, in a way that makes
57 sense given the text in the preceding lines. If the region is active
58 and spans multiple lines, all those lines are indented this way. If
59 point was initially within the current line's indentation, it is
60 positioned after that indentation; otherwise, it remains at same point
61 in the newly-indented text. @xref{Program Indent}.
64 Normally, indentation commands insert (or remove) an optimal mix of
65 @dfn{tab characters} and spaces to align to the desired column. Tab
66 characters (@acronym{ASCII} code 9) are displayed as a stretch of
67 empty space extending to the next @dfn{display tab stop}. By default,
68 there is one display tab stop every eight columns; the number of
69 columns is determined by the variable @code{tab-width}. You can
70 insert a single tab character by typing @kbd{C-q @key{TAB}}.
73 @findex edit-tab-stops
74 @findex tab-to-tab-stop
76 The command @kbd{M-i} (@code{tab-to-tab-stop}) adjusts the
77 whitespace characters around point, inserting just enough whitespace
78 to advance point up to the next tab stop. By default, this involves
79 deleting the existing whitespace and inserting a single tab character.
81 @xref{Just Spaces}, for how to disable use of tabs. However,
82 @kbd{C-q @key{TAB}} always inserts a tab, even when tabs are disabled
83 for the indentation commands.
85 @vindex tab-always-indent
86 The variable @code{tab-always-indent} tweaks the behavior of the
87 @key{TAB} (@code{indent-for-tab-command}) command. The default value,
88 @code{t}, gives the behavior described above. If you change the value
89 to the symbol @code{complete}, then @key{TAB} first tries to indent
90 the current line, and if the line was already indented, it tries to
91 complete the text at point (@pxref{Symbol Completion}). If the value
92 is @code{nil}, then @key{TAB} indents the current line only if point
93 is at the left margin or in the line's indentation; otherwise, it
94 inserts a real tab character.
97 * Indentation Commands:: Various commands and techniques for indentation.
98 * Tab Stops:: You can set arbitrary "tab stops" and then
99 indent to the next tab stop when you want to.
100 * Just Spaces:: You can request indentation using just spaces.
103 @node Indentation Commands, Tab Stops, Indentation, Indentation
104 @section Indentation Commands and Techniques
107 @findex back-to-indentation
108 To move over the indentation on a line, do @kbd{M-m}
109 (@code{back-to-indentation}). This command, given anywhere on a line,
110 positions point at the first nonblank character on the line, if any,
111 or else at the end of the line.
113 To insert an indented line before the current line, do @kbd{C-a C-o
114 @key{TAB}}. To make an indented line after the current line, use
117 If you just want to insert a tab character in the buffer, type
122 @kbd{C-M-o} (@code{split-line}) moves the text from point to the end of
123 the line vertically down, so that the current line becomes two lines.
124 @kbd{C-M-o} first moves point forward over any spaces and tabs. Then it
125 inserts after point a newline and enough indentation to reach the same
126 column point is on. Point remains before the inserted newline; in this
127 regard, @kbd{C-M-o} resembles @kbd{C-o}.
130 @findex delete-indentation
131 To join two lines cleanly, use the @kbd{M-^}
132 (@code{delete-indentation}) command. It deletes the indentation at
133 the front of the current line, and the line boundary as well,
134 replacing them with a single space. As a special case (useful for
135 Lisp code) the single space is omitted if the characters to be joined
136 are consecutive open parentheses or closing parentheses, or if the
137 junction follows another newline. To delete just the indentation of a
138 line, go to the beginning of the line and use @kbd{M-\}
139 (@code{delete-horizontal-space}), which deletes all spaces and tabs
142 If you have a fill prefix, @kbd{M-^} deletes the fill prefix if it
143 appears after the newline that is deleted. @xref{Fill Prefix}.
147 @findex indent-region
148 @findex indent-rigidly
149 There are also commands for changing the indentation of several lines
150 at once. They apply to all the lines that begin in the region.
151 @kbd{C-M-\} (@code{indent-region}) indents each line in the ``usual''
152 way, as if you had typed @key{TAB} at the beginning of the line. A
153 numeric argument specifies the column to indent to, and each line is
154 shifted left or right so that its first nonblank character appears in
155 that column. @kbd{C-x @key{TAB}} (@code{indent-rigidly}) moves all of
156 the lines in the region right by its argument (left, for negative
157 arguments). The whole group of lines moves rigidly sideways, which is
158 how the command gets its name.
160 @cindex remove indentation
161 To remove all indentation from all of the lines in the region,
162 invoke @kbd{C-x @key{TAB}} with a large negative argument, such as
165 @findex indent-relative
166 @kbd{M-x indent-relative} indents at point based on the previous line
167 (actually, the last nonempty line). It inserts whitespace at point, moving
168 point, until it is underneath the next indentation point in the previous line.
169 An indentation point is the end of a sequence of whitespace or the end of
170 the line. If point is farther right than any indentation point in the
171 previous line, @code{indent-relative} runs @code{tab-to-tab-stop}
178 unless it is called with a numeric argument, in which case it does
181 @xref{Format Indentation}, for another way of specifying the
182 indentation for part of your text.
184 @node Tab Stops, Just Spaces, Indentation Commands, Indentation
188 @cindex using tab stops in making tables
189 @cindex tables, indentation for
191 @findex tab-to-tab-stop
192 For typing in tables, you can use @kbd{M-i} (@code{tab-to-tab-stop}).
193 This command inserts indentation before point, enough to reach the
194 next tab stop column.
196 @findex edit-tab-stops
197 @findex edit-tab-stops-note-changes
198 @kindex C-c C-c @r{(Edit Tab Stops)}
199 @vindex tab-stop-list
200 You can change the tab stops used by @kbd{M-i} and other indentation
201 commands, so that they need not be spaced every eight characters, or
202 even regularly spaced. The tab stops are stored in the variable
203 @code{tab-stop-list}, as a list of column numbers in increasing order.
205 A convenient way to set the tab stops is with @kbd{M-x
206 edit-tab-stops}, which creates and selects a buffer containing a
207 description of the tab stop settings. You can edit this buffer to
208 specify different tab stops, and then type @kbd{C-c C-c} to make those
209 new tab stops take effect. The buffer uses Overwrite mode
210 (@pxref{Minor Modes}). @code{edit-tab-stops} records which buffer was
211 current when you invoked it, and stores the tab stops back in that
212 buffer; normally all buffers share the same tab stops and changing
213 them in one buffer affects all, but if you happen to make
214 @code{tab-stop-list} local in one buffer then @code{edit-tab-stops} in
215 that buffer will edit the local settings.
217 Here is what the text representing the tab stops looks like for ordinary
218 tab stops every eight columns.
223 0123456789012345678901234567890123456789012345678
224 To install changes, type C-c C-c
227 The first line contains a colon at each tab stop. The remaining lines
228 are present just to help you see where the colons are and know what to do.
230 Note that the tab stops that control @code{tab-to-tab-stop} have
231 nothing to do with how tab characters are displayed in the buffer.
232 Tab characters are always displayed as empty spaces extending to the
233 next display tab stop, which occurs every @code{tab-width} columns
234 regardless of the contents of @code{tab-stop-list}. @xref{Text
237 @node Just Spaces,, Tab Stops, Indentation
238 @section Tabs vs. Spaces
240 @vindex indent-tabs-mode
241 Emacs normally uses both tabs and spaces to indent lines. If you
242 prefer, all indentation can be made from spaces only. To request
243 this, set @code{indent-tabs-mode} to @code{nil}. This is a per-buffer
244 variable, so altering the variable affects only the current buffer,
245 but there is a default value which you can change as well.
248 A tab is not always displayed in the same way. By default, tabs are
249 eight columns wide, but some people like to customize their editors to
250 use a different tab width (e.g., by changing the variable
251 @code{tab-width} in Emacs). By using spaces only, you can make sure
252 that your file looks the same regardless of the tab width setting.
256 There are also commands to convert tabs to spaces or vice versa, always
257 preserving the columns of all nonblank text. @kbd{M-x tabify} scans the
258 region for sequences of spaces, and converts sequences of at least two
259 spaces to tabs if that can be done without changing indentation. @kbd{M-x
260 untabify} changes all tabs in the region to appropriate numbers of spaces.