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.

New contributor
fde-capu is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 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 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 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 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 at 16:41
1

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.

New contributor
fde-capu is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 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 at 16:39

Your Answer

fde-capu is a new contributor. Be nice, and check out our Code of Conduct.

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.