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.

1 Answer 1

4

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'))
1
  • 1
    You just saved me a lot of time with the trim part, thanks!
    – mbl
    Dec 26, 2019 at 10:06

Your Answer

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

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