:set autoindent
Now, when you indent a line with spaces or tabs, the
following lines will automatically be indented by the same
amount. When you press
RETURN
after typing the first indented
line, the cursor goes to the next line and automatically
indents the same distance as the previous line.
As a programmer, you will find this saves you quite a bit of work
getting the indentation right, especially when you have several
levels of indentation.
When you are entering code with autoindent enabled, typing
CTRL-T
at the start of a line gives you another level of indentation and
typing
CTRL-D
takes one away.
We should point out that
CTRL-T
and
CTRL-D
are typed while you are in insert mode, unlike most other commands,
which are typed in command mode.
Try using the autoindent option when you are entering
source code. It simplifies the job of getting indentation
correct. It can even sometimes help you avoid bugs (e.g., in C
source code, where you usually need one closing curly brace
(}) for every
level of indentation you go backwards).
The << and >>
commands are also helpful when indenting
source code.
By default, >> shifts a line right eight spaces
(i.e., adds eight spaces of indentation)
and << shifts a line left eight spaces.
For example, move the cursor to the beginning of a line and
press the > key twice (>>).
You will see the line move right.
If you now press the <
key twice (<<), the line will move back again.
You can shift a number of lines by typing the number followed
by >> or <<.
For example, move the cursor to the first line of
a good-size paragraph and type 5>>.
You will shift the first five lines in
the paragraph.
The default shift is eight spaces (right or left). This default
can be changed with a command like:
:set shiftwidth=4
You will find it convenient to have a shiftwidth that is the
same size as the width between tab stops.
vi attempts to be smart when doing indenting.
Usually, when you see text indented by eight spaces at a time,
vi will actually insert tab characters into
the file, since tabs usually expand to eight spaces.
This is the UNIX default; it is most noticable when you type
a tab during normal input, and when files are sent to a
printer—UNIX expands them with a tab stop of eight spaces.
If you wish, you can change how vi represents
tabs on your screen, by changing the tabstop
option. For example, if you have something that is deeply indented,
you might wish to have use a tab stop setting of every four
characters, so that the lines will not wrap.
The following command will make this change:
:set tabstop=4
NOTE:
Changing your tab stops is not recommended. Although vi
will display the file using an arbitrary tabstop setting, the tab characters
in your files will still be expanded using an eight-character tab
stop by every other UNIX program.
Eight-character tab stops are one of the facts of life on UNIX,
and you should just get used to them.
Sometimes indentation won't work the way you expect, because
what you believe to be a tab character is actually one or more
spaces.
Normally, your screen displays both a tab and a space
as whitespace, making the two indistinguishable.
You can, however, issue the command:
:set list
This alters your display so that a tab appears as the control
character ^I and an end-of-line appears as a $.
This way, you can spot a true space, and you can see extra spaces
at the end of a line. A temporary equivalent is the
:l command.
For example, the command:
:5,20 l
displays lines 5 through 20,
showing tab characters and end-of-line characters.