Home » Software Development » Editor » Vim » How to set TAB key to 4 Spaces in VIM ?

How to set TAB key to 4 Spaces in VIM ?

If you are using Ubuntu and installed VIM using the steps as mentioned in “How to Install Vim Text Editor in Linux ?” by default, when you start writing the source code, the TAB key is assigned with 8 spaces.

Source code written with indentation of Tab with 8 spaces looks fine till the source code is small and as the number of loops inside the source code goes on increasing, the 8 space TAB written code starts overflowing from one line to next and overall the code indentation doesn’t look good.

The Solution to this, is to “Change the TAB Key to 4 Spaces” instead of “default 8 spaces” this can be done

Permanently by creating vim configuration file as,

$ vim ~/.vimrc

and add,

set tabstop=4

Now, save the ~/.vimrc file and open your source code and start writing.. Now if you enter TAB, you will see indentation is done only with 4 spaces.

Temporary by entering the command in your source code

Lets say you are writing your source code in “helloworld.c” and have this file opened using vim as,

$ vim helloworld.c

The type this command as,

:set tabstop=4

This will change the TAB key to 4 spaces only for “helloworld.c” and once you close and reopen same file, TAB will be set to default 8 spaces.


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment