0

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)

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

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)
    return
  endif

  " 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
      break
    endif
    let idx += 1
  endwhile

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

  " open qf, jump to next error line
  copen
  exe next_idx
endfunction

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

0

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':1}), 'idx', 0)+1})
command! Csilentprev call setqflist([], 'a', {'idx': get(getqflist({'idx':1}), 'idx', 0)-1})

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.