Md Toy Blog

Vim tabs to spaces


When you are given a file with tabs for indentation you can :retab and it will convert tabs to spaces*.

*Important : it convert the tabs in a file to spaces (if you have :set expandtab directive before) or to tabs (if you have the default :set noexpandtab).

I have :set expandtab, How to control number of spaces for each tab ?

You need to have set :set expandtab. Then you can control how many spaces a tab should be replaced with, using:

:set expandtab
:set tabstop=x


usually x is 4 or 2 (:set tabstop=4).

How many spaces for indentation

In PHP PSR-4 standard it is 4. In javascript some use 2. But 4 is easier on the eyes, so I stick to 4. The rationale behind using 2 is that, it is the first number to allow distinguishing tabs from spaces, and in javascript you could write very deep nested functions. The latter is not so true anymore with import. So stick to 4.

How to control the number of spaces when using >> for indentation

Once you have :set expandtab set up, and you try to use >> or << for indentation, you need to have the matching:

:set shiftwidth=4