33

How would I go about using a variable in a vim shell command (one done with !) in a vimscript? For instance, something kind of like this: (just an example, not what I'm really trying to do)

function Ls(dir)
    !ls a:dir
endfunction

1 Answer 1

47

Use the execute command. Everything after it is an expression that evaluates to a string, which it then executes like a command you had typed in yourself.

function Ls(dir)
    execute '!ls ' . a:dir
endfunction

This says, "Evaluate the expression '!ls ' . a:dir and then execute it." The variable a:dir is expanded, the dot concatenates the two strings into '!ls whatever' and then that is executed as if you had typed it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.