Use an AutoCommand
.
See :help QuickFixCmdPost
and :help QuickFixCmdPost-example
.
*QuickFixCmdPost*
QuickFixCmdPost Like QuickFixCmdPre, but after a quickfix
command is run, before jumping to the first
location. For |:cfile| and |:lfile| commands
it is run after the error file is read and
before moving to the first error.
See |QuickFixCmdPost-example|.
Also, from :help CTRL-W_<Enter>
:
When the quickfix window has been filled, two autocommand events are
triggered. First the 'filetype' option is set to "qf", which triggers the FileType event (also see |qf.vim|). Then the BufReadPost event is triggered, using "quickfix" for the buffer name. This can be used to perform some action on the listed errors. Example:
au BufReadPost quickfix setlocal modifiable
\ | silent exe 'g/^/s//\=line(".") .. " "/'
\ | setlocal nomodifiable
This prepends the line number to each line. Note the use of "=" in the substitute string of the ":s" command, which is used to evaluate an expression. The BufWinEnter event is also triggered, again using "quickfix" for the buffer name.
In your function, use getmatches()
to extract the last one and execute :match
on that string. Once you get it working, edit my answer to provide a workable script and delete this sentence. :)