Convert Tab to 4 spaces in vim

Description

We usually use the “Tab” key for quick indention in code writing. However, the Tab key(tabulator key) is not equal to indention. Particularly in Python, using Tab for indention may cause complication error. Here is how to make the Tab key be equivalent to 4 spaces for indention in Python.

Solution

I use the vim editor to write code. Therefore, go to the ~/.vimrc configure file and add the following code:

set shiftwidth=4 smarttab
set expandtab

Then source ~/.vimrc to make it happen.
set shiftwidth=4 This sets the number of spaces for each level of indentation to 4. When you press the Tab key or use automatic indentation, Vim will insert or remove spaces in multiples of 4.
set smarttab When this option is set, Vim will use the shiftwidth value (in this case, 4) for shifting, but it will use the tabstop value for inserting actual tab characters. If smarttab is not set, the Tab key inserts spaces based on the shiftwidth value.
set expandtab This option is used to make Vim insert spaces instead of actual tab characters when you press the Tab key for indentation. When expandtab is set, Vim replaces tabs with spaces according to the shiftwidth value.
(From ChatGPT-3.5)

Expansion

The tab key Tab (abbreviation of tabulator key or tabular key) on a keyboard is used to advance the cursor to the next tab stop.
The word tab derives from the word tabulate, which means “to arrange data in a tabular, or table, form.” When a person wanted to type a table (of numbers or text) on a typewriter, there was a lot of time-consuming and repetitive use of the space bar and backspace key. To simplify this, a horizontal bar was placed in the mechanism called the tabulator rack(制表机架). Pressing the tab key would advance the carriage to the next tabulator stop. The original tabulator stops were adjustable clips(夹子) that could be arranged by the user on the tabulator rack. Fredric Hillard filed a patent application for such a mechanism in 1900.
(From Wikipedia: Tab key)