I am trying to write a function to lint buffers in neovim. It seems that the usual recommendation for achieving such a thing is to run the command over the entire range (ie %!lint
). This is however, problematic if the file is not properly formatted. In (neo)vim's current behaviour, it appears that even stderr output from the lint program will replace the entire buffer.
I have attempted to fix this by running the lint command separately and checking for shell errors, like so:
" example usage:
" :call RunBuf("python -m json.tool")
function! RunBuf(cmd) abort
redir => output
silent execute ":w !" . a:cmd
redir END
if v:shell_error
echo output
return
endif
let pos = getcurpos()
execute "%!" . a:cmd
call setpos('.', pos)
endfunction
This is still not optimal as the lint command needs to be executed twice. I am thinking if there is a way to replace the entire buffer with output
, or if there's a simpler way of achieving the same?