How can vimscript replace the output of an external command with a variable?

Asked 2 years ago, Updated 2 years ago, 140 views

Looking for a way to substitute the output of an external command to a variable in vimscript.

For example, you want to execute the ls command in vimscript and substitute the result to the variable.
I executed the code below, but

redir=>message
execute":!ls"
redir END

echo message

The message variable is substituted by the ":!ls" command itself and
The ls output was not substituted.
How do I substitute the message variable for the output of ls?

Thank you for your cooperation

vim

2022-09-30 21:26

1 Answers

You can use the system() function.

let message=system('ls')


2022-09-30 21:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.