Using AStyle in Vim

妖精的绣舞 提交于 2019-12-03 13:25:25

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:

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

Breaking it down:

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?

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!