Last active
September 2, 2025 08:43
-
-
Save BoltsJ/5942ecac7f0b0e9811749ef6e19d2176 to your computer and use it in GitHub Desktop.
Automatically place signs based on the quickfix list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if exists('g:loaded_qfsign') | |
| finish | |
| endif | |
| let g:loaded_qfsign=1 | |
| sign define QFErr texthl=QFErrMarker text=E | |
| sign define QFWarn texthl=QFWarnMarker text=W | |
| sign define QFInfo texthl=QFInfoMarker text=I | |
| augroup qfsign | |
| autocmd! | |
| autocmd QuickFixCmdPre [^l]* call s:clear_signs() | |
| autocmd QuickFixCmdPost [^l]* call s:place_signs() | |
| augroup END | |
| nnoremap <Plug>(QfSignPlace) :silent call <SID>place_signs()<CR> | |
| nnoremap <Plug>(QfSignClear) :silent call <SID>clear_signs()<CR> | |
| let s:sign_count = 0 | |
| function! s:place_signs() abort | |
| let l:errors = getqflist() | |
| for l:error in l:errors | |
| if l:error.bufnr < 0 | |
| continue | |
| endif | |
| let s:sign_count = s:sign_count + 1 | |
| if l:error.type ==# 'E' | |
| let l:err_sign = 'sign place ' . s:sign_count | |
| \ . ' line=' . l:error.lnum | |
| \ . ' name=QFErr' | |
| \ . ' buffer=' . l:error.bufnr | |
| elseif l:error.type ==# 'W' | |
| let l:err_sign = 'sign place ' . s:sign_count | |
| \ . ' line=' . l:error.lnum | |
| \ . ' name=QFWarn' | |
| \ . ' buffer=' . l:error.bufnr | |
| else | |
| let l:err_sign = 'sign place ' . s:sign_count | |
| \ . ' line=' . l:error.lnum | |
| \ . ' name=QFInfo' | |
| \ . ' buffer=' . l:error.bufnr | |
| endif | |
| silent! execute l:err_sign | |
| endfor | |
| endfunction | |
| function! s:clear_signs() abort | |
| while s:sign_count > 0 | |
| execute 'sign unplace ' . s:sign_count | |
| let s:sign_count = s:sign_count - 1 | |
| endwhile | |
| redraw! | |
| endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is really neat! Thank you for sharing.
A few improvements I've made that I would like to share:
Filter out text unmatched by errorformat
In cases where linter/compiler output doesn't match
errorformatexactly,getqflist()still reports it, causing the unmatched output to be calculated/reported in the finalelsecontrol flow.For example, consider the following
tflint.vimcompiler configuration:And the following terraform file:
Running
:make %produces:Running
:echo getqflist()in vim produces (note the first 2 elements in the array):[ { "lnum": 0, "bufnr": 0, "end_lnum": 0, "pattern": "", "valid": 0, "vcol": 0, "nr": -1, "module": "", "type": "", "end_col": 0, "col": 0, "text": "2 issue(s) found:" }, { "lnum": 0, "bufnr": 0, "end_lnum": 0, "pattern": "", "valid": 0, "vcol": 0, "nr": -1, "module": "", "type": "", "end_col": 0, "col": 0, "text": "" }, { "lnum": 1, "bufnr": 1, "end_lnum": 0, "pattern": "", "valid": 1, "vcol": 0, "nr": -1, "module": "", "type": "W", "end_col": 0, "col": 1, "text": "Missing version constraint for provider \"\" in \"required_providers\" (terraform_required_providers)" }, { "lnum": 1, "bufnr": 1, "end_lnum": 0, "pattern": "", "valid": 1, "vcol": 0, "nr": -1, "module": "", "type": "W", "end_col": 0, "col": 1, "text": "data \"\" \"\" is declared but not used (terraform_unused_declarations)" } ]A simple fix is to check if
bufnr <= 0(is buffer number of0even possible?), like:Alternative, you can check if
lnumis0(because why/how would you show signs for something with no line number?), like:Sign Priority
Signs were not showing up in my
signcolumn. This was because Vim assigns a default priority of10for signs (:h sign-place). Priority helps Vim determine which sign to show when multiple signs are placed on the same line. This scenario happes easily & frequently with plugins likevim-signifyorvim-gitgutter.I fixed this by assigning a priority of
99to errors,98to warnings, and97to info, because I much rather see issues with code than git status indicators in mysigncolumn, like this:Note:
prioritymust be specified/assigned beforefile=orbuffer=.Support location lists
Surface counts to vimrc
I placed this script in my
~/.vim/plugin/directory and wanted to surface the count of errors/warnings/infos to show in my statusline.Then in my
vimrc:Everything put together: https://gist.github.com/pbnj/dc19a2d0f579933daef7f0fd9604dc0c