vim中的自适应tabing

vim中的自适应tabing,vim,indentation,Vim,Indentation,我碰巧在编写代码,其中一些模块使用制表符进行缩进,而另一些模块使用空格。许多文本编辑器(如Np++)具有某种自适应制表符功能,如果前一行(或代码块)使用空格或制表符(视情况而定),则会使用空格进行缩进 我在vim里没见过这样的东西。有这样的插件或设置吗?正如@zkhr所说,您可以使用smartindent或autoindent。您还可以使用cindent,这是vim在编辑C/C++文件时使用的默认缩进 “smartindent”在某些情况下会自动插入一个额外级别的缩进,并适用于类似C的文件 “c

我碰巧在编写代码,其中一些模块使用制表符进行缩进,而另一些模块使用空格。许多文本编辑器(如Np++)具有某种自适应制表符功能,如果前一行(或代码块)使用空格或制表符(视情况而定),则会使用空格进行缩进


我在vim里没见过这样的东西。有这样的插件或设置吗?

正如@zkhr所说,您可以使用
smartindent
autoindent
。您还可以使用
cindent
,这是vim在编辑C/C++文件时使用的默认缩进

“smartindent”在某些情况下会自动插入一个额外级别的缩进,并适用于类似C的文件

“cindent”更易于定制,但在语法方面也更严格

“smartindent”和“cindent”可能会干扰基于文件类型的缩进,因此不应与之结合使用

如果要编辑特定文件,并且要防止该文件内自动缩进,请输入:

:setlocal noautoindent
:setlocal nocindent
:setlocal nosmartindent
:setlocal indentexpr=

我喜欢按照下面的例子来设置我的环境。我制定了用空格替换制表符的一般规则,并在需要覆盖该规则时使用
augroup
。Makefiles是一个很好的例子,说明您可能需要制表符,而cpp文件则说明您可能需要空格

" A tab produces a 4-space indentation
:set softtabstop=4
:set shiftwidth=4
:set expandtab
" replace tabs with spaces unless noted otherwise

" <snip>

augroup CPPprog
   au!
   "-----------------------------------
   " GENERAL SETTINGS
   "-----------------------------------
   au BufRead,BufNewFile,BufEnter             *.cpp,*.c,*.h,*.hpp   set nolisp
   au BufRead,BufNewFile,BufEnter             *.cpp,*.c,*.h,*.hpp   set filetype=cpp
   au FileType                                *                     set nocindent smartindent
   au FileType                                *.c,*.cpp             set cindent
   au BufRead,BufNewFile,BufEnter             *.cpp                 let g:qt_syntax=1
   " turn on qt syntax highlighting (a plugin)
   au BufNewFile,BufRead,BufEnter             *.c,*.h,*.cpp,*.hpp   let c_space_errors=1
   " trailing white space and spaces before a <Tab>

   " <snip>

augroup END

" <snip>

augroup filetype
  au! BufRead,BufNewFile,BufEnter *Makefile*,*makefile*,*.mk set filetype=make
augroup END
" In Makefiles, don't expand tabs to spaces, since we need the actual tabs
autocmd FileType make set noexpandtab
“选项卡产生4个空格的缩进
:设置softtabstop=4
:设置shiftwidth=4
:设置展开选项卡
“除非另有说明,否则将制表符替换为空格
" 
augroupcpprog
哎呀!
"-----------------------------------
“一般设置
"-----------------------------------
au BufRead、BufNewFile、BufEnter*.cpp、*.c、*.h、*.hpp机组nolisp
au BufRead、BufNewFile、BufEnter*.cpp、*.c、*.h、*.hpp集文件类型=cpp
au文件类型*设置nocindent smartindent
au文件类型*.c,*.cpp集cindent
au BufRead,BufNewFile,BufEnter*.cpp let g:qt_syntax=1
“打开qt语法高亮显示(插件)
au BufNewFile、BufRead、BufEnter*.c、*.h、*.cpp、*.hpp让c_空间_误差=1
“尾随空格和
" 
螺旋端
" 
augroup文件类型
哎呀!BufRead,BufNewFile,BufEnter*Makefile*,*Makefile*,*.mk set filetype=make
螺旋端
“在Makefiles中,不要将选项卡扩展到空格,因为我们需要实际的选项卡
autocmd文件类型设置为noexpandtab

我不认为Vim中有任何东西正是您想要的。但是您可能想查看
copyindt
。请参阅
:h copyindt
。它提供了“自适应选项卡”“但不是你想要的。新行的前导制表符/空格将复制上一行的制表符/空格。但是,如果增加缩进,是否添加制表符或空格的决定将取决于
expandtab
设置。(您可能还想查看
preserveindent
选项的帮助,我想这也应该在您的场景中设置。)


您还需要通过
autoindent
smartindent
进行自动制表设置。不确定,您可能需要在设置copyindent后重置
smartindent
autoindent
,以使其正常工作(例如,执行
:设置nosmartindent
,然后
:再次设置smartindent

此插件似乎可以实现您的目标

您应该安装免费插件,加载相应的自动命令