Vim:用于过滤文本块的多命令 [英] Vim: multi-command for filtering out blocks of text
问题描述
这是我关于在Vim中使用多行正则表达式。
一个快速的解释:我有一些文本标记为 #tags
,并用空白行分隔。我想过滤掉包含特定标记的所有块。我在这里帮助regex(感谢!),所以我构造了以下命令:
A quick explanation: I have blocks of text tagged with #tags
and separated by a blank line. I want to filter out all blocks that include a specific tag. I was helped with regex here (thanks!), so I constructed the following command:
command -nargs=1 TagSearch g/^\(.\+\n\)\{-}.*#<args>.*/y a | vnew | put a | %s/^#.*<args>.*\n/&\r
因此,执行:TagSearch标记
应该:
- 搜索
#tag
, - 将全部对应的文字块粘贴到新的垂直缓冲区 b
- 在新缓冲区中的文本块之间添加一个空行。
- Search for the
#tag
, - paste all corresponding Text Blocks into a new vertical buffer,
- Add a blank line between the Text Blocks in the new buffer.
Q1 :我分别做每一步,一切工作。但是多命令只会将第一个匹配的文本块粘贴到新的缓冲区中。为什么?
Q1: When I do each step separately, everything works. But the multi-command only pastes the first matching text block into the new buffer. Why?
Q2 :如何让命令接受多个参数(如果我想搜索许多 #tags
)?感谢!
Q2: How could I make the command accept multiple arguments (in case I want to search for many #tags
at a time)? Thanks!
推荐答案
可以使用以下实现。
command! -nargs=* -bar TagSearch call CollectParagraphs([<f-args>])
function! CollectParagraphs(tags) range
let tags = len(a:tags) > 0 ? a:tags : [expand('<cword>')]
let pats = map(copy(tags), '"\\.\\*#" . escape(v:val, "\\")')
let v = winsaveview()
let [sr, @/; lines] = [@/, '\V' . join(pats, '\&')]
g//call extend(lines, getline(search('\n\n\zs', 'bnW'), line("'}")))
let @/ = sr
call winrestview(v)
exe 'vnew' escape(join(tags), ' %#|\')
set buftype=nofile bufhidden=hide noswapfile
call setline(1, lines)
endfunction
这篇关于Vim:用于过滤文本块的多命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!