Setting Autoindent in Vi(m)

vim autoindent

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:

:set autoindent

You guessed it, turning it off would be done like so:

:set autoindent!

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.

set autoindent

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

Use Vim

About Deepak Devanand

Seeker of knowledge
This entry was posted in Linux, vi and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s