The reason why you're seeing the command you just ran coming back in the channel is because Windows' cmd.exe
will by default run with “echo” on, which means it will print back the command to be executed before executing it and sending back the output produced by it.
You can use job_start("cmd /q")
to have it disable “echo” at startup, which should solve the problem and have your read from the channel return the first line produced by the dir
command. See documentation for cmd.exe
at Microsoft's website.
Another alternative is using, for example, bash.exe
from a distribution such as Git Bash, which will not produce the “echo” you've seen with cmd
.
Note that a read from the channel will only produce the first line of output from dir
and further channel reads don't seem to send the next lines of output from that command. I haven't really used Vim jobs and channels extensively, so I'm not really sure what the issue there is. You might want to ask a separate question about that if you'd like to explore that further...
If all you want is to capture the output of an external command, a much easier approach is to use the system()
or systemlist()
functions, which will take care of all the details for you and simply return a string or list. For example, to put the output of dir
under the cursor in the current buffer, all you need is:
:put =systemlist('dir')
"dir"
itself if you run"cmd /k"
instead? I think what you're seeing iscmd
's "echo", which I believe is somewhat common in DOS/Windows (at least I recall having to start my.BAT
files with@echo off
back in the day...) See docs.microsoft.com/en-us/windows-server/administration/…dir
when I runcmd /k
instead. I also get the same result if I replace cmd.exe with powershell.exe, anddir
withls
dir
withls
, then I get the first line of output ofls
cmd /q
(for quiet.) I think it's normal that you only get the first line of output... I'm not super familiar with jobs and channels, but that's what I'm getting too. If you just want to run an external command and get the output, usingsystemlist()
is much more straightforward!cmd /q
works -- I getVolume in drive C is OS
instead ofdir