How to make vim correctly display the errors reported by jq?

Viewed 186

When I edit the json file through vim, I use jq to format it. However, if the json is incomplete, the error message given by jq will be filled into the editing area of vim. Examples are as follows:

Json file:

{
  "demo": nul
}

Execute instructions:

:%!jq .

result:

parse error: Invalid literal at line 3, column 0

How can I make vim automatically create a window to display jq error messages instead of directly covering my editing area? Every time such a situation occurs, the command must be executed: u This is very troublesome.

3 Answers

If your main concern is covered editing area, try this:

:!jq < %

As an immediately usable workaround, you can use :help :w_c:

:%w !jq .

which echoes the output of jq in the command-line without affecting the content of the buffer:

:h :w_c

You can use ShellFilterPost event to display error messages in a new window. Similar post: vim filters and stdout/stderr.

autocmd ShellFilterPost * call ShowFilterError()

function! ShowFilterError()
  if v:shell_error 
    w! /tmp/filter-error
    split /tmp/filter-error
    wincmd p 
    u 
    wincmd p
  endif
endfunction
Related