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

5

10

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.

Buffy answered 8/2, 2013 at 18:1 Comment(0)
B
4

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.

Belleslettres answered 8/2, 2013 at 20:5 Comment(3)
Syntastic will echo the first error message to the command line, but often the message is truncated; hence I want to jump to the error in the location list, myself. The fact that Syntastic can retrieve errors message based on the current line in the buffer offers hope that it is possible to jump from the buffer to the corresponding error line in the location list.Buffy
There's no command for that, unfortunately. You should post a feature request on Syntastic's issue tracker.Belleslettres
If you set your command line to use 2 lines it will use them both for the messages which helps a lot with truncating. "set cmdheight=2"Timekeeper
E
9

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.

Emalia answered 19/2, 2013 at 12:11 Comment(0)
B
4

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.

Belleslettres answered 8/2, 2013 at 20:5 Comment(3)
Syntastic will echo the first error message to the command line, but often the message is truncated; hence I want to jump to the error in the location list, myself. The fact that Syntastic can retrieve errors message based on the current line in the buffer offers hope that it is possible to jump from the buffer to the corresponding error line in the location list.Buffy
There's no command for that, unfortunately. You should post a feature request on Syntastic's issue tracker.Belleslettres
If you set your command line to use 2 lines it will use them both for the messages which helps a lot with truncating. "set cmdheight=2"Timekeeper
R
2

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()
Rabbi answered 15/10, 2017 at 10:55 Comment(1)
Thanks for this, it helped me initially.Deadradeadweight
P
1

: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.

Preinstruct answered 17/9, 2021 at 14:37 Comment(0)
D
0

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.

Deadradeadweight answered 16/6, 2019 at 22:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.