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
job_start