vim中的自适应选项卡



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

我在vim中从未见过这样的东西。有这样的插件或设置吗?

我更喜欢像下面的例子那样设置我的环境。我制定了用空格替换制表符的一般规则,并在需要覆盖该规则时使用augroup。makefile是一个很好的例子,说明何时需要制表符,而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

这个插件似乎实现了你的目标。IndentConsistencyCop

你应该安装免费的插件加载适当的自动命令。IndentConsistencyCopAutoCmds

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

'smartindent'在某些情况下会自动插入一层额外的缩进,并且适用于类c文件。

'cindent'更可定制,但在语法方面也更严格。

'smartindent'和'cindent'可能会干扰基于文件类型的缩进,并且不应该与它结合使用。

如果您正在编辑一个特定的文件,并且希望在该文件中防止自动缩进,请输入:

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

我不认为Vim中有什么是你想要的。但你可能想看看copyindent。参见:h copyindent。它提供了"自适应选项卡",但不完全是你想要的。新行开头的制表符/空格将复制前一行的制表符/空格。但是,如果增加缩进,则决定是否添加制表符或空格将取决于expandtab设置。(您可能还想看看preserveindent选项的帮助,我认为它也应该在您的场景中设置。)

您还需要通过autoindentsmartindent进行自动选项卡设置。不确定,您可能需要重置smartindentautoindent设置copyindent后,使其正常工作(例如,做:set nosmartindent,然后再做:set smartindent)。

最新更新