Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: how to redirect formatprg errors to echoerr?

In vim, I use external code formatters to beautify my code. Vim supports that. One can set the "formatprg" option to an external program that formats code. See :help formatprg. The formatprg takes your unformatted code on the stdin and puts the formatted code to the stdout.

Now, when the formatprg succeeds, everything works very well. But when something goes wrong, an error message is put to stderr. Unfortunately, vim doesn’t recognize this, and your unformatted code is replaced with the error message.

How can I redirect errors to vim's echoerr?

Thanks in advance.

like image 281
chtenb Avatar asked Feb 25 '26 01:02

chtenb


1 Answers

You can avoid that the error output is inserted into the buffer by

:set shellredir=>%s

but still, the text will be cleared, with litte indication that an error has occurred. You can easily :undo the bad modification (u), though.

This similar question shows a workaround, but to me this looks ugly, and I wonder why Vim does not evaluate the external program's exit code. (I'll go ask on vim_dev for the reasons behind this. Edit: Here it is)

like image 117
Ingo Karkat Avatar answered Feb 27 '26 18:02

Ingo Karkat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!