如何在 Vim 中插入对齐文本列所需的可变数量的空格? [英] How to insert variable number of spaces necessary to align textual columns in Vim?
问题描述
我很喜欢 Vim 中的可视化模式,因为它允许在任何给定的列之前插入文本.
I’m a fan of Visual mode in Vim, as it allows to insert text before any given column.
例如,在下面的引文前导后插入空格:
For example, insertion of spaces after the quotation leaders below:
> one
> two
> three
可以通过
来完成:
> one
> two
> three
如下:
- 使用 Ctrl-V 启动可视模式.
- 使用
jj
扩展视觉选择. - 用
I__
插入一些空格. - 使用 Esc 将更改传播到块选择的所有行.
- Start Visual mode with Ctrl-V.
- Extend visual selection with
jj
. - Insert some spaces with
I__
. - Propagate the change to all the lines of the block selection with Esc.
现在我有一个需要一些格式的文本文件.这是它的样子:
Now I have a text file that needs some formatting. This is what it looks like:
start() -- xxx
initialize() -- xxx
go() -- xxx
现在我想对齐此文本的一部分以将其排列成这样的列:
Now I want to align part of this text to arrange it into columns like this:
start() -- xxx
initialize() -- xxx
go() -- xxx
我遇到的问题是我不能在每一行中插入不同数量的缩进,仅仅缩进固定数量的空格/制表符是不够的.如果所有缩进的文本都必须在同一列中对齐,如何进行缩进?
The problem I have is that I cannot insert a different amount of indentation into each line and merely indenting a fixed amount of spaces/tabs is insufficient. How can you do an indentation where all indented text will have to be aligned at the same column?
我只是想出了一个相当冗长和笨拙的方法:
I only figured out a rather verbose and unwieldy method:
- 找到要缩进的字符串位置:
--
. - 在此之前插入 n(假设为 20)个空格:
20i
. - 将这些空格的一部分删除回某个列(假设为 15):
d|15
. - 将这些步骤保存为宏,并根据需要经常重复宏.
但是这种方法非常难看!
But this approach is very ugly, though!
推荐答案
如果没有任何 vim 插件,我会过得更好.这是我的解决方案:
I'm much better off without any vim plugins. Here is my solution:
然后将 --
插入多行,就像你在问题中写的一样.
Then insert --
into multiple lines just as you wrote in the question.
您还可以在插入时附加一些注释.
You can also append a number of comments at insertion time.
:set virtualedit=all
这篇关于如何在 Vim 中插入对齐文本列所需的可变数量的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!