1

I can catch the output of running a system command via system() or systemlist(), but that actually gives me the result of stdout and stderr combined. As I am only interested in stdout (and may use stderr for other purposes), this isn't useful for me.

How can I separate the output of a system command between stdout and stderr?

  • You're mixing up the in / out / err names quite a bit (but I think I understand what you mean). Please do some proofreading and correct the references to stdin. – Ingo Karkat Nov 22 '19 at 12:01
  • @IngoKarkat Sorry, you are right, of course. I have fixed the question. – radlan Nov 22 '19 at 14:10
  • 1
    use job_start – Mass Nov 27 '19 at 2:26
3

Vim's system() indeed captures both standard out and standard error by default. If you just want one of those, just use the normal shell means to redirect the other to the null device.

For example, ignore stderr (2>) with Bash:

:echo system('grep root /etc/passwd /doesnotexist 2>/dev/null')

On Windows, you'd use 2>NUL instead.

Alternatively, you could (temporarily) modify the 'shellredir' setting; e.g. by dropping the 2>&1 from it to ignore stderr. (Thanks @ChristianBrabandt for the tip!)

separate stdout and stderr

If you need both (but separately), that's a bit tricky. You could:

  • redirect one / both into a temp file and read that via readfile(), or via another system('cat /tmpfile')
  • invoke the command twice, ignoring first stdout and then stderr
  • add prefixes (via sed) to one / both (possible with fancy piping and redirections), read all in one fell swoop, and separate inside Vim
| improve this answer | |
  • 1
    Urgh, that's ugly. Being dependend on the operating system and additionally on the shell doesn't sound nice. The alternatives to separate stdout/stderr for using both also isn't funny, but I think your first approach for is doable without too much effort and still usable. What I dislike even more is the system dependency… As this seems to be the only way to achieve this, I accept your answer. Thank you very much! – radlan Nov 22 '19 at 14:17
  • No, it only caputes stderr, if the shellredir option contains 2>%1 and this depends on the shell option. So simply setting :set srr=> should fix capturing the error – Christian Brabandt Nov 22 '19 at 16:18
  • Well, external commands naturally depend on the environment. In practice, most Unix/Linux shells are very similar (unless you're using an exotic one), and if you use Cygwin or WSL on Windows, you could even get away with a single implementation. – Ingo Karkat Nov 22 '19 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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