1

Is there a way echoing a message if grep has no result and getqflist() is empty?

I've tried this, but it doesn't work:

au QuickFixCmdPost grep call g:GrepPostActions() 

function! g:GrepPostActions()
  let qflist = getqflist()
  if len(qflist) > 0
    "Move window to the very bottom
    wincmd J
  else 
    "??? QuickFixCmdPost doesn't fire on empty qflist ???
     echo 'No match found in '.getcwd() 
  endif
endfunction

  • As the help text says "...after a quickfix command is run, before jumping to the first location", it would be valid to not fire the event, if there is no location to jump to. – Ralf Apr 18 at 15:09
0

Finally, I came up with this solution:

function! EchoResultMsg()
  let qflist = getqflist()
  if len(qflist) > 0
    echo 'grep: '.len(qflist).' matches found.'
  else
    echo 'grep: No match found in '.getcwd() 
  endif
endfunction

command! -nargs=+ -complete=file Grep grep <f-args> | cwindow | call EchoResultMsg() 

The newly defined :Grep command calls cwindow and EchoResultMsg after grep. The cwindow command doesn't open the quickfix window, if there is no resulted match, but EchoResultMsg can check the length of qflist.

This is the simplified part of the code. The whole plugin can be found here: https://github.com/mattia72/vim-ripgrep

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.