正如其他答案中指出的那样,现在首选的方法不是使用smartindent,而是在您的.vimrc
中使用以下内容:
filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
The help files take a bit of time to get used to, but the more you read, the better Vim gets:
:help smartindent
Even better, you can embed these settings in your source for portability:
:help auto-setting
To see your current settings:
:set all
As graywh points out in the comments, smartindent has been replaced by cindent which "Works more cleverly", although still mainly for languages with C-like syntax:
:help C-indenting
backspace
键时,wim 只会一次删除一个空格,这很烦人。有什么解决办法吗? - Fermat's Little Student如果您打开一个同时使用制表符和空格的文件,假设您已经
set expandtab ts=4 sw=4 ai
你可以使用以下方法将整个文件中的所有制表符替换为空格:
:%retab
获取特定文件类型的缩进最好的方法是在您的vimrc中使用filetype plugin indent on
。例如,您可以在.vim/ftplugin/c.vim中指定set sw=4 sts=4 et
,而无需将其应用于正在编辑的所有文件,并且其他非C类型语法也会正确缩进,甚至包括Lisp。
为了在大多数文件中使用4个空格的 tab,Makefile 中使用真正的 8 个字符宽度的 tab,以及在各种文件(包括 C/C++)中自动缩进,请将以下内容放入您的 ~/.vimrc
文件:
" Only do this part when compiled with support for autocommands.
if has("autocmd")
" Use filetype detection and file-based automatic indenting.
filetype plugin indent on
" Use actual tab chars in Makefiles.
autocmd FileType make set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab
endif
" For everything else, use a tab width of 4 space chars.
set tabstop=4 " The width of a TAB is set to 4.
" Still it is a \t. It is just that
" Vim will interpret it to be having
" a width of 4.
set shiftwidth=4 " Indents will have a width of 4.
set softtabstop=4 " Sets the number of columns for a TAB.
set expandtab " Expand TABs to spaces.
else
?在我看来,最后一行似乎在所有情况下都会覆盖特定于makefile的内容。 - lucidbrot在许多Linux系统(例如Ubuntu)上,默认情况下不存在.vimrc
文件,因此建议您首先创建该文件。
不要使用位于主目录中的.viminfo
文件。它用于不同的目的。
步骤1:进入您的主目录
cd ~
步骤2:创建文件
vim .vimrc
步骤3:添加上述配置
filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab
第三步:按下 Shift + ZZ 保存文件。
推荐的方式是使用基于文件类型的缩进,并仅在必要时使用smartindent和cindent。
将以下内容添加到您的.vimrc文件中:
set expandtab
set shiftwidth=2
set softtabstop=2
filetype plugin indent on
希望它能作为不同答案来帮助你。
编辑你的 ~/.vimrc 文件
$ vim ~/.vimrc
添加以下代码:
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
tabstop
、shiftwidth
和expandtab
的值,并保持正确的空格和制表符混合(最大数量的制表符后跟任何必要数量的空格)。:imap <Tab> ^T
:imap <S-Tab> ^D
这里的^T和^D是字面上的控制字符,可以通过Ctrl-VCtrl-T插入。
有了这个映射,您仍然可以使用Ctrl-VTab在缓冲区中输入字面上的制表符。请注意,即使:set expandtab
打开,如果这样做,您也会得到未扩展的制表符。
使用:set smarttab
也可以实现与<Tab>
映射类似的效果,它还会导致行首的退格键行为更加智能。
在smarttab
模式下,当Tab不在行首使用时,它没有特殊含义。这与我上面将Tab映射为Ctrl-T不同,因为在任何一行(插入模式下)使用Ctrl-T都会增加该行的缩进。
其他有用的映射可能包括:
:map <Tab> >
:map <S-Tab> <
shiftwidth
、tabstop
和expandtab
。
shiftwidth
参数控制缩进大小;如果您想要四个空格缩进,请使用:set shiftwidth=4
或缩写:set sw=4
。noexpandtab
是默认设置。使用:set expandtab
。这会导致您在缓冲区中键入的制表符字符扩展为空格,并且Vim管理的缩进只使用空格。expandtab
并且通过所有正确的Vim机制管理缩进时,tabstop
的值变得无关紧要。它控制文件中制表符出现的方式。如果您设置了set tabstop=8 expandtab
,然后使用Ctrl-VTab在文件中插入一个硬制表符,它将像往常一样对齐到下一个基于8列的制表符位置。<C-t>
可以使用该语法而不是字面值。最后,在正常模式下映射<Tab>
也会映射<C-i>
(它们是相同的字符),所以我不建议这样做(你会失去一个很棒的vim功能)。 - D. Ben Knoble