在vim中递归搜索模式的函数 [英] Function to search recursively for patterns in vim

查看:58
本文介绍了在vim中递归搜索模式的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有简单文本行的文本文件.我想为 vim(和 gvim)文本编辑器创建一个函数,其中

I have a text file with simple text lines. I want to create a function for vim (and gvim) text editor which

can be sent a variable number of patterns and 
it should find lines will all patterns (in any order) 
and keep only these lines 
while deleting the rest. 

我在网上搜索并找到了一些有用的链接,但没有一个可以完成上述所有操作:

I searched the net and found some useful links but none that could do all above:

以下将查找并删除所有不包含该模式的行:

Following will find and delete all lines not containing the pattern:

 :v/pattern/d

可以使用脚本进行多次搜索和突出显示 MultipleSearch http://www.vim.org/scripts/script.php?script_id=479 .布尔逻辑搜索可以使用 LogiPat 脚本 https 完成://vim.sourceforge.io/scripts/script.php?script_id=1290 .过滤包也可用,但我无法使其工作:http://www.vim.org/scripts/script.php?script_id=2759

Multiple searching and highlighting can be done with scripts as MultipleSearch http://www.vim.org/scripts/script.php?script_id=479 . Boolean logic searching can be done with LogiPat script https://vim.sourceforge.io/scripts/script.php?script_id=1290 . A filtering package is also available but I could not make it work: http://www.vim.org/scripts/script.php?script_id=2759

使用 AND 搜索和保留具有多个模式的行:

To search and keep lines with multiple patterns with AND :

":v/.*pattern1\&.*pattern2/d"

但我每次都输入代码.

如何创建一个递归运行 :v/pattern/d 的函数以仅查找包含所有模式的行?我希望该功能可以运行为:

How can I create a function that recursively runs :v/pattern/d to find only lines that contain all the patterns? I expect the function can be run as:

:Myfn pattern1 pattern2 pattern3

我尝试为此函数编写以下代码,尝试在 vim 中使用 Linux grep 命令:

I have tried to write following code for this function, trying to use Linux grep command from within vim:

:function Myfn (Title, ...)
  : let outstr=""
  : for s in a:000
  :   outstr=!echo outstr | grep s
  : endfor
  : return outstr
 : endfunction

但我收到以下错误:

Not an editor command:   :   outstr=!echo outstr | grep s

推荐答案

您可以创建一个命令Keepword:

com! -nargs=* Keepword v/\v<args>/d

并称之为:

Keepword word1|word2|word3

使用该命令后,所有行(提供单词的行除外)将被删除.

After using the command all lines (except those with the words provided) will be deleted.

这篇关于在vim中递归搜索模式的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