Set Tab to 4 spaces in Vim
It is not a problem to use tab or space for indentation, just like using four spaces in Python. This depends on your preferences. In Vim, you can easily set tabs or spaces based on different file types, and set the length to be flexible.
First, let's take a look at how to set the tab width and determine whether to use a tab or space to represent an indent:
set tabstop=4set softtabstop=4set shiftwidth=4set noexpandtab / expandtab
Note:
WheretabstopThe length of spaces displayed on a tab. The default value is 8.
softtabstopIndicates that the length of the indent is returned when you press the backspace key in the editing mode.expandtabIs particularly useful.
shiftwidthIndicates the length of each level of indentation, which is generally setsofttabstopSame.
When setexpandtabThe indentation is represented by spaces,noexpandtabA tab is used to represent an indent.
Set the tab Based on the file type:
Sometimes we want to set different tab representations for some types of files. For example, python uses four spaces to represent an indent. In our javascript conventions, we also use four spaces to represent indentation, HTML and CSS prefer to use tab labels for indentation, so you can make the following settings:
if has("autocmd") autocmd FileType javascript setlocal ts=4 sts=4 sw=4 expandtab autocmd FileType python setlocal ts=4 sts=4 sw=4 expandtabendif
In this way, when the opened file is.js .py, Will be indented with four spaces.
Use special symbols to represent tab characters:
In Vim, you can use special symbols to represent a tab. In this way, tabs and spaces can be easily distinguished. For example:
You can add it to vimrc:
set listset listchars=tab:▸\ ,eol:¬
Build VIM into a simple and practical IDE
Vim Learning Guide
Quick learn Vi Editor
Powerful Vim Editor
Build a Vim Development Environment on CentOS 6.2
Install the highlighted Vim editing tool in CentOS 5.4
Vim tips: C language settings
Set the Vim row number in Ubuntu
Vim editor basic tutorial
This article permanently updates the link address: