vim 缩进括号内的大括号
在vim(例如7.3)中,我如何使用/修改cindent
或smartindent
选项,或以其他方式增强我的.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,[]{});
在 vim7.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定是否可以欺骗任何 {auto,smart,c}indent 功能来完成您想要的操作。我制作了一个可能会带来一些启发的映射:
缺点是您可能需要做一些比“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:
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!