Calling the function system("<command>") or systemlist("<command>") retrieves the stdout output from a shell call to <command>. How can I retrieve the exit status of that command?

For example, I would like to do something like this:

let output =  system_call_with_exit_status("my_shell_command")
let stdout = output[0]
let exit_status = output[1]
if (exit_status != 0)
    echo output

The intention is to print the output from my_shell_command only if the command gives a non-zero exit status.

  • 7
    :h v:shell_error Commented Apr 28, 2019 at 16:43
  • @ChristianBrabandt Thanks, that works.
    – Jasha
    Commented Apr 28, 2019 at 16:57
  • @Jasha consider self-answering if youve got it figured out now
    – D. Ben Knoble
    Commented Apr 28, 2019 at 17:06
  • @D.BenKnoble Ok, will do.
    – Jasha
    Commented Apr 28, 2019 at 17:08

2 Answers 2


Mentioned in the comments above, the v:shell_error variable gives the result of the last shell command. So the exit status can be tested as follows:

let output = system("my_shell_command")
if v:shell_error != 0
    echo output

If you use Lua, the following utility function also helps:

function shell_error()
  return vim.v.shell_error ~= 0

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.