Home Command line toolsVim How to autoindent in vim

How to autoindent in vim

by The Linux Digest Guy
Vim logo

When you are working with a file type vim doesn’t recognize and doesn’t indent automatically, autoindent can be helpful. So on to how to autoindent in vim.

How do you autoindent? Autoindent tells vim to copy the indentation of the current line to a new line. This is enabled with “:set autoindent”. The command to turn it off is “:set noautoindent”. However, there are more efficient methods available.

Read on to better understand how vim handles automatic indentation.

Smart indentation is slightly smarter

Auto indent only copies the indentation of the current line and applies that to the next line you create. There is another option called smartindent. Smartindent will try to detect how the code should be indented by the style of the code. This option is mostly only helpful with C like programs.

To enable smartindent, all that is needed is to run the command:

:set smartindent

To disable smart indentation run the opposite command:

:set nosmartindent

For C code use cindent

If you are writing C code, cindent might be a better choice than smartindent. Cindent will insert the amount of indenting that is required by C indenting rules.

:set cindent

Vim handles indentation automatically for most popular file types.

Smartindent and autoindent are both very helpful features. Especially if the file type is not very common. But there is a better way. This better way is a feature that is enabled by default in many installations. This method is much more powerful and flexible than the methods mentioned above.

Vim has a built-in feature for file type detection. The file type detection will also enable automatic indentation for that file type if enabled. To reveal if file type detection is enabled and how it is set up, type in the command :filetype. This will display whether file type detection is on and also if that file types plugin is loaded and indentation is enabled.

If file type detection is not on, you can turn it on in your vimrc file. Your vimrc file should be either in your home directory as .vimrc or under /etc/vim/vimrc. set the following parameter in your vimrc file:

:set filetype on

To enable vim to load a plugin based on the file type that was detected, set the filetype plugin parameter in your vimrc:

:set filetype plugin on

You may also need to enable loading of the indent file for the particular file type. Do that like this:

:set filetype indent on

How file type detection works

File type detection uses several vim scripts that are stored in the vim runtime directory. A common location for this directory is /usr/share/vim/vim[xy]/ where [xy] is version number of vim.

The first of these scripts is filetype.vim. This script is run each time you open or create a new file. It will try to detect the type of file that is being opened by its file extension and in some cases the content of the file.

If a filetype is successfully detected, vim will look for a plugin for that file type. The plugins are located in a subdirectory named ftplugins/.

Likewise, if indentation is enabled, vim will load indentation files that are located in the subdirectory indent/.

Indent using spaces

Let’s not get into a long discussion on tabs vs. spaces. But suffice to say, many people prefer to use 2 or 4 spaces instead of a single tab. This can be a good way to ensure that the code is consistently formatted in what ever editor it is opened in. But, who would want to hit space 4 times instead of hitting the tab key once?

There is a simple solution to this, expandtab. Expandtab does exactly what it sounds like. It expands each tab to a certain number of spaces. To turn this option on, just type:

:set expandtab

And again to turn it of just add no in front of the command:

:set noexpandtab

Now, you just have to set how many spaces you want each tab to expand to. To do this, you will need two options, tabstop and shiftwidth. The value of tabstop will determine how many spaces are inserted for every tab. Shiftwidth will determine how many spaces make up one level of indentation. So if you want to auto indent 4 spaces instead of one tab enter the following in your vim session or put it in your vimrc file.

:set expandtab
:set tabstop=4
:set shiftwidth=4

You could also do all of this with just line of commands. For example, if you want to automatically auto indent by 2 spaces. You can enter all of the lines above like this:

:set tabstop=2 shiftwidth=2 expandtab


This has gone on a bit longer than you would expect for a simple option like autoindent. The reason being that autoindent is not always going to be the best option. If you are trying to find the best way to auto indent your code in vim, you are most likely going to want to use the indenting feature of vim’s file type detection.

Now, you should have all the information you need to choose the code formatting method that fits your workflow the best.


This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy