我目前的设置是8个空格;我该如何重新定义它呢?


当前回答

确保vartabstop未设置

set vartabstop=

设置tabstop为4

set tabstop=4

其他回答

确保vartabstop未设置

set vartabstop=

设置tabstop为4

set tabstop=4

将您想要的设置放在~/。vimrc文件——请参阅下面的一些指导方针和最佳实践。

在Vim中使用标签有四种主要方式:

Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4 (or 3 or whatever you prefer) and use 'noexpandtab'. Then Vim will use a mix of tabs and spaces, but typing and will behave like a tab appears every 4 (or 3) characters. Note: Setting 'tabstop' to any other value than 8 can make your file appear wrong in many places (e.g., when printing it). Set 'tabstop' and 'shiftwidth' to whatever you prefer and use 'expandtab'. This way you will always insert spaces. The formatting will never be messed up when 'tabstop' is changed. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a |modeline| to set these values when editing the file again. Only works when using Vim to edit the file. Always set 'tabstop' and 'shiftwidth' to the same value, and 'noexpandtab'. This should then work (for initial indents only) for any tabstop setting that people use. It might be nice to have tabs after the first non-blank inserted as spaces if you do this though. Otherwise aligned comments will be wrong when 'tabstop' ischanged.

来源:

vimdoc.sourceforge.net/htmldoc/options.html #“制表符” :帮助制表符

还有一件事,使用 : retab 将现有制表符转换为空格 http://vim.wikia.com/wiki/Converting_tabs_to_spaces

这取决于你的意思。你的意思是:

您希望文件中的制表符显示为4个字符单元格宽? 您希望TAB键生成由4个空格字符组成的缩进

根据您需要的行为,以下设置之一应该工作:

如果你想要文件中的制表符显示为4个字符单元格宽:

set tabstop=4

如果你的代码需要使用实际的制表符,这些设置可以防止无意插入空格(这些是默认值,但你可能想要防御性地设置它们):

set softtabstop=0 noexpandtab

如果你还想使用制表符进行缩进,你还应该将shiftwidth设置为与tabstop相同:

set shiftwidth=4

要使这些设置永久存在,请将它们添加到vimrc中。

如果你想按tab键缩进4个空格字符:

首先,告诉vim使用4空格缩进,并智能地使用tab键进行缩进,而不是插入制表符(当在一行的开头时):

set shiftwidth=4 smarttab

如果你也想让vim只使用空格字符,而不是制表符:

set expandtab

最后,我还建议设置制表位与缩进宽度不同,以减少制表符伪装成适当缩进的机会:

set tabstop=8 softtabstop=0

要使这些设置永久存在,请将它们添加到vimrc中。

更多的细节

如果你需要做出调整,或者只是想了解这些选项的含义,下面是每个选项的含义:

tabstop The width of a hard tabstop measured in "spaces" -- effectively the (maximum) width of an actual tab character. shiftwidth The size of an "indent". It's also measured in spaces, so if your code base indents with tab characters then you want shiftwidth to equal the number of tab characters times tabstop. This is also used by things like the =, > and < commands. softtabstop Setting this to a non-zero value other than tabstop will make the tab key (in insert mode) insert a combination of spaces (and possibly tabs) to simulate tab stops at this width. expandtab Enabling this will make the tab key (in insert mode) insert spaces instead of tab characters. This also affects the behavior of the retab command. smarttab Enabling this will make the tab key (in insert mode) insert spaces or tabs to go to the next indent of the next tabstop when the cursor is at the beginning of a line (i.e. the only preceding characters are whitespace).

更多细节请参见:help 'optionname' in vim(例如:help 'tabstop')

添加一行 将ts = 4 在 ~ /。每个用户的Vimrc文件 或 /etc/vimrc文件用于整个系统