我有一个包含简单文本行的文本文件。我想创建一个函数vim
(and gvim
) 文本编辑器
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.
我在网上搜索并找到了一些有用的链接,但没有一个可以完成上述所有操作:
以下将查找并删除所有不包含该模式的行:
:v/pattern/d
可以使用脚本完成多次搜索和突出显示,如下所示MultipleSearch
http://www.vim.org/scripts/script.php?script_id=479 http://www.vim.org/scripts/script.php?script_id=479。布尔逻辑搜索可以通过以下方式完成LogiPat
script https://vim.sourceforge.io/scripts/script.php?script_id=1290 https://vim.sourceforge.io/scripts/script.php?script_id=1290。过滤包也可用,但我无法使其工作:http://www.vim.org/scripts/script.php?script_id=2759 http://www.vim.org/scripts/script.php?script_id=2759
要使用 AND 搜索并保留具有多种模式的行:
":v/.*pattern1\&.*pattern2/d"
但我每次都输入代码。
如何创建一个递归运行的函数:v/pattern/d
只查找包含所有模式的行?我希望该函数可以运行为:
:Myfn pattern1 pattern2 pattern3
编辑:我尝试为此函数编写以下代码,尝试在 vim 中使用 Linux grep 命令:
: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