Controlling indention
The source code for a program differs from ordinary text in a number of ways. One of the most important of these is the way in which source code uses indention. Indention shows the logical structure of the program: the way in which statements are grouped into blocks.
:set autoindent
(Don't forget to press<Enter>after
you have typed this.) This command turns on an option supplied primarily
to control indention when entering source code. Each line is automatically
indented the same distance as the previous one. As a programmer,
you will find this saves you quite a bit of work getting the indention
right, especially when you have several levels of indention.- Type <EscChar-T> at the start of a line to indent it in one level.
- Type <EscChar-D> at the start of a line to indent it out one level.
Try using the autoindent option when you are entering source code. It simplifies the job of getting indention correct. It can even sometimes help you avoid bugs; for example, in C source code, you usually need one closing } for every level of indention you go backwards.
- >>
- Shifts a line right 8 spaces (that is, adds 8 spaces of indention)
- <<
- Shifts a line left 8 spaces (that is, removes 8 spaces of indention)
You can shift a number of lines by typing the number followed by >> or <<. For example, typing 5>> will indent five lines, including the line the cursor is on.
:set shiftwidth=4
Tip: It is convenient to have a shiftwidth that is the same size as the width between tab stops.