我如何像这样与 VIM 的表格插件对齐?

发布于 2024-10-26 20:36:27 字数 305 浏览 5 评论 0原文

这是我的代码:

john,betty,wally,beth 
walter,george,thomas,john
herbert,bob,petty,mick`

有谁知道如何使用 VIM 的表格插件将其与此对齐:

john,    betty,  wally,  beth
walter,  george, thomas, john
herbert, bob,    petty,  mick

我知道如何在对齐插件中执行此操作,但无法找出它在表格中的工作原理。

This is my code:

john,betty,wally,beth 
walter,george,thomas,john
herbert,bob,petty,mick`

Does anyone know how to align it to this with VIM's Tabular Plugin:

john,    betty,  wally,  beth
walter,  george, thomas, john
herbert, bob,    petty,  mick

I know how to do this in Align Plugin but can't find out how it works in Tabular.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

垂暮老矣 2024-11-02 20:36:28

另一种选择可能是欺骗并将名称也用作分隔符的一部分:

:Tabularize /[a-z]*,/l0l1

它会查找带有逗号的小写名称。所有内容均左对齐,并且分隔逗号后至少有一个空格。

Another option may be to cheat and use the names also as a part of the delimiter:

:Tabularize /[a-z]*,/l0l1

Which looks for any name in lowercase with a comma. Everything is aligned left and at least one space follows on the delimited comma.

生寂 2024-11-02 20:36:28

这个怎么样:

%s/,/,\t/g

And how about this:

%s/,/,\t/g
玻璃人 2024-11-02 20:36:27
Tabularize /,\zs

请参阅 :help \zs

由于之前的模式不起作用,请尝试以下操作:

Tabularize /[^,]\+,
Tabularize /,\zs

See :help \zs

Since the previous pattern doesn't work, try with this:

Tabularize /[^,]\+,
夏の忆 2024-11-02 20:36:27

我不是这方面的专家,我在这里所说的一切都是基于对 Tabularize 的理解。

制表总是将事物分割成字段,由正则表达式描述。例如:

abc,d,e
a,b,cde

:Tab /,

导致它像下面这样划分每一行:

|abc|,|d|,|e|
|a|,|b|,|cde|

然后每个字段都用空格填充,以便分隔符字段对齐 - 并且默认情况下所有内容都会接收和额外的空格(我认为除了最后一个字段)。

|abc |, |d |, |e  |
|a   |, |b |, |cde|

结果是:

abc , d , e
a   , b , cde

通过添加标志,您可以控制每个字段的对齐和填充。如果您提供的标志少于所需的标志,它们将被重复使用。因此,为了使所有内容都以相同的方式对齐,例如向左填充 0,您可以使用一个将为每个字段重复的标志。所以:

abc,d,e
a,b,cde

Tab /,/l0 <-- or c0, or r0 or whatever

abc,d,e
a  ,b,cde

我的结论是,零宽度字段(如 :Tab /,\zs)没有多大意义,可能会导致它在图案并弄乱,切割它。

现在,对我来说 :Tab /[^,]\+, 无法正常工作,生成双倍空格:

john,     betty,   wally,  beth
walter,   george,  thomas, john
herbert,  bob,     petty,  mick`
        ^^       ^^

我认为这是因为没有中间字段。这种模式使一个字段分隔符与另一个字段分隔符并排存在,如下所示:

|john,||betty,||wally,||beth|

然后,默认情况下,生成的字段的零(分隔符之间)也会用额外的 1 空格填充。

|john, | |betty, | |wally, ||beth| <-- for some outrageous reason the las one is cut.

john,  betty,  wally, beth

如何解决?

我会为分隔符留出空间,这不会导致分隔符靠近其他分隔符。如何?只需在逗号后添加一个空格即可。

john,betty,wally,beth 
walter,george,thomas,john
herbert,bob,petty,mick`

:%s/,/, /g

john, betty, wally, beth 
walter, george, thomas, john
herbert, bob, petty, mick`

现在您可以在空格和零填充上对齐所有内容:

john, betty, wally, beth 
walter, george, thomas, john
herbert, bob, petty, mick`

:Tab / /l0

