如何在Vim中插入可变数量的空格以对齐文本列? [英] How to insert variable number of spaces necessary to align textual columns in Vim?
问题描述
我是Vim中Visual模式的粉丝,因为它允许在任何给定列之前插入文本.
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
可以通过< Ctrl-V> jjI< Esc>
来完成:
> 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< Esc>
. - 将这些空格的一部分删除回某个列(例如15):
d | 15
. - 将这些步骤另存为宏,并根据需要重复执行该宏.
但是,这种方法非常丑陋!
But this approach is very ugly, though!
推荐答案
如果没有任何vim插件,我的状况会更好.这是我的解决方案:
I'm much better off without any vim plugins. Here is my solution:
< Shift-V> jj:!column -ts-
然后将-
插入多行,就像您在问题中所写的一样.
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
< Ctrl-V> jjA-- xxx< Esc>
这篇关于如何在Vim中插入可变数量的空格以对齐文本列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!