Jump to the errors in the quickfix or location list for the current line in Vim (with Syntastic)

StackOverflow https://stackoverflow.com/questions/14778612

  •  07-03-2022
  •  | 
  •  

Pergunta

I started using the Syntastic plugin for Vim, which will run a syntax checker on the current buffer and then indicate any lines which have errors. I can open up the list of errors as as a location list using :Errors, and then jump to the line of a given error by hitting Enter, which will jump to the line containing the error in my buffer.

I want to know how I can do the opposite. I want to go from a line in my buffer that is marked with having a syntax error to the corresponding entry in the location list, so that I can read the full error message in the list. How can I do this? I know that :ll [n] will jump to the nth error in the list, but often I will not know exactly which error number corresponds to the given line in the buffer. I cannot find a command that accepts a line number, rather than an error number, however.

Foi útil?

Solução 2

I think that it's not possible, at least with default Vim commands or Syntastic.

But Syntastic actually echoes the error message associated with the current line in your command-line. This feature is enabled by default.

Outras dicas

You're right, there's no built-in way to find out which error is at or after the current cursor position, though that would often be useful. I've written the QuickFixCurrentNumber plugin for that.

With the g<C-q> mapping, you can go to the item in the quickfix / location list for the current cursor position (or the next item after the cursor). It also offers [q / ]q mappings to jump to previous / next errors while limiting the navigation to errors in the current buffer.

I just created this for my :Man viewer. It tracks the current item in the 'locationlist' window when navigating:

function! s:visibleLoc()
   return len(filter(getwininfo(), {i,v -> v.loclist}))
endfunc

function! s:followLine()
   let curLine = line(".")
   if (exists("b:lastLine") && b:lastLine == curLine) || 0 == s:visibleLoc()
      return
   endif
   let b:lastLine = line(".")
   let ent = len(filter(getloclist("."), {i,v -> v.lnum <= curLine}))
   if ent < 1 || (exists("b:lastEntry") && b:lastEntry == ent)
      return
   endif
   let b:lastEntry = ent
   let pos = [ 0, curLine, col("."), 0 ]
   exe "ll ".ent
   call setpos(".", pos)
endfunc

au CursorMoved <buffer> call <SID>followLine()

I tried to get this feature 'in' here which resulted in:

vim-loclist-follow:
https://www.vim.org/scripts/script.php?script_id=5799
https://github.com/elbeardmorez/vim-loclist-follow

Nothing fancy, it just ensures the 'nearest' item is selected. Works for me (™️) using either of my Syntastic, or now Ale, setups.

:caf goes to the error after the cursor, which works pretty well if you don't mind shifting your cursor back and then using it.

You could probably make mappings to move your cursor back and then :caf but dealing with the edge cases like when error is the first character of the file might make it slightly complex.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top