Vim:将由空格分隔的列对齐,但仅限于前几列,避免处理注释中的空格

时间:2016-07-13 20:09:23

标签: vim vertical-alignment

给出以下文字:

create table hivetable
(
  field1        string                  comment 'one comment #1 here',
  field2     string                   comment 'toto comment #2 here',
  field3        string,
  field4        string,
  field5  bigint    comment 'foobar comment #5 here',              
  field6          bigint         
)

我如何调用Vim的Align脚本来获得最终结果:

create table hivetable
(
  field1 string  comment 'one comment #1 here',
  field2 string  comment 'toto comment #2 here',
  field3 string,
  field4 string,
  field5 bigint  comment 'foobar comment #5 here',              
  field6 bigint         
)

我想这将是一个多步骤的过程。我想知道如何指示Align只对齐两个第一列并保持原样。

create table hivetable
(
  field1 string                  comment 'one comment #1 here',
  field2 string                   comment 'toto comment #2 here',
  field3 string,
  field4 string,
  field5 bigint    comment 'foobar comment #5 here',              
  field6 bigint         
)

之后,“:对齐评论”和一些手动编辑,我将结束:

create table hivetable
(
  field1 string  comment 'one comment #1 here',
  field2 string  comment 'toto comment #2 here',
  field3 string,
  field4 string,
  field5 bigint  comment 'foobar comment #5 here',
  field6 bigint
)

2 个答案:

答案 0 :(得分:1)

安装插件并通过键入:help align阅读帮助后,我通过首先设置控件对齐来实现这一目的:

:AlignCtrl =Clp1P1IW \S\+

之后您需要做的就是选择要通过linewise-visual Shift + v 对齐的块并在其上应用命令{{1} }:

Align

这是结果:

:'<,'>Align

如果要按照注释中的讨论进行对齐,则需要在对齐控件之后运行此命令:

create table hivetable
(
   field1  string   comment  'comment  #1  here', 
   field2  string   comment  'comment  #2  here', 
   field3  string, 
   field4  string, 
   field5  bigint   comment  'comment  #5  here', 
   field6  bigint  
)

答案 1 :(得分:1)

我不使用Align,但我使用Tabular

因此,在您的情况下,您希望对齐第二列的第一个字符,即此处的类型。

假设您有set hlsearch,您可以创建一个vim正则表达式,使用标准的vim搜索捕获这些第一个字符,并通过观察突出显示来直观地确认它。

如果您想对每个单词对齐,那么对齐模式可以简单为\w\+

(
  field1  string   comment ' comment # 1  here ',
  field2  string   comment ' comment # 2  here ',
  field3  string ,
  field4  string ,
  field5  bigint   comment ' comment # 5  here ',
  field6  bigint
)

如果您只想按第二列进行对齐,则搜索模式必须稍微复杂^\s*\w\+\s\+\zs\zs表示忽略我的\zs符号前面的匹配字符,从此向前突出显示内容(vim向前看)。

(
  field1  string                  comment 'comment #1 here',
  field2  string                   comment 'comment #2 here',
  field3  string,
  field4  string,
  field5  bigint    comment 'comment #5 here',
  field6  bigint
)

这里的要点是,您使用vim搜索机制/pattern形成一个与您正在对齐的列匹配的正则表达式,并使用突出显示来直观地确认它是正确的。然后,您可以选择行vi(:'<,'>Tab /CTRL+R/,其中CTRL+R表示您按CTRL和R来检索寄存器,然后是/搜索寄存器。< / p>