Best way to read shell command output

In Vim, What is the best (portable and fastest) way to read shell command output? This output can be binary and therefore contain zeros and (not) have a value for a new line. Current solutions that I see:

  • Use system() . Problems: does not work with NULL.
  • Use :read ! . Problems: do not save the final new line, trying to be an intelligent way to determine the output format (dos / unix / mac).
  • Use ! redirected to a temporary file, then readfile(, "b") to read it. Problems: two calls to the fs parameter, shellredir also redirects stderr by default, and it should be less portable ( 'shellredir' is mentioned here because it can be set to a valid value).
  • Use system() and filter the outputs via xxd . Problems: very slow, less portable (for pipes not equivalent to 'shellredir' ).

Any other ideas?

+8
vim shell shellexecute
source share
1 answer

You are using the text editor. If you care about NULs, trailing EOLs and (possibly) conflicting encodings, should you still use a hex editor?

If I need this amount of control over my operations, I use the xxd route really, with

 :se binary 

One nice option that you seem to miss is inserting the register of the insert expression:

Cr =system('ls -l') Enter

It may or may not be smarter / less intrusive in the character encoding business, but you can try it if it's important enough for you.

Or you can use Perl or Python support to use popen effectively

Tough idea:

 :perl open(F, "ls /tmp/ |"); my @lines = (<F>); $curbuf->Append(0, @lines) 
+4
source share

All Articles