5

I am looking for a way to grep the output of cscope queries from Vim.

The following didn't work for me:

:cs f s symbol !grep pattern

It gave:

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

P.S:
I know the redir method, I am looking for a simpler way to filter
output of ex command(s) through Unix commands.

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
Aman Jain
  • 10,927
  • 15
  • 50
  • 63

2 Answers2

6

You can use :redir to send message output to a register or file.

redir @c
cs f s symbol
redir END

Now you can put the c register into a file and filter it.

I don't get much output from cscope (it's all in the quickfix), but that will do what you're describing.


In general, you can filter shell commands (see :help :!cmd) with | (bar):

:!echo 0updateView | cscope -dl | grep global

But ex commands interpret bar as a command separator (so you can put multiple commands on one line):

:if &ft != 'help' | silent! cd %:p:h | endif

I don't think you can filter the output of ex commands aside from using redir. However, you could use Benoit's answer to filter the quickfix.

idbrii
  • 10,975
  • 5
  • 66
  • 107
0

Here are my functions and macros to filter the quickfix list:

Usage:

  • _qf, _qF, _qp and _qP will open a dialog prompt
  • You will type a Vim pattern into that dialog prompt
  • _qf and _qF will filter on file names, _qp and _qP on line contents
  • capitalized versions have a :v effect (keep lines not matching), normal versions keep lines matching your pattern.
  • With :colder and :cnewer you can jump through older and newer quickfix lists if the results do not satisfy you. I have mappings to call those commands, too.

The code:

" 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>
Benoit
  • 76,634
  • 23
  • 210
  • 236
  • Did not work for me. I have also `:set cscopequickfix=s-,c-,d-,i-,t-,e-,a- `. Actually I can call this function with `:call FilterQFList`, but the key mapping just do not work. I mean this mapping can work on normal mode before I search anything with cscope. Also I build my vim with `huge` feature. – VicX Aug 31 '16 at 15:20