Vim Tabular:如何忽略长评论

Ner*_*ste 5 python vim tabular

Tabular在 Vim 中使用来将 Python 中数据文件的定义与一些长注释对齐:

# A long comment here with many words
param_1 = 42
parameter_2 = 0.25

# Another long comment
para_3 = 'abc'
parameter_number_4 = some_var
Run Code Online (Sandbox Code Playgroud)

如果我Tabularize /=在选择上使用,评论也会被处理,因为有些评论很长,我得到:

# A long comment here with many words
param_1                               = 42
parameter_2                           = 0.25

# Another long comment
para_3                                = 'abc'
parameter_number_4                    = some_var
Run Code Online (Sandbox Code Playgroud)

这不是我想要的。#有没有办法忽略选择中以“”开头的行?为了获得这样的东西:

# A long comment here with many words
param_1            = 42
parameter_2        = 0.25

# Another long comment
para_3             = 'abc'
parameter_number_4 = some_var
Run Code Online (Sandbox Code Playgroud)

Eve*_*ree 4

我建议您使用global命令而不是视觉选择来告诉表格化要对齐的内容。在你的情况下,像这样:

:%g/^\s*[^#]/Tabularize /=
Run Code Online (Sandbox Code Playgroud)

这会找到文件中不是注释的每一行并Tabularize /=对其运行。如果您想定位更具体的行,可以将 替换%cmdline-range. 您也可以直接传递 acmdline-range来进行制表,如下所示:

:2,3Tabularize /=
Run Code Online (Sandbox Code Playgroud)

相关帮助主题:

:help cmdline-ranges
:help :g
Run Code Online (Sandbox Code Playgroud)