You're close. When you want to use a variable in an Ex command you need to use :execute
. This command takes a Vim expression as arguments, evaluates the expression, and runs the resulting string as an Ex command. During this process variables are resolved into their values.
Before evaluation all of the arguments are concatenated together. :execute
inserts spaces between the arguments. You can also do the concatenation yourself and pass a single string argument. (You may want to do this if you don't want the just noted space separation.)
For your case you have some fixed/static text ("read !ls"
) and one variable. For the multiple argument approach you could use either one of these:
exe "r !ls" a:var1
exe "r" "!ls" a:var1
The single string argument approach would look like this:
exe "r !ls " . a:var1
Notice how the .
(string concat) operator joins the command string with the variable. In this case the variable is resolved into it's value before it's passed to :exe
but the end result is the same.