如何对齐代码参数列?

时间:2014-06-12 18:25:18

标签: vim

假设我有几行代码如下:

callbacks.registerFunction("position", bind(MovementControllerCallbacks::position, movementController, _1));
callbacks.registerFunction("setPosition", bind(MovementControllerCallbacks::setPosition, movementController, _1));
callbacks.registerFunction("translate", bind(MovementControllerCallbacks::translate, movementController, _1));
callbacks.registerFunction("positionDelta", bind(MovementControllerCallbacks::positionDelta, movementController, _1));
callbacks.registerFunction("measuredVelocity", bind(MovementControllerCallbacks::measuredVelocity, movementController, _1));
callbacks.registerFunction("velocity", bind(MovementControllerCallbacks::velocity, movementController, _1));
callbacks.registerFunction("setVelocity", bind(MovementControllerCallbacks::setVelocity, movementController, _1));
callbacks.registerFunction("setXVelocity", bind(MovementControllerCallbacks::setXVelocity, movementController, _1));
callbacks.registerFunction("setYVelocity", bind(MovementControllerCallbacks::setYVelocity, movementController, _1));
callbacks.registerFunction("applyForce", bind(MovementControllerCallbacks::applyForce, movementController, _1));
callbacks.registerFunction("control", bind(MovementControllerCallbacks::control, movementController, _1));
callbacks.registerFunction("xControl", bind(MovementControllerCallbacks::xControl, movementController, _1));
callbacks.registerFunction("yControl", bind(MovementControllerCallbacks::yControl, movementController, _1));
callbacks.registerFunction("inLiquid", bind(MovementControllerCallbacks::inLiquid, movementController, _1));
callbacks.registerFunction("onGround", bind(MovementControllerCallbacks::onGround, movementController, _1));
callbacks.registerFunction("collisionBounds", bind(MovementControllerCallbacks::collisionBounds, movementController, _1));

我希望它们看起来像:

callbacks.registerFunction("position",         bind(MovementControllerCallbacks::position,         movementController, _1));
callbacks.registerFunction("setPosition",      bind(MovementControllerCallbacks::setPosition,      movementController, _1));
callbacks.registerFunction("translate",        bind(MovementControllerCallbacks::translate,        movementController, _1));
callbacks.registerFunction("positionDelta",    bind(MovementControllerCallbacks::positionDelta,    movementController, _1));
callbacks.registerFunction("measuredVelocity", bind(MovementControllerCallbacks::measuredVelocity, movementController, _1));
callbacks.registerFunction("velocity",         bind(MovementControllerCallbacks::velocity,         movementController, _1));
callbacks.registerFunction("setVelocity",      bind(MovementControllerCallbacks::setVelocity,      movementController, _1));
callbacks.registerFunction("setXVelocity",     bind(MovementControllerCallbacks::setXVelocity,     movementController, _1));
callbacks.registerFunction("setYVelocity",     bind(MovementControllerCallbacks::setYVelocity,     movementController, _1));
callbacks.registerFunction("applyForce",       bind(MovementControllerCallbacks::applyForce,       movementController, _1));
callbacks.registerFunction("control",          bind(MovementControllerCallbacks::control,          movementController, _1));
callbacks.registerFunction("xControl",         bind(MovementControllerCallbacks::xControl,         movementController, _1));
callbacks.registerFunction("yControl",         bind(MovementControllerCallbacks::yControl,         movementController, _1));
callbacks.registerFunction("inLiquid",         bind(MovementControllerCallbacks::inLiquid,         movementController, _1));
callbacks.registerFunction("onGround",         bind(MovementControllerCallbacks::onGround,         movementController, _1));
callbacks.registerFunction("collisionBounds",  bind(MovementControllerCallbacks::collisionBounds,  movementController, _1));

这些块会被换行符抵消,因此您可以使用{和}来浏览它们。

我想制作一个宏或函数或其他东西来做这件事。但感觉我在我的vim工具箱中缺少一些重要的工具,因为我不知道如何开始解决这个问题。

例如,我不知道如何根据特定标记的列插入可变数量的空格,我假设该解决方案涉及。

我知道手动选择视觉块很容易。但是我有很多代码需要格式化,我希望避免使用手动解决方案。

1 个答案:

答案 0 :(得分:2)

您应该查看plugin Tabular

使用文本的第一行,您可以使用以下命令:

:%Tabularize /,

获得以下输出:

callbacks.registerFunction("position"         , bind(MovementControllerCallbacks::position         , movementController , _1));
callbacks.registerFunction("setPosition"      , bind(MovementControllerCallbacks::setPosition      , movementController , _1));
callbacks.registerFunction("translate"        , bind(MovementControllerCallbacks::translate        , movementController , _1));
callbacks.registerFunction("positionDelta"    , bind(MovementControllerCallbacks::positionDelta    , movementController , _1));
callbacks.registerFunction("measuredVelocity" , bind(MovementControllerCallbacks::measuredVelocity , movementController , _1));

我不知道是否可以在空格之前使用逗号。也许你可以通过详细查看这个插件来找到一些东西(否则,一个简单的替换应该可以做到这一点)。

编辑:命令中的%似乎没用。同时使用空格作为分隔符,您可以获得更接近您要查找的输出:

:Tabularize / /

输出:

callbacks.registerFunction("position",           bind(MovementControllerCallbacks::position,           movementController,   _1));
callbacks.registerFunction("setPosition",        bind(MovementControllerCallbacks::setPosition,        movementController,   _1));
callbacks.registerFunction("translate",          bind(MovementControllerCallbacks::translate,          movementController,   _1));
callbacks.registerFunction("positionDelta",      bind(MovementControllerCallbacks::positionDelta,      movementController,   _1));
callbacks.registerFunction("measuredVelocity",   bind(MovementControllerCallbacks::measuredVelocity,   movementController,   _1));