To enable automatic indenting of source code add the following line to your .vimrc file
set autoindent
Indenting the whole file
Ever copied and pasted source code in vi and dreaded the idea of having to go through each line and using the tab key, well you don’t have to because it can be done for you automatically with a few key strokes.
press ‘esc’ to go into command mode
then type `gg` (g key twice) to place the cursor at the start of the file
then type `=G` (equals key followed by uppercase g) this is the magic
thats all, you will see the code being indented right before your eyes.
You can also indent part of the file by placing the cursor from where you want the indenting to start and typing `=G`
Indenting a single line
To indent a single line place the cursor on the line you want to indent and press `CTRL+T`. You must be in insert mode for this to work.
Copy and paste float-right problem
When copying and pasting source code with the autoindent feature enabled you will find that each line which vi would indent floats further to the right than the previous line.
To fix this you need to go into command mode and type the following
:set noautoindent
This will temporarily disable the autoindent feature. You can then copy and paste your code (paste your code in insert mode or else you might motice that the first letter or two are missing) and enable the autoindent feature again by typing
:set autoindent