Tab键== 4个空格并在Vim中的花括号后自动缩进
-
04-07-2019 - |
解决方案
正如下面几个答案所指出的,现在首选方法不是使用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
<击> 在您的 .vimrc:文件中:
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
帮助文件需要一些时间来习惯,但读的越多,Vim就越好:
:help smartindent
更好的是,您可以在源代码中嵌入这些设置以实现可移植性:
:help auto-setting
要查看您当前的设置:
:set all
正如 graywh 在评论中指出的那样,smartindent已经被cindent所取代,“cindent更加巧妙”,尽管仍主要用于具有类似C语法的语言:
:help C-indenting
击> <击> 撞击>
其他提示
相关的,如果您打开一个同时使用制表符和空格的文件,假设您已经
set expandtab ts=4 sw=4 ai
您可以使用
替换整个文件中包含空格的所有选项卡:%retab
获取特定于文件类型的缩进的最佳方法是在vimrc中使用上的 filetype插件缩进。然后你可以在.vim / ftplugin / c.vim中指定像
set sw = 4 sts = 4 et
之类的东西,例如,不必为正在编辑的所有文件和其他非C编译那些全局类型语法也会正确缩进(甚至是lisps)。
要在大多数文件中使用4个空格的选项卡,在Makefile中使用真正的8-wide tab char,并在各种文件(包括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.
在许多Linux系统上,如Ubuntu,默认情况下 .vimrc
文件不存在,因此建议您先创建它。
请勿使用主目录中存在的 .viminfo
文件。它用于不同的目的。
第1步:转到您的主目录
cd~
第2步:创建文件
vim .vimrc
步骤3:添加上述配置
filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab
步骤3:按 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
来自 VIM wiki :
:set tabstop=4
:set shiftwidth=4
:set expandtab
自动缩进基于当前语法模式。我知道如果您正在编辑Foo.java,那么输入 {
并按 Enter 缩进以下行。
对于标签,有两种设置。在Vim中,键入一个冒号,然后“set tabstop = 4”。这将设置选项卡显示为四个空格。再次点击冒号并输入“set expandtab”;这将为标签插入空格。
您可以将这些设置放在主目录中的.vimrc(或Windows上的_vimrc)中,这样您只需键入一次。