括号内的vim缩进括号

时间:2011-11-22 04:24:55

标签: c++ vim auto-indent

在vim(例如7.3)中,我如何使用/修改cindentsmartindent选项,或以其他方式扩充.vimrc,以便在开括号内自动缩进花括号与开头(之前的第一个“单词”(稍后定义)对齐?

fN选项似乎很有希望,但在开括号内部时,似乎被(N选项覆盖。来自:help cinoptions-values

fN    Place the first opening brace of a function or other block in
      column N.  This applies only for an opening brace that is not
      inside other braces and is at the start of the line.  What comes
      after the brace is put relative to this brace.  (default 0).

        cino=           cino=f.5s       cino=f1s
          func()          func()          func()
          {                 {                 {
              int foo;          int foo;          int foo;

当前行为:

func (// no closing ) 
      // (N behavior, here N=0      
      {    // (N behavior overrides fN ?
        int foo; // >N behavior, here N=2 

虽然我希望:

func (// no closing )   
      // (N behavior as before   
{ // desired behavior
  int foo; // >N behavior still works

我要求的是来自fN不同,因为fN与流行的缩进对齐,我想直接与任何C ++ nested-name-specifier对齐在开头(之前,如

code; f::g<T> (         instead of         code; f::g<T> (
      {                                    { 

如果没有nested-name-specifier,我希望它与(本身相匹配。也许匹配nested-name-specifier太复杂了,或者可能还有另一部分语法,这更适合这种情况。无论如何,对于我的典型用例,我认为如果{与最内层未封闭(左边的最大字符序列的第一个非空白字符对齐,我会感到满意,包括不包含任何分号或左括号}

顺便说一下,当我尝试在vim7.3中自动添加各种std::for_each(b,e,[]{});结构时,我就达到了这个目的。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

不确定任何{auto,smart,c}缩进功能是否可以指向您想要的任何内容。我编写了一个可能给出一些启发的映射:

inoremap ({ <esc>T<space>y0A<space>(<cr><esc>pVr<space>A{

缺点是您可能需要做一些比'T'更聪明的事情才能回到最后一个标识符的开头(您可以使用'?'和正则表达式),它会破坏您的默认寄存器,如果您的paren之前的标识符位于你必须做的行的开头'({'你自己。想法是跳回到标识符之前,复制到行的开头,将其粘贴到下一行,然后替换每个有空格的角色。

祝你好运!