Vim: multi-command for filtering out blocks of text

ⅰ亾dé卋堺 提交于 2019-12-06 17:06:32

One can use the following implementation.

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

I had a good old play around with this and learnt quite a bit in the process! Looks like there are a few problems going on here.

Here's how I worked through to get it working, although as there are so many approaches with vim there is probably a tidier way somehow.

The g command is of the form g/pattern/command. What I think was happening was in your original form, the | vnew | put a | %s... part of the command was being executed linewise with the g command, rather than once as part of the TagSearch command. I changed the g command to being 'executed' instead which solves the problem - I'd be interested to know if there is a way to specify what the pipe applies to without using execute, I couldnt get it working (brackets don't work for example). This would have been why you were only getting the first line. This gives us (fixing a typo in your '%s' command):

command! -nargs=1 TagSearch execute 'g/^\(.\+\n\)\{-}.*#<args>.*/y a' | vnew | put a | %s/^.*#<args>.*\n/&\r

This seems to reverse the problem though, and we now only get the last line in the new buffer. The other problem with the g command, is that when you do /y a, it overwrites the a register each time. There is a way to get vim to append to a register instead, using the capitalized register name (/y A) (see :help quotea). Doing it this way we need to initialize the register to blank first, using let. That gives us this:

command! -nargs=1 TagSearch let @a='' | execute 'g/^\(.\+\n\)\{-}.*#<args>.*/y A' | vnew | put a | %s/^.*#<args>.*\n/&\r

Finally to get it to execture with multiple tags, I just fiddled with the <args> a bit (calling TagSearch tag1 tag2, args would literally be the string 'tag1 tag2') to get it to fit in the regex as below:

command! -nargs=* TagSearch let @a='' | execute 'g/^\(.\+\n\)\{-}.*#\(' . substitute('<args>', ' ', '\\|', 'g') . '\).*/y A' | vnew | put a | execute '%s/^.*#\(' . substitute('<args>', ' ', '\\|', 'g') . '\).*\n/&\r'

If you add any more features to this you might want to try playing around with a little vimscript function or something instead, otherwise it might get pretty hard to maintain! You'd be able to grab your text blocks into nice lists and potentially process them a bit more easily rather than having to do everything as if you were actually typing. Take a look at :help functions for things that are available (although there may be a better starting point in the help for vimscript somewhere else).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!