vim 缩进括号内的大括号

发布于 2024-12-17 03:58:10 字数 1849 浏览 0 评论 0原文

在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 太复杂,或者可能还有语法这更适合这种情况。无论如何,对于我的典型用例,我认为如果 { 与最里面未闭合 (,包含在内,不包含任何分号或左花括号 }

顺便说一句,我在尝试自动缩进各种内容时得出了这一点 。 std::for_each(b,e,[]{}); 在 vi​​m7.3 中构造,感谢您的帮助!

In vim (eg 7.3), how can I use/modify the cindent or smartindent options, or otherwise augment my .vimrc, in order to automatically indent curly braces inside open parentheses to align to the first "word" (defined later) directly preceding the opening (?

The fN option seems promising, but appears to be overridden by the (N option when inside open parentheses. From :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;

Current behavior:

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

while I wish for:

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

What I am asking for is different from fN because fN aligns to the prevailing indent, and I want to align to any C++ nested-name-specifier that directly precedes the opening (, like

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

If there is no nested-name-specifier, I'd like it to match the ( itself. Perhaps matching a nested-name-specifier is too complicated, or maybe there is another part of the grammer this is more appropriate for this scenario. Anyway, for my typical use case, I think I'd be satisfied if the { aligns with the first nonwhitespace character of the maximal sequence of characters to the left of the innermost unclosed (, inclusive, that does not contain any semicolons or left curly braces }.

By the way, I arrived at this when trying to autoindent various std::for_each(b,e,[]{}); constructs in vim7.3. Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫卆 2024-12-24 03:58:10

不确定是否可以欺骗任何 {auto,smart,c}indent 功能来完成您想要的操作。我制作了一个可能会带来一些启发的映射:

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

缺点是您可能需要做一些比“T”更聪明的事情才能回到最后一个标识符的开头(您可以将“?”与正则表达式一起使用),它会浪费你的默认寄存器,如果括号之前的标识符位于行的开头,你必须自己执行“({”。这个概念是跳回到标识符之前,复制到行的开头,粘贴到下一行,并将每个字符替换为空格,

祝你好运!

Not sure that any of the {auto,smart,c}indent features could be finagled to do what you want. I made up a mapping which might give some inspiration:

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

Downsides are that you may need to do something smarter than 'T' to get back to the beginning of the last identifier (you could use '?' with a regex), that it trashes your default register, and that if your identifier before the paren is at the start of the line you have to do '({' yourself. The notion is to jump back to just before the identifier, copy to the beginning of the line, paste that to the next line, and replace every character with a space.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文