I have this user defined command:

command! PS silent! w | PlugSnapshot! ~/.vim/plugins/plugins.lock

How could I change it to take the filepath from a variable, something like this:

let PathToPluginsFolder =  ~/.vim/plugins
command! PS silent! w | PlugSnapshot! %%%PathToPluginsFolder%%%/plugins.lock
  • @statox The question is different, and the answer could've been very different.
    – ruohola
    Jun 11, 2019 at 18:37
  • I don’t think these are different questions: these are two variations of “how can I use a variable in a vimscript expression?”. By marking this question as a duplicate I’m not arguing against its quality, I just want to make sure that future users can get an answer to their question and I also make sure that we keep together the related contents to give all the answers a better visibility. However you can vote to reopen the question and if the community agrees it will be reopened :)
    – statox
    Jun 11, 2019 at 20:48

1 Answer 1


Can simply use exec and . to build the command:

let PathToPluginsFolder =  ~/.vim/plugins
command! PS silent! exec ':w | PlugSnapshot! ' . PathToPluginsFolder . '/plugins.lock'

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