PAS
PAS

Reputation: 2055

How to autoupdate vimgrep search result in quickfix window?

How to autoupdate search vimgrep results from quickfix window after file is autoudpated due to change?

I use :vim/pattern/% to search and put result in quickfix window.

Upvotes: 0

Views: 596

Answers (1)

Peter Rincker
Peter Rincker

Reputation: 45087

You can always do <up> on the command line with a prefix of :vimgrep to make searching history easier. You may also want to check out q: and the use ?// to search for the command to re-execute.

Assuming the quickfix title is set correctly, you can use following the command:

:execute get(getqflist({'title':1}), 'title')

This however I am not certain this will work with all :grep/:make commands. You also need a newer Vim version to get the Quckfix list title (Vim 8+ I think).

If you find yourself doing this often you may want to bind a mapping or command.

command! -nargs=0 -bar QFRefresh execute get(getqflist({'title':1}), 'title')

Now how to add do this automatically? We can use FileChangedShellPost autocmd to run our QFRefresh command once a file change has been detected. Add the following to you vimrc file:

augroup QFRefresh
  autocmd!
  autocmd FileChangedShellPost * if get(b:, 'qfrefresh_auto', 0) | QFRefresh | endif
augroup END

command! -nargs=0 -bar QFAutoRefreshToggle let b:qfrefresh_auto = !get(b:, 'qfrefresh_auto', 0) | echo b:qfrefresh_auto ? 'Auto Refresh' : 'No Auto Refresh'

Now you can use :QFAutoRefreshToggle to toggle refreshing a file.

Note: As stated before QFRefresh uses the quickfix's title to get the quickfix command. If the title is not set correctly a refresh may not work correctly. Also I am not sure what guarantees Vim has on triggering FileChangedShellPost. You can force a check via :checktime command.

For more help see:

:h getqflist()
:h :execute
:h :get
:h q:
:h cmdwin
:h c_Up
:h FileChangedShellPost
:h :checktime

Upvotes: 1

Related Questions