0

I want to:

let s:asciiart = ["", "", s:hardinclude(system("command params")), "", ""]

where command returns one string with:

"art-line-1", "art-line-2", "art-line-3"

so I end up with:

s:asciiart == ["", "", "art-line-1", "art-line-2", "art-line-3", "", ""]

Is there a way to make a working s:hardinclude()? How? Thanks.

4
  • Welcome to Vi and Vim! Are you perhaps looking for systemlist() ? To be honest, your question is unclear—you've not explained how (algorithmically, not programatically, of course) to get from your input to your output...
    – D. Ben Knoble
    Dec 2, 2019 at 3:20
  • I have contoured this issue by having my "command" return a csv, then splitting the string return value inside vim script environment. So it is solved. I am fairly new to SE, how should this question be marked?
    – fde-capu
    Dec 2, 2019 at 3:55
  • Welp, if you have a solution, post the answer (self-answering is quite alright!) You might be interested in help center or How to Ask
    – D. Ben Knoble
    Dec 2, 2019 at 4:16
  • @fde-capu The answer using eval() is the one you were looking for. But note that's usually a bad idea, generating a Vim snippet from an external command and evaluating it as Vimscript. Instead, generate your output in a more general format (multiple lines, or comma-separated, or tab-separated) and parse that simple format in your Vim code.
    – filbranden
    Dec 2, 2019 at 16:41

4 Answers 4

2

Version 8.2.935 introduced a flatten() function (and version 8.2.937 fixed a bug in that function). With this function you can also write

let s:asciiart = flatten(["", "", systemlist("command params"), "", ""])

to insert the elements of the inner list into the outer list.

2

If you can guarantee your original format, you can use

split(system(command), ',\s\+')

But CSV (as in the self-answer) is probably cleanest.

Alternatively, have the outputs on their own lines and use

systemlist(command)

Vim unfortunately doesn't have an "explode" operator that I am aware of.

1

As an alternative, you can use :h eval(). For example:

let s:asciiart = eval('["", "", ' . trim(system("command args")) . ', "", ""]')

Note that system() output includes extra newline(s), so you have to deal with it.

0

Although it would still be fine to find out if a "hard-include" would be possible, I answer my own question:

I have changed my command to return a CSV, as:

art-1,art-2,art3

then, from the vim script:

let s:asciiart = ["", ""] + split(system("command args"), ",") + ["", ""]

...well, solved the problem, but did not exactly answered the question. Not sure if should be marked as solved.

1
  • Better yet, return one item per line and use systemlist() to get the result, that already does the split for you as well. Plus, if these are lines of asciiart, then printing it in multiple lines means it will actually look like asciiart already, no? 😁
    – filbranden
    Dec 2, 2019 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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