Adaptive tabbing in vim

早过忘川 提交于 2019-12-05 04:09:51

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).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!