Vim:将连续线与空格对齐

Jac*_*ieg 1 c++ vi vim coding-style

我想用制表符缩进 vim 中的所有内容,但特殊情况除外。例如,我有这个 C++ 代码(其中<tab>是制表符系列,<s>是空格字符系列):

<tab>if(true &&
<tab><s>true)
<tab>{
<tab><tab>//code here
<tab>}
Run Code Online (Sandbox Code Playgroud)

我想在写完 '&&' 并按 'o' 跳到下一行并开始编写以使 vim 放置一个制表符和空格数,直到 '(' 从前一行开始。

是否可以在 vim 中定义这种编码风格?

谢谢!

Joh*_*ter 5

我想你要找的是什么(N的选项cinoptions。试试set cinoptions+=(0。根据文档,这看起来像您寻求的对齐方式。

通过使用 help 命令可以找到更多信息::help cinoptions-values或查看cinoptions-values 帮助的在线版本。

就标签而言,您需要禁用expandtabwith :set noexpandtab,并且您需要确保您的制表位、软制表位和 shiftwidth 都进行了相应的设置。举个例子,Linux 源代码使用了你上面提到的风格,我的 vimrc 中有这个:

setlocal ts=8 sts=8 sw=8 tw=80

" Don't expand tabs to spaces.
setlocal noexpandtab

" Enable automatic C program indenting.
setlocal cindent

" Don't outdent function return types.
setlocal cinoptions+=t0

" No extra indentation for case labels.
setlocal cinoptions+=:0

" No extra indentation for "public", "protected", "private" labels.
setlocal cinoptions+=g0

" Line up function args.
setlocal cinoptions+=(0

" Setup formatoptions:
"   c - auto-wrap comments to textwidth.
"   r - automatically insert comment leader when pressing <Enter>.
"   o - automatically insert comment leader after 'o' or 'O'.
"   q - allow formatting of comments with 'gq'.
"   l - long lines are not broken in insert mode.
"   n - recognize numbered lists.
"   t - autowrap using textwidth,
setlocal formatoptions=croqlnt
Run Code Online (Sandbox Code Playgroud)