在Vim中使用AStyle [英] Using AStyle in Vim

查看:306
本文介绍了在Vim中使用AStyle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让AStyle使用Vim,以便我可以使用=键重新缩进代码的各个部分。例如,我想能够键入我通常的 = iB 使用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.

我试图在我的vimrc中设置equalprg = astyle,但问题是astyle只接收选择的块,但认为它接收一个整个文件。因此,当我尝试只缩进一个嵌套类时,缩进是完全关闭的。

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.

我知道我可以一次重新格式化整个文件,在vim中使用astyle,完全复制vim的原始格式化行为(所有我的 = - 移动命令工作 - 和自动缩进使用astyle的奖励点!)? p>

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!)?

推荐答案

除非有一个版本的AStyle有部分文件格式化选项,你需要在运行后应用额外的缩进AStyle。

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.

可以从第一行获取缩进,将代码传递给equalprg,然后将缩进添加到所有行:

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:

vnoremap = <Esc>`<dwgv=`<<C-v>`>I<C-r>"<Esc>

分解:

vnoremap -- so we can use = for equalprg
<Esc>`< -- stop selecting and go to beginning of line at beginning of selection
dw -- grab the initial indentation
gv= -- reselect and indent as normal
`<<C-v>`> -- block select the selection
I<C-r>"<Esc> -- insert the initial indentation

也许你可以做类似的动作吗?

Maybe you can do something similar with motions?

这篇关于在Vim中使用AStyle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