indent.texi 10.6 KB
Newer Older
Dave Love's avatar
#  
Dave Love committed
1
@c This is part of the Emacs manual.
2
@c Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997, 2001, 2002,
Glenn Morris's avatar
Glenn Morris committed
3
@c   2003, 2004, 2005, 2006, 2007, 2008  Free Software Foundation, Inc.
Dave Love's avatar
#  
Dave Love committed
4 5 6 7
@c See file emacs.texi for copying conditions.
@node Indentation, Text, Major Modes, Top
@chapter Indentation
@cindex indentation
8
@cindex tabs
Dave Love's avatar
#  
Dave Love committed
9 10 11 12 13 14 15
@cindex columns (indentation)

  This chapter describes the Emacs commands that add, remove, or
adjust indentation.

@table @kbd
@item @key{TAB}
16
Indent the current line appropriately, in a mode-dependent fashion.
Dave Love's avatar
#  
Dave Love committed
17 18 19
@item @kbd{C-j}
Perform @key{RET} followed by @key{TAB} (@code{newline-and-indent}).
@item M-^
20
Merge the previous and the current line (@code{delete-indentation}).
21
This would cancel the effect of a preceding @kbd{C-j}.
Dave Love's avatar
#  
Dave Love committed
22
@item C-M-o
Eli Zaretskii's avatar
Eli Zaretskii committed
23 24 25
Split the current line at point; text on the line after point becomes a
new line indented to the same column where point is located
(@code{split-line}).
Dave Love's avatar
#  
Dave Love committed
26 27 28 29
@item M-m
Move (forward or back) to the first nonblank character on the current
line (@code{back-to-indentation}).
@item C-M-\
30
Indent lines in the region to the same column (@code{indent-region}).
Dave Love's avatar
#  
Dave Love committed
31
@item C-x @key{TAB}
32
Shift lines in the region rigidly right or left (@code{indent-rigidly}).
Dave Love's avatar
#  
Dave Love committed
33 34 35 36 37 38 39
@item M-i
Indent from point to the next prespecified tab stop column
(@code{tab-to-tab-stop}).
@item M-x indent-relative
Indent from point to under an indentation point in the previous line.
@end table

Chong Yidong's avatar
Chong Yidong committed
40
  In most major modes, the @key{TAB} key runs the command
41 42 43
@code{indent-for-tab-command}, which either performs indentation or
inserts whitespace at point, depending on the situation.

Chong Yidong's avatar
Chong Yidong committed
44
  In programming modes such as Lisp mode and C mode, @key{TAB} indents
45 46 47 48 49
the current line if the region is inactive.  If the region is active,
it indents every line in the region (@pxref{Mark}).  Indentation means
adding or removing some combination of space and tab characters
(@dfn{whitespace characters}) at the start of the line, in a way that
makes sense given the text in the preceding lines.  Exactly how
50 51
indentation is performed depends on the major mode.  @xref{Program
Indent}.
52

Chong Yidong's avatar
Chong Yidong committed
53
  In text modes, @key{TAB} inserts some whitespace characters to
54 55 56
advance point to the next tab stop (@pxref{Tab Stops}).  For the
purposes of this command, the position of the first non-whitespace
character on the preceding line is treated as an additional tab stop.
Chong Yidong's avatar
Chong Yidong committed
57 58
You can therefore use @key{TAB} to ``align'' point with the preceding
line.  If the region is active, @key{TAB} performs this action on
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
every line in the region.

@vindex tab-width
  Indentation is often performed with the help of @dfn{tab characters}
(ASCII code 9), which are displayed as a stretch of empty space
extending to the next @dfn{display tab stop}.  By default, there is
one display tab stop every eight columns; the number of columns is
determined by the variable @code{tab-width}.  You can insert a single
tab character by typing @kbd{C-q @key{TAB}}.  @xref{Text Display}.

@findex edit-tab-stops
@findex tab-to-tab-stop
@kindex M-i
  The command @kbd{M-i} (@code{tab-to-tab-stop}) adjusts the
whitespace characters around point, inserting just enough whitespace
to advance point up to the next tab stop.  By default, this involves
deleting the existing whitespace and inserting a single tab character.

  Normally, most of these indentation commands insert an optimal mix
of tabs and spaces to align to the desired column.  @xref{Just
Spaces}, for how to disable use of tabs.  However, @kbd{C-q @key{TAB}}
always inserts a tab, even when tabs are disabled for the indentation
commands.
82

