在 Vim 中使用 AStyle
我正在尝试让 AStyle 与 Vim 一起使用,以便我可以使用“=”键重新缩进代码的各个部分。例如,我希望能够输入常用的 =iB
来使用 AStyle 而不是内置的缩进器来缩进当前的代码块。
我尝试在 vimrc 中设置 equalprg=astyle ,但问题是 astyle 只接收选定的块,但认为它正在接收整个文件。因此,当我尝试仅缩进嵌套类时,缩进完全关闭。
我知道我总是可以一次重新格式化整个文件,但是有没有一种方法可以在 vim 中使用 astyle 来完全复制 vim 的原始格式化行为(我所有的 =
-movement 命令都有效 - 还有奖励也可以使用 astyle 进行自动缩进!)?
I am trying to get AStyle working with Vim so that I can use the "=" key to re-indent various sections of code. For example, I'd like to be able to type my usual =iB
to indent the current block of code using AStyle rather than the built in indenter.
I tried just setting equalprg=astyle in my vimrc, but the problem is that astyle only receives the selected block but thinks that it's receiving a whole file. Therefore, the indentation is completely off when I try to only indent a nested class.
I know that I can always reformat an entire file at once, but is there a way to use astyle in vim which completely replicates the original formatting behavior of vim (all my =
-movement commands work - and bonus points for autoindent using astyle as well!)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非 AStyle 版本具有部分文件格式选项,否则您需要在运行 AStyle 后应用额外的缩进。
我不确定你如何通过动作来做到这一点。
通过视觉选择,您可以从第一行获取缩进,将代码传递给 equalprg,然后将该缩进添加到所有行:
分解:
也许您可以对动作做类似的事情?
Unless there is a version of AStyle that has a partial file formatting option, you'll need to apply the extra indentation after you run AStyle.
I'm not sure how you can do this with motions.
With visual selection, you could grab the indentation from the first line, pass the code to equalprg, and then add that indentation to all of the lines:
Breaking it down:
Maybe you can do something similar with motions?
它仅适用于具有部分文件格式化选项的格式化程序,就像 idbrii 已经指出的那样。执行此操作的格式化程序示例是 clang-format。
将其集成到 vim 中的一种方法是使用 vim-autoformat。使用此插件,您可以
viB
,然后按您自定义的格式键,例如
。这将仅格式化选定的内部代码块。It only works for formatters that have a partial file formatting option, like idbrii already pointed out. An example of a formatter that does this is clang-format.
One way to integrate this into vim is by using vim-autoformat. Using this plugin you can
viB
and then press your self-defined format key, like<F3>
. This will then only format the selected inner code block.