I've got a pretty large log file with some info interleaved with some errors / warnings and I would like to go to the next error in my quickfix window but without jumping to that location in the code.

At the moment I've got a dirty hack that basically consist in doing :cnext <C-o> but that's not ideal.

Any cleaner alternative ? I was thinking about something with getqflist and setqflist to get the current position and increment it, but I have only managed to go to the next line in the log (Which might just be plain text), whereas I want the next error. (So I probably need to iterate over the valid entries in the quickfix window but I haven't figured out how to do that yet)

2 Answers 2

function! s:next_error()
  let wv = winsaveview()
  keepjumps cnext
  call winrestview(wv)

the only problem of your approach is that it changed the :h jumplist , you can use :h :keepjumps to avoid that. But your cursor position still get changed after keepjump cnext, you window might also get scrolled, to restore that, use :h winsaveview() and :h winrestview() .

It's unpleasent to do this with getqflist():

function! s:next_error2()

  " return if current qflist empty
  let qfl = getqflist()
  if empty(qfl)

  " find next valid error
  let idx = getqflist({'idx':0}).idx
  let next_idx = 0
  while idx < len(qfl)
    if qfl[idx].valid
      let next_idx = idx + 1
    let idx += 1

  " return if there has no next valid error
  if !next_idx

  " open qf, jump to next error line
  exe next_idx

Note that this approach jump to next valid error, i'm not sure if it's exactly the same as cnext.


If you have a new enough version of Vim which supports getqflist()/setqflist() with the idx option then you can directly change the current index without jumping.

Example changing the index to 10:

:call setqflist([], 'a', {'idx': 10})

This can be wrapped into a commands to make it easier:

command! Csilentnext call setqflist([], 'a', {'idx': get(getqflist({'idx':0}), 'idx', 0)+1})
command! Csilentprev call setqflist([], 'a', {'idx': get(getqflist({'idx':0}), 'idx', 0)-1})
  • 1
    Those get(getqflist({'idx':1})s should be get(getqflist({'idx':0}) or else it only ever returns the index for the 1st value in the dictionary (namely: 1). Sep 20, 2021 at 6:58
  • Not only that, but this just scrolls through the lines in the quickfix window. It doesn't jump to the next line that's actually been matched as a quickfix error/warning/etc. Sep 20, 2021 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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