vim c语言格式缩进_如何使Vim正确缩进C ++ 11 lambda?

vim c语言格式缩进

Vim seems not indent C++11 lambas very well. How to make Vim indent C++11 lambdas correctly?

Vim似乎不太适合缩进C ++ 11 lambas。 如何使Vim正确缩进C ++ 11 lambda?

For this following program, Vim indents it as

对于以下程序,Vim将其缩进为

#include 
#include 
#include 
#include 

int main ()
{
  std::vector strs({"one", "two"});
  std::vector newstrs;

  std::transform(strs.begin(), strs.end(), std::back_inserter(newstrs), [](const std::string& s) -> std::string {
      if (s == "one") {
      return "1";
      } else if (s == "two") {
      return "2";
      } else {
      return s;
      }
      });

  return 0;
}

The C++ lambda is indented incorrectly by Vim. The lambda function should be indented like

Vim错误地缩进了C ++ lambda 。 lambda函数应该像

#include 
#include 
#include 
#include 

int main ()
{
  std::vector strs({"one", "two"});
  std::vector newstrs;

  std::transform(strs.begin(), strs.end(), std::back_inserter(newstrs), [](const std::string& s) -> std::string {
    if (s == "one") {
      return "1";
    } else if (s == "two") {
      return "2";
    } else {
      return s;
    }
  });

  return 0;
}

You may add these lines to your vimrc for C/C++ to indent lambdas correctly as you listed above

您可以将这些行添加到C / C ++的vimrc中,以正确缩进lambda,如上所列

setlocal cindent
" handle lambda correctly
setlocal cino=j1,(0,ws,Ws

The key is setting cino (cinoption) to be j1,(0,ws,Ws. jN is new in Vim 7.4 for “indent Java anonymous classes correctly” which improves C/C++ lambda indenting too.

关键是将cino (cinoption)设置为j1,(0,ws,Ws jN是Vim 7.4中的新功能,用于“正确缩进Java匿名类”,这也改善了C / C ++ lambda缩进。

Check my vimrc as an example.

以我的vimrc为例。

Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。

翻译自: https://www.systutorials.com/how-to-make-vim-indent-c11-lambdas-correctly/

vim c语言格式缩进

你可能感兴趣的:(vim c语言格式缩进_如何使Vim正确缩进C ++ 11 lambda?)