Wednesday, March 7, 2012

Dealing with Tabs in vim

To insert space characters whenever the tab key is pressed, set the 'expandtab' option:
:set expandtab

With this option set, if you want to enter a real tab character use Ctrl-V<tab> key sequence.

To control the number of space characters that will be inserted when the tab key is pressed, set the 'tabstop' option. For example, to insert 4 spaces for a tab, use:

:set tabstop=4

After the 'expandtab' option is set, all the new tab characters entered will be changed to spaces. This will not affect the existing tab characters. To change all the existing tab characters to match the current tab settings, use:
:retab

To change the number of space characters inserted for indentation, use the 'shiftwidth' option:
:set shiftwidth=4

For example, to get the following coding style,
*No tabs in the source file.
*All tab characters are 4 space characters.

use the following set of options:
:set tabstop=4
:set shiftwidth=4
:set expandtab

Add the above settings to your .vimrc.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.