How do I search the open buffers in Vim?

后端 未结 6 456
陌清茗
陌清茗 2020-12-23 16:36

I\'d like to search for text in all files currently open in vim and display all results in a single place. There are two problems, I guess:

  • I can\'t pass the li
6条回答
  •  生来不讨喜
    2020-12-23 17:08

    I made this function a long time ago, and I'm guessing it's probably not the cleanest of solutions, but it has been useful for me:

    " Looks for a pattern in the open buffers.
    " If list == 'c' then put results in the quickfix list.
    " If list == 'l' then put results in the location list.
    function! GrepBuffers(pattern, list)
        let str = ''
    
        if (a:list == 'l')
            let str = 'l'
        endif
    
        let str = str . 'vimgrep /' . a:pattern . '/'
    
        for i in range(1, bufnr('$'))
            let str = str . ' ' . fnameescape(bufname(i))
        endfor
    
        execute str
        execute a:list . 'w'
    endfunction
    
    " :GrepBuffers('pattern') puts results into the quickfix list
    command! -nargs=1 GrepBuffers call GrepBuffers(, 'c')
    
    " :GrepBuffersL('pattern') puts results into the location list
    command! -nargs=1 GrepBuffersL call GrepBuffers(, 'l')
    

提交回复
热议问题