With line numbers and syntax highlighting, our vim editor is already looking like an IDE. Let’s improve it further by adding the auto-indenting capability. To turn the autoindent on, enter the following in command mode:
You guessed it, turning it off would be done like so:
Now vim will auto indent your source code according to the scope of opening and closing braces or matching pair of delimiters. To make autoindent permanent, add the following line to the .vimrc file.
You can further customize the tab width and space of indentation. The expandtab option causes Vim to insert with spaces instead of tabs. The exact number of spaces can be set using softtabstop. The shiftwidth should also be set because this controls how many spaces will be used with autoindent.
For example, Python requires four spaces to define the scope. Accordingly we need to set the indentation to make vim autoindent properly. The .vimrc looks as follows:
set expandtab set shiftwidth=4 set softtabstop=4