john,    betty,  wally,  beth
walter,  george, thomas, john
herbert, bob,    petty,  mick`

我希望这有助于理解表格化!

I'm not a master in this, and everything I'll say here is based on my understanding of Tabularize.

Tabularize always splits things in fields, described by the regex. For example:

abc,d,e
a,b,cde

:Tab /,

Causes it to divide each line like the following:

|abc|,|d|,|e|
|a|,|b|,|cde|

Then each field is padded with spaces so the delimiter field align — and everything receives and extra-space by default (except for the last field, I think).

|abc |, |d |, |e  |
|a   |, |b |, |cde|

Resulting in:

abc , d , e
a   , b , cde

By adding flags, you can control alignment and padding for each field. If you provide less flags than needed, they are reused. So, to make everything align the same way, say left with padding 0 you can use a single flag that will be repeated for every field. So:

abc,d,e
a,b,cde

Tab /,/l0 <-- or c0, or r0 or whatever

abc,d,e
a  ,b,cde

The conclusion I have I my mind is that there isn't much sense in having a zero width field (like :Tab /,\zs), probably causing it to get the first character after the pattern and messing up, cutting it.

Now, for me :Tab /[^,]\+, didn't worked properly, generating doubled spaces:

john,     betty,   wally,  beth
walter,   george,  thomas, john
herbert,  bob,     petty,  mick`
        ^^       ^^

I think that's because there is no intermediate field. This pattern, makes a field delimiter reside side by side with another one, like this:

|john,||betty,||wally,||beth|

Then the zero with field generated (between delimiters) is also padded with the extra 1 space by default.

|john, | |betty, | |wally, ||beth| <-- for some outrageous reason the las one is cut.

john,  betty,  wally, beth

How to solve it?

I'd open space for a delimiter, that doesn't cause a delimiter to be near others. How? Simply adding a space after the comma.

john,betty,wally,beth 
walter,george,thomas,john
herbert,bob,petty,mick`

:%s/,/, /g

john, betty, wally, beth 
walter, george, thomas, john
herbert, bob, petty, mick`

Now you can align everything on spaces and zero padding:

john, betty, wally, beth 
walter, george, thomas, john
herbert, bob, petty, mick`

:Tab / /l0

john,    betty,  wally,  beth
walter,  george, thomas, john
herbert, bob,    petty,  mick`

I hope that helps understanding Tabularize!

晒暮凉 2024-11-02 20:36:27

以雷蒙迪的答案为基础,

:Tabularize /,\zs/l0r1

对我来说更有意义,而且它也只用一个命令:-)。

l0r1 表示左对齐,左侧填充 0 个空格,右侧填充 1 个空格。

Building on Raimondi's answer,

:Tabularize /,\zs/l0r1

makes more sense to me and it's also in just one command :-).

The l0r1 means left-align with 0 space in the left padding and 1 space in the right padding.

末蓝 2024-11-02 20:36:27

我正在做类似的事情。我经常需要将逗号分隔的值转换为列:

a, b
a,b,c
a, b, c
a, b, c
ab,cd,ef
ab, cd,ef
ab,cd
ab, cd

john, betty davis, wally, beth 
walter, george, thomas, john
herbert, bob, petty, mick

使用 :s/,\s*/, /g 准备值以在逗号后添加一个空格:

a, b
a, b, c
a, b, c
a, b, c
ab, cd, ef
ab, cd, ef
ab, cd
ab, cd

john, betty davis, wally, beth 
walter, george, thomas, john
herbert, bob, petty, mick

运行 :Tab / ,\zs \+/10 将它们变成:

a,  b
a,  b,  c
a,  b,  c
a,  b,  c
ab, cd, ef
ab, cd, ef
ab, cd
ab, cd

john,    betty davis, wally,  beth
walter,  george,      thomas, john
herbert, bob,         petty,  mick

现在我只需要弄清楚如何将它们变成有效的 vmap

I have something similar I'm doing. I regularly need to turn comma-delimited values into columns:

a, b
a,b,c
a, b, c
a, b, c
ab,cd,ef
ab, cd,ef
ab,cd
ab, cd

john, betty davis, wally, beth 
walter, george, thomas, john
herbert, bob, petty, mick

Using :s/,\s*/, /g preps the values to add a single space after the commas:

a, b
a, b, c
a, b, c
a, b, c
ab, cd, ef
ab, cd, ef
ab, cd
ab, cd

john, betty davis, wally, beth 
walter, george, thomas, john
herbert, bob, petty, mick

Running :Tab /,\zs \+/10 turns them into:

a,  b
a,  b,  c
a,  b,  c
a,  b,  c
ab, cd, ef
ab, cd, ef
ab, cd
ab, cd

john,    betty davis, wally,  beth
walter,  george,      thomas, john
herbert, bob,         petty,  mick

Now I just need to figure out how to turn them into a vmap that works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文