Vim,删除一个正则表达式中的空白和注释行

时间:2022-12-07 18:19:15

I want to remove blank and commented lined at one time. I already found the similar question regarding removing blank lines, so for blank lines I use:

我想一次删除空白和注释。我已经找到了关于删除空行的类似问题,所以对于我使用的空行:

:g/^$/d

and for commented lines:

并为评论行:

:g/^#/d

I'm curious is there any way to merge these regex in one? Something like

我很好奇有没有办法合并这些正则表达式?就像是

:g/^[$#]/d

but obviously it doesn't work in vim.

但显然它在vim中不起作用。

5 个解决方案

#1


17  

You can try this command:

您可以尝试以下命令:

:g/^\(#\|$\)/d

Or

要么

:g/\v^(#|$)/d

  • $ matches literal '$' inside [...] (type :help /$ for help)
  • $匹配[...]内的文字'$'(类型:help / $ for help)
  • \| is for alternation
  • \ |是为了交替
  • \v is very magic (minimal backslash escape)
  • \ v非常神奇(最小的反斜杠逃脱)

#2


3  

You can combine regex patterns with the "or" operator: \|, eg:

您可以将正则表达式与“或”运算符组合:\ |,例如:

:g/^\(#.*\|$\)/d

Though, in this particular case, you actually just need to specify that #.* is optional, eg:

但是,在这种特殊情况下,您实际上只需要指定#。*是可选的,例如:

:g/^\(#.*\)\?$/d

Finally, be aware that you can chain together most commands with VIM's (not regex's) "pipe" operator, also |, eg:

最后,请注意,您可以将大多数命令与VIM(非正则表达式)“管道”运算符链接在一起,例如:

:g /^#/d | /^$/d

#3


3  

Try the following :

请尝试以下方法:

:g/^$/d | /^#/d

The | is there to combine multi command at the same time.

|是否可以同时组合多个命令。

#4


3  

Another way of solving this is keeping the non-commented lines:

另一种解决方法是保留未注释的行:

:g!/^[^#]/d

:克/ ^ [^#] / d

#5


0  

Expanding on kevs answer:
If anyone would also like to delete the comments in config files that are tabbed,
for example:

扩展kevs答案:如果有人还想删除选项卡文件中的注释,例如:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;
    ...

You can try this:

你可以试试这个:

:g/\v^(#|$|\t#)/d

#1


17  

You can try this command:

您可以尝试以下命令:

:g/^\(#\|$\)/d

Or

要么

:g/\v^(#|$)/d

  • $ matches literal '$' inside [...] (type :help /$ for help)
  • $匹配[...]内的文字'$'(类型:help / $ for help)
  • \| is for alternation
  • \ |是为了交替
  • \v is very magic (minimal backslash escape)
  • \ v非常神奇(最小的反斜杠逃脱)

#2


3  

You can combine regex patterns with the "or" operator: \|, eg:

您可以将正则表达式与“或”运算符组合:\ |,例如:

:g/^\(#.*\|$\)/d

Though, in this particular case, you actually just need to specify that #.* is optional, eg:

但是,在这种特殊情况下,您实际上只需要指定#。*是可选的,例如:

:g/^\(#.*\)\?$/d

Finally, be aware that you can chain together most commands with VIM's (not regex's) "pipe" operator, also |, eg:

最后,请注意,您可以将大多数命令与VIM(非正则表达式)“管道”运算符链接在一起,例如:

:g /^#/d | /^$/d

#3


3  

Try the following :

请尝试以下方法:

:g/^$/d | /^#/d

The | is there to combine multi command at the same time.

|是否可以同时组合多个命令。

#4


3  

Another way of solving this is keeping the non-commented lines:

另一种解决方法是保留未注释的行:

:g!/^[^#]/d

:克/ ^ [^#] / d

#5


0  

Expanding on kevs answer:
If anyone would also like to delete the comments in config files that are tabbed,
for example:

扩展kevs答案:如果有人还想删除选项卡文件中的注释,例如:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;
    ...

You can try this:

你可以试试这个:

:g/\v^(#|$|\t#)/d