Dave Love's avatar
#  
Dave Love committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96
@menu
* Indentation Commands::  Various commands and techniques for indentation.
* Tab Stops::             You can set arbitrary "tab stops" and then
                            indent to the next tab stop when you want to.
* Just Spaces::           You can request indentation using just spaces.
@end menu

@node Indentation Commands, Tab Stops, Indentation, Indentation
@section Indentation Commands and Techniques

@kindex M-m
@findex back-to-indentation
  To move over the indentation on a line, do @kbd{M-m}
(@code{back-to-indentation}).  This command, given anywhere on a line,
97 98
positions point at the first nonblank character on the line, if any,
or else at the end of the line.
Dave Love's avatar
#  
Dave Love committed
99 100 101 102 103

  To insert an indented line before the current line, do @kbd{C-a C-o
@key{TAB}}.  To make an indented line after the current line, use
@kbd{C-e C-j}.

104
  If you just want to insert a tab character in the buffer, type
Dave Love's avatar
#  
Dave Love committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118
@kbd{C-q @key{TAB}}.

@kindex C-M-o
@findex split-line
  @kbd{C-M-o} (@code{split-line}) moves the text from point to the end of
the line vertically down, so that the current line becomes two lines.
@kbd{C-M-o} first moves point forward over any spaces and tabs.  Then it
inserts after point a newline and enough indentation to reach the same
column point is on.  Point remains before the inserted newline; in this
regard, @kbd{C-M-o} resembles @kbd{C-o}.

@kindex M-^
@findex delete-indentation
  To join two lines cleanly, use the @kbd{M-^}
Richard M. Stallman's avatar
Richard M. Stallman committed
119 120 121 122 123 124 125
(@code{delete-indentation}) command.  It deletes the indentation at
the front of the current line, and the line boundary as well,
replacing them with a single space.  As a special case (useful for
Lisp code) the single space is omitted if the characters to be joined
are consecutive open parentheses or closing parentheses, or if the
junction follows another newline.  To delete just the indentation of a
line, go to the beginning of the line and use @kbd{M-\}
Dave Love's avatar
#  
Dave Love committed
126 127 128 129 130 131 132 133 134 135 136
(@code{delete-horizontal-space}), which deletes all spaces and tabs
around the cursor.

  If you have a fill prefix, @kbd{M-^} deletes the fill prefix if it
appears after the newline that is deleted.  @xref{Fill Prefix}.

@kindex C-M-\
@kindex C-x TAB
@findex indent-region
@findex indent-rigidly
  There are also commands for changing the indentation of several lines
137 138 139 140 141 142 143
at once.  They apply to all the lines that begin in the region.
@kbd{C-M-\} (@code{indent-region}) indents each line in the ``usual''
way, as if you had typed @key{TAB} at the beginning of the line.  A
numeric argument specifies the column to indent to, and each line is
shifted left or right so that its first nonblank character appears in
that column.  @kbd{C-x @key{TAB}} (@code{indent-rigidly}) moves all of
the lines in the region right by its argument (left, for negative
Dave Love's avatar
#  
Dave Love committed
144
arguments).  The whole group of lines moves rigidly sideways, which is
Richard M. Stallman's avatar
Richard M. Stallman committed
145
how the command gets its name.
Dave Love's avatar
#  
Dave Love committed
146

147
@cindex remove indentation
Richard M. Stallman's avatar
Richard M. Stallman committed
148 149 150
  To remove all indentation from all of the lines in the region,
invoke @kbd{C-x @key{TAB}} with a large negative argument, such as
-1000.
151

Dave Love's avatar
#  
Dave Love committed
152 153 154
@findex indent-relative
  @kbd{M-x indent-relative} indents at point based on the previous line
(actually, the last nonempty line).  It inserts whitespace at point, moving
155
point, until it is underneath the next indentation point in the previous line.
Dave Love's avatar
#  
Dave Love committed
156 157
An indentation point is the end of a sequence of whitespace or the end of
the line.  If point is farther right than any indentation point in the
158
previous line, @code{indent-relative} runs @code{tab-to-tab-stop}
159
@ifnottex
160
(@pxref{Tab Stops}),
161
@end ifnottex
Dave Love's avatar
#  
Dave Love committed
162
@iftex
163
(see next section),
Dave Love's avatar
#  
Dave Love committed
164
@end iftex
165 166
unless it is called with a numeric argument, in which case it does
nothing.
Dave Love's avatar
#  
Dave Love committed
167 168 169 170 171 172 173

  @xref{Format Indentation}, for another way of specifying the
