done so that
preprocessor directives will all start in column 1. The indentation is
restored for the next line.
CORRECTING INDENTS
When you are using 'autoindent' or 'smartindent' to get the indent of the
previous line, there will be many times when you need to add or remove one
'shiftwidth' worth of indent. A quick way to do this is using the CTRL-D and
CTRL-T commands in Insert mode.
For example, you are typing a shell script that is supposed to look like
this:
if test -n a; then ~
echo a ~
echo "-------" ~
fi ~
Start off by setting these options: >
:set autoindent shiftwidth=3
You start by typing the first line, <Enter> and the start of the second line:
if test -n a; then ~
echo ~
Now you see that you need an extra indent. Type CTRL-T. The result:
if test -n a; then ~
echo ~
The CTRL-T command, in Insert mode, adds one 'shiftwidth' to the indent, no
matter where in the line you are.
You continue typing the second line, <Enter> and the third line. This time
the indent is OK. Then <Enter> and the last line. Now you have this:
if test -n a; then ~
echo a ~
echo "-------" ~
fi ~
To remove the superfluous indent in the last line press CTRL-D. This deletes
one 'shiftwidth' worth of indent, no matter where you are in the line.
When you are in Normal mode, you can use the ">>" and "<<" commands to
shift lines. ">" and "<" are operators, thus you have the usual three ways to
specify the lines you want to indent. A useful combination is: >
>i{
This adds one indent to the current block of lines, inside {}. The { and }
lines themselves are left unmodified. ">a{" includes them. In this example
the cursor is on "printf":
original text after ">i{" after ">a{"
if (flag) if (flag) if (flag) ~
{ { { ~
printf("yes"); printf("yes"); printf("yes"); ~
flag = 0; flag = 0; flag = 0; ~
} } } ~
==============================================================================
*30.5* Tabs and spaces
A QUICK HISTORY OF THE RATIONALE BEHIND TABS
`vi` (the ancestor of Vim) was created by Bill Joy. At the time, he was using
a PDP-11 with limited memory and I/O operation capabilities. Back then, it
was common to optimize the size of source code with the following trick.
The ASCII table was first designed to remotely control teleprinters. When
control character 9 (the Horizontal Tab, caret notation: ^I) was sent to a
teleprinter, it would move the carriage to the next tab stop. Assuming tab
stops were separated by 8 columns (a typical standard), this means that a
single control character could produce the same visual effect as up to 8 space
characters. For example, the following two lines will display identically >
1234^I9
1234 9
Using the <Tab> key was also faster than typing <Space> several times; the
same was true for <BS>.
THE ISSUE WITH TABS AND INDENTATION
In Vim, the number of columns between two (virtual) horizontal tab stops
is controlled by 'tabstop' and is set to eight by default. Although you can
change it, you quickly run into trouble later. Other programs won't know what
tabstop value you used. They probably use the default value of eight, and
your text suddenly looks very different. Also, most printers use a fixed
tabstop value of eight. Thus it's best to keep 'tabstop' alone; if you edit a
file which was written with a different tabstop setting, see |25.3| for how
to fix that.
For indenting lines in a program, using a multiple of eight columns makes
you quickly run into the right border of the window. Using a single space
doesn't provide enough visual difference. Many people prefer to use four
spaces, a good compromise.
Since a tab character at the beginning of a line is visually represented
as eight spaces and you want to use an indent of four spaces, you can't use a
tab character to make your indent.
To remedy this, `vi` had the 'shiftwidth' option. When set to 4, on a new
line, pressing <C-t>