Replacing tabs by spaces in vi

Orange Pi Development Boards

In order to have code formatting consistency, we may choose to use only tabs or only spaces in the source code.

We chose to only use spaces since that makes sure the code will be properly formatted in any text editor.

In order to create 4 spaces while pressing tabs in vi, edit your vi settings:

vi ~/.vimrc

and add the 3 lines below to your config file

set tabstop=4
set shiftwidth=4
set expandtab

The next time you’ll use vi, pressing the tabulation key will create 4 spaces.

However, in some cases, e.g. Makefile, you may still need to use real tabs. Just press Ctrl+V then tab to create a real tab.

Leave a Reply

Your email address will not be published. Required fields are marked *