vim中的自适应标签

时间:2011-08-05 00:10:36

标签: vim indentation

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

我没有在vim中看到过这样的东西。是否有这样的插件或设置?

4 个答案:

答案 0 :(得分:4)

我更喜欢设置我的环境,就像下面的例子所示。我制定了使用空格替换制表符的一般规则,并在需要覆盖该规则时使用augroup。 Makefiles是一个很好的例子,当你可能需要TABS时,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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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

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

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

'smartindent'和'cindent'可能会干扰基于文件类型的缩进,并且永远不应与其一起使用。

如果您正在编辑特定文件并且想要阻止该文件中的自动缩进,请输入:

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

答案 3 :(得分:0)

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

您还希望通过autoindentsmartindent设置自动标签设置。不确定,您可能需要在设置copyindent后重置smartindentautoindent才能使其正常工作(例如,再次执行:set nosmartindent然后:set smartindent。)