vim非贪婪搜索并拷贝结果:

如下的html code:

<div class="pic_list"><ul><!--开始 --><li class="fl"> <div class="pic"><span class="pic_center"><i></i><a href="/VideoPlayer/A5D3EAF22059DB53/A5E4EC9BEDEAD3FA-429EB6D3BECB871C.shtml"><img src="http://117.34.87.32/Files/201206/20120608084232656micrS.jpg" /></a></span></div><div class="title_bg"><p class="title"><a href="/VideoPlayer/A5D3EAF22059DB53/A5E4EC9BEDEAD3FA-429EB6D3BECB871C.shtml"></span></div>

想查找以VideoPlayer开头,以shtml结束的字符串,用:video.*player.*shtml 正则匹配,结果如下:

image-20190120124849052

用非贪婪匹配:

\/video.\{-}player.\{-}shtml

结果如下:

image-20190120124920178

对于上面的结果,如果有一千个结果,想把搜到的结果保存下来,定义如下vim函数:

"http://vim.wikia.com/wiki/Copy_search_matches
"https://www.vim.org/scripts/script.php?script_id=4795
function! CopyMatches(reg)
  let hits = []
  %s//\=len(add(hits, submatch(0))) ? submatch(0) : ''/gne
  let reg = empty(a:reg) ? '+' : a:reg
  execute 'let @'.reg.' = join(hits, "\n") . "\n"'
endfunction
command! -register CopyMatches call CopyMatches(<q-reg>)

执行Ex命令

:CopyMatches

就会把结果拷到*寄存器。

REF:

Search

    Table of Contents