2

I have a variable that I would set to the current result I get from :pwd.

I tried the following, which obviously doesn't work:

:let b:projectroot = expand("pwd")

But this is essentially what I want to do. I want to switch to a directory using :cd and set that as the value for the variable b:projectroot.

2

The usual way to get the printed output of an Ex command such as :pwd is to use the execute() function.

However, in this specific case, you can get the current directory in a more straightforward way by simply calling getcwd():

let b:projectroot = getcwd()

The issue with execute() in this particular case is that you get leading whitespace in your result, so you also need to use trim() (or another similar string cleanup function) to get only the directory name:

let b:projectroot = trim(execute('pwd'))
|improve this answer|||||
  • 1
    You just saved me a lot of time with the trim part, thanks! – mbl Dec 26 '19 at 10:06

Your Answer

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.