indentation for part of your text.

@node Tab Stops, Just Spaces, Indentation Commands, Indentation
@section Tab Stops

174
@cindex tab stops
Dave Love's avatar
#  
Dave Love committed
175 176 177 178
@cindex using tab stops in making tables
@cindex tables, indentation for
@kindex M-i
@findex tab-to-tab-stop
179 180 181
  For typing in tables, you can use @kbd{M-i} (@code{tab-to-tab-stop}).
This command inserts indentation before point, enough to reach the
next tab stop column.
Dave Love's avatar
#  
Dave Love committed
182 183 184 185 186

@findex edit-tab-stops
@findex edit-tab-stops-note-changes
@kindex C-c C-c @r{(Edit Tab Stops)}
@vindex tab-stop-list
187 188 189 190
  You can change the tab stops used by @kbd{M-i} and other indentation
commands, so that they need not be spaced every eight characters, or
even regularly spaced.  The tab stops are stored in the variable
@code{tab-stop-list}, as a list of column numbers in increasing order.
Dave Love's avatar
#  
Dave Love committed
191

192
  A convenient way to set the tab stops is with @kbd{M-x
Dave Love's avatar
#  
Dave Love committed
193 194 195
edit-tab-stops}, which creates and selects a buffer containing a
description of the tab stop settings.  You can edit this buffer to
specify different tab stops, and then type @kbd{C-c C-c} to make those
196 197 198 199 200
new tab stops take effect.  The buffer uses Overwrite mode
(@pxref{Minor Modes}).  @code{edit-tab-stops} records which buffer was
current when you invoked it, and stores the tab stops back in that
buffer; normally all buffers share the same tab stops and changing
them in one buffer affects all, but if you happen to make
Dave Love's avatar
#  
Dave Love committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
@code{tab-stop-list} local in one buffer then @code{edit-tab-stops} in
that buffer will edit the local settings.

  Here is what the text representing the tab stops looks like for ordinary
tab stops every eight columns.

@example
        :       :       :       :       :       :
0         1         2         3         4
0123456789012345678901234567890123456789012345678
To install changes, type C-c C-c
@end example

  The first line contains a colon at each tab stop.  The remaining lines
are present just to help you see where the colons are and know what to do.

217 218 219 220 221 222
  Note that the tab stops that control @code{tab-to-tab-stop} have
nothing to do with how tab characters are displayed in the buffer.
Tab characters are always displayed as empty spaces extending to the
next display tab stop, which occurs every @code{tab-width} columns
regardless of the contents of @code{tab-stop-list}.  @xref{Text
Display}.
Dave Love's avatar
#  
Dave Love committed
223 224 225 226 227

@node Just Spaces,, Tab Stops, Indentation
@section Tabs vs. Spaces

@vindex indent-tabs-mode
Richard M. Stallman's avatar
Richard M. Stallman committed
228 229 230 231 232 233
  Emacs normally uses both tabs and spaces to indent lines.  If you
prefer, all indentation can be made from spaces only.  To request
this, set @code{indent-tabs-mode} to @code{nil}.  This is a per-buffer
variable, so altering the variable affects only the current buffer,
but there is a default value which you can change as well.
@xref{Locals}.
Dave Love's avatar
#  
Dave Love committed
234

235
  A tab is not always displayed in the same way.  By default, tabs are
236 237 238
eight columns wide, but some people like to customize their editors to
use a different tab width (e.g., by changing the variable
@code{tab-width} in Emacs).  By using spaces only, you can make sure
239 240
that your file looks the same regardless of the tab width setting.

Dave Love's avatar
#  
Dave Love committed
241 242 243 244
@findex tabify
@findex untabify
  There are also commands to convert tabs to spaces or vice versa, always
preserving the columns of all nonblank text.  @kbd{M-x tabify} scans the
245
region for sequences of spaces, and converts sequences of at least two
Dave Love's avatar
#  
Dave Love committed
246 247
spaces to tabs if that can be done without changing indentation.  @kbd{M-x
untabify} changes all tabs in the region to appropriate numbers of spaces.
Miles Bader's avatar
Miles Bader committed
248 249 250 251

@ignore
   arch-tag: acc07de7-ae11-4ee8-a159-cb59c473f0fb
@end ignore