如何在Vim中过滤cscope输出

5

我希望找到一种从Vim中grep cscope查询输出的方法。

以下方法对我无效:

:cs f s symbol !grep pattern

这将会给出:

E259: no matches found for cscope query s symbol !grep pattern ...

P.S:
我知道使用redir命令可以过滤ex命令的输出,但我正在寻找一种更简单的方法来通过Unix命令过滤输出。

2个回答

6
您可以使用:redir将消息输出发送到寄存器或文件。
redir @c
cs f s symbol
redir END

现在,您可以将 c 寄存器放入文件并进行过滤。
我从cscope那里得不到太多输出(全部都在快速修复中),但这将完成您所描述的操作。
一般来说,您可以使用|(竖杠)过滤shell命令(请参见: help:!cmd)。
:!echo 0updateView | cscope -dl | grep global

但是ex命令将竖线解释为命令分隔符(因此您可以在一行上放置多个命令):
:if &ft != 'help' | silent! cd %:p:h | endif

我认为你无法过滤 ex 命令的输出,除非使用 redir。然而,你可以使用 Benoit 的答案来过滤快速修复。


0

这里是我用于过滤quickfix列表的函数和宏:

使用方法:

  • _qf_qF_qp_qP将打开一个对话框提示
  • 您将在该对话框提示中输入Vim模式
  • _qf_qF将过滤文件名,_qp_qP将过滤行内容
  • 大写版本具有:v效果(保留不匹配的行),普通版本保留与您的模式匹配的行。
  • 使用:colder:cnewer,如果结果不满意,您可以跳转到较旧和较新的quickfix列表。我也有映射来调用这些命令。

代码:

" Filter Quickfix list
function! FilterQFList(type, action, pattern)
    let s:curList = getqflist()
    let s:newList = []
    for item in s:curList
        if a:type == 'f'     " filter on file names
            let s:cmpPat = bufname(item.bufnr)
        elseif a:type == 'p' " filter on line content (pattern)
            let s:cmpPat = item.text . item.pattern
        endif
        if item.valid
            if a:action == '-'
                " Delete matching lines
                if s:cmpPat !~ a:pattern
                    let s:newList += [item]
                endif
            elseif a:action == '+'
                " Keep matching lines
                if s:cmpPat =~ a:pattern
                    let s:newList += [item]
                endif
            endif
        endif
    endfor
    " Assing as new quickfix list
    call setqflist(s:newList)
endfunction

nnoremap _qF            :call FilterQFList('f', '-', inputdialog('Delete from quickfix files matching: ', ''))<CR>
nnoremap _qf            :call FilterQFList('f', '+', inputdialog('Keep only quickfix files matching: ', ''))<CR>
nnoremap _qP            :call FilterQFList('p', '-', inputdialog('Delete from quickfix lines matching: ', ''))<CR>
nnoremap _qp            :call FilterQFList('p', '+', inputdialog('Keep only quickfix lines matching: ', ''))<CR>

对我没有用。我也使用了:set cscopequickfix=s-,c-,d-,i-,t-,e-,a-。实际上,我可以使用:call FilterQFList调用此函数,但是键映射就是不起作用。我的意思是,在使用cscope搜索之前,这个映射可以在正常模式下工作。另外,我使用了huge特性来构建我的vim。 - VicX

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接