I'm writing small liniting plugin and I want to show errors in the quickfix as soon as errorfile changes.

fu! OpenErrors(job_id, data, event)
    let l:winid = win_getid()
    let l:output = split(a:data[0])
    echom l:output[0] . l:output[2]
    let l:view = winsaveview()
    exe 'silent! cfile! ' . escape(l:output[0] . l:output[2], '%#/')
    call winrestview(l:view)
    if len(getqflist()) > 0
    call win_gotoid(l:winid)

The problems with this code is that it the cursor jumps to quickfix window and it's annoying when errorfile changes fast.


In Build-Tools-Wrapper, the callback receives the new line to display (as the job simply executes make (or equivalent) and doesn't play with any intermediary file), the line is processed with :caddexpr. If I'm supposed to be at the bottom I simply execute :cbottom -- moving the cursor (in the qf windows) changes that. These two commands don't change the current window I'm in.

Also when I start the compilation, I open the quickfix window without jumping to it. Actually, I jump to it in order to apply a few hooks (like syntax highlighting, concealment...) and eventually I jump back to the window I was in before starting the background compilation. Along the way I've an old trick able to detect if we need a qf window in other situations.

  • I'm doing this to display quickfix only if the output of a linter changed. This way If I want to ignore errors I just close quickfix and until output is changed it wouldn't be opened. I guess I could clean quickfix maybe and add to it line by line from a file, but this sounds stupid. – user1685095 Aug 8 '17 at 12:00
  • I can close the qf window while I compile and open it back when the job is finished. It's not incompatible. – Luc Hermitte Aug 8 '17 at 12:38
  • I don't understand what you are talking about unfortunately. I want to open quickfix only if linter results changed. I also don't want my cursor to jump into quickfix window to show it. How can I achieve that? – user1685095 Aug 8 '17 at 13:45
  • When I compile something in the background. make is running and feeds its result back though job_start() callbacks. On each line fed back, I use caddexpr which doesn't trigger to qf-window to open. You can check it by yourself with :cclose + :caddexpr expand('%').":1: Error: test". This means that when I run :Make+:cclose, I can do my :COpen any time I want. – Luc Hermitte Aug 8 '17 at 14:06
  • In your case, you can simply close the qf-window whenever you want, and force it to pop-open when there is something new to feed with :caddexpr. You'll want to do it with :cwindow I guess, or some other trick (see my code) that keep the cursor where it was. – Luc Hermitte Aug 8 '17 at 14:06

Try this quickfix preview plugin: https://github.com/ronakg/quickr-preview.vim

enter image description here

  • Not sure how this should help me, but introducing dependency for my plugin isn't a good idea. – user1685095 Aug 8 '17 at 13:37

Here is what I ended up using to prevent the cursor from moving after executing copen:

function! OpenErrors(job_id, data, event)
    " Store the original window number
    let l:winnr = winnr()

    " Open a window to show the current list of errors

    " If focus changed, jump to the last window
    if l:winnr !=# winnr()
        wincmd p

Also, the vim-qf plugin makes it easier to work with the location/quickfix list.

  • Well, that's the same as what I do with win_gotoid. – user1685095 Aug 20 '17 at 7:02

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.