Replacing tabs by spaces in vi

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.

Jean-Luc started CNX Software in 2010 as a part-time endeavor, before quitting his job as a software engineering manager, and starting to write daily news, and reviews full time later in 2011.

Support CNX Software - Donate via PayPal, become a Patron on Patreon, or buy review samples
Notify of