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.
I haven't seen anything in vim like this. Is there any such plugin or setting for this?
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
This plugin seems to accomplish your goals. IndentConsistencyCop
You should install the complimentary plugin which loads the appropriate autocommands. IndentConsistencyCopAutoCmds
As @zkhr said you can use smartindent
or autoindent
. You can also use cindent
which is the default indentation used by vim while editing C/C++ files.
'smartindent' automatically inserts one extra level of indentation in some cases, and works for C-like files.
'cindent' is more customizable, but also more strict when it comes to syntax.
'smartindent' and 'cindent' might interfere with file type based indentation, and should never be used in conjunction with it.
If you are editing a particular file and you want to prevent auto indenting within that file, enter:
:setlocal noautoindent
:setlocal nocindent
:setlocal nosmartindent
:setlocal indentexpr=
I don't think there's anything in Vim that's exactly what you want. But you may want to check out copyindent
. See :h copyindent
. It gives "adaptive tabbing" but not quite what you wanted. The leading tabs/spaces on a new line will copy that of the previous line. However, if you increase the indent the decision over whether tabs or spaces are added will depend on the expandtab
setting. (You may also want to take a look at help for preserveindent
option, which should also be set in your scenario, I think.)
You will also want to have the automatic tabbing setting, via either autoindent
or smartindent
. Not sure, you may have to reset smartindent
or autoindent
after setting copyindent to get it to work properly (e.g., do :set nosmartindent
then :set smartindent
again).
来源:https://stackoverflow.com/questions/6950063/adaptive-tabbing-in-vim