vim中的自适应制表 [英] Adaptive tabbing in vim
问题描述
我碰巧在代码中工作,其中某些模块使用制表符进行缩进,而其他模块则使用空格.许多文本编辑器(例如Np ++)都具有某种自适应制表符功能,如果前一行(或代码块)使用空格或制表符,则使用空格来缩进.
I happen to work on code in which some modules use tabs for indentation while others use spaces. Many text editors such as Np++ has some sort of an adaptive tabbing feature, which use spaces for indentation if the previous line (or block of code) use spaces, or tabs as the case may be.
我在Vim中还没有看到这样的东西.是否有任何此类插件或设置?
I haven't seen anything in vim like this. Is there any such plugin or setting for this?
推荐答案
我愿意设置环境,如以下示例所示.我制定了将制表符替换为空格的一般规则,并在需要覆盖该规则时使用augroup
. Makefile是一个很好的例子,说明何时可能需要TABS,而cpp文件是何时需要空格.
I perfer to set my enviroment up like the below example demonstrates. I make a general rule of replacing tabs with spaces and use augroup
when I need to override that rule. Makefiles are a good example of when you may require TABS and a cpp file is when you may require spaces.
" 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
这篇关于vim中的自适应制表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!