我的复制运算符源代码编写如下。
foo = rhs.foo;
foobar = rhs.foobar;
bar = rhs.bar;
toto = rhs.toto;
我想将内容排列如下(更容易阅读,不是吗?)。
foo = rhs.foo;
foobar = rhs.foobar;
bar = rhs.bar;
toto = rhs.toto;
是否有一个 VIM 魔法插入到第 N 列,或者类似的东西可以让我每行敲击几次按键来排列内容?
最佳答案
这里的其他答案都很棒,尤其是 @nelstrom 对 Tabular.vim 的评论和他出色的截屏视频。
但是如果我懒得安装任何 Vim 插件,但又愿意使用 Vim 宏,我就会使用宏。
算法:
For each line,
Add tons of spaces before the symbol =
Go to the column you want to align to
Delete all text up to =, thereby shifting the = into the spot you want.
对于您的示例,
foo = rhs.foo;
foobar = rhs.foobar;
bar = rhs.bar;
toto = rhs.toto;
将光标定位在第一行的任意位置,并在正常模式下键入以下内容来记录该行的宏:
qa0f=100i <Esc>8|dwjq
这意味着:
-
qa
-- 在热键中录制宏a
-
0
-- 转到行首 -
f=
-- 转到第一个等号 -
100i <Esc>
-- (i
后面有一个空格,<Esc>
表示按转义键,不要输入“”。)插入 100 个空格 -
8|
-- 转到第 8 列(抱歉,您必须手动确定要对齐到哪一列) -
dw
-- 删除直到下一个非空格字符 -
j
-- 转到下一行 -
q
-- 停止录音。
然后运行存储在热键 a
处的宏, 3 次(对于剩下的 3 行),将光标放在第二行并按:
3@a
关于vim - 如何插入空格直至 X 列以将列中的内容对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6154306/