Summary
In [Neo]Vim, if I call an external command on the current visual selection (expecting that command to transform the selected text, which Vim then updates in the buffer), and the command fails with a nonzero exit value, then the error message on the command's stderr isn't displayed. How can I get it to display automatically?
Background
It's easy to manually make a visual selection, then call an external command and have Vim use the stdout from that to replace the selected text.
xnoremap <Leader>d :!mycommand<CR>
This defines a key mapping, so that pressing leader key then 'd' runs external script 'mycommand' on the visual selection, as described above. The use of 'xnoremap', as opposed to other '-remap' commands, means this only happens when a visual selection currently exists, automatically passing the selected text to the command, etc.
Problem
But if mycommand fails (for example, perhaps the text in the visual selection is not formatted as mycommand expects), then mycommand will print nothing on stdout, an error message on stderr, and give a nonzero exit value. In this case, my Neovim correctly leaves the buffer unmodified, but does not display the error message. Instead, it just shows:
:'<,'>!mycommand
shell returned 1
Current kludgy workaround
I've fallen back on what seems like a bit of a kludge:
function! s:MyFunc() range
silent '<,'>!mycommand 2>/tmp/vim.err
if v:shell_error
echo join(readfile("/tmp/vim.err"), "\n")
endif
endfunction
xnoremap <Leader>d :call <sid>MyFunc()<CR>
No doubt there are more sensible temp storage locations I could use (proper tempfiles, variables, registers, etc?)
But, is this even necessary? Am I going to have to do it for every external program I call as a filter while wanting to see the error message? Am I going to end up generalizing the above function to operate on arbitrary external commands, and arbitrary ranges?
This seems like such a predictable and common need that I feel as though I must be missing something obvious that would automatically do this for me, but I haven't figure it from my web searches, other stackoverflow answers, nor the Vim docs around "help !".
