In neovim, I created this very simple command to run my javascript code through prettierd on save.

:command! Prettier normal mz:%!prettierd %<CR>'zzz

au BufWritePre *.js :Prettier

This works reasonably well (and compared to the plugins I have tried so far, this only touches the file once instead of twice which has quite an impact on the feedback loop of my tests).

But if the file has some serious issues, e.g. mismatching brackets or the like, pretterd exits with a non-zero exit code and an error message, which now replaces my buffer contents.

Can I get the filter to keep the original content on non-zero exit codes? Or do I need to handle that in shell scripts?

  • Hi, welcome. You could try to redirect the error stream with prettier 2> /dev/null. However, I'm not quite sure your question belongs to this site as it's not about using vim per se.
    – Biggybi
    Mar 22, 2022 at 12:09
  • wrap it into a shell script, that always returns true Mar 22, 2022 at 12:52
  • @Biggybi I would argue that it is a question about vim, as my question is primarily about, can I get vim to not replace my buffer with an error message when the script fails. If the answer to that question is "no", the answer is still an answer to a vim question (but not the answer I'm hoping for). In that case, I will need to proceed in a non-vim way.
    – Pete
    Mar 22, 2022 at 13:49

1 Answer 1


You can check the exit code of an external command spawned with ! in the v:shell_error special variable.

For instance, you can use that to check the error status of the external command and then use the :undo command to revert the filter action.

This is easier to do in a function:

function! Prettier()
  let save_cursor = getcurpos()
  silent %!prettierd %
  if v:shell_error != 0
    echomsg 'Prettier failed.'
  call setpos('.', save_cursor)

au BufWritePre *.js call Prettier()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.