0

I found that using the :pwd command is the same as using the :echo getcwd() command in the commandline mode of vim. Is pwd the same as cwd? Is there a difference between the two? As follows:

I am currently in the directtory: /tmp/xyz

$ pwd
/tmp/xyz

The /tmp/xyz directory structure :

$ find /tmp/xyz
/tmp/xyz
/tmp/xyz/dir1
/tmp/xyz/dir1/file1
/tmp/xyz/file

I open /tmp/xyz/file file via vim in /tmp/xyz directory:

$ vim /tmp/xyz/file

Then I execute :pwd command in the vim window that i just opened:

:pwd

The result is : /tmp/xyz

And then to execute :echo getcwd() command :

:echo getcwd()

The result is: /tmp/xyz

At this time, it seems that pwd and cwd are the same.

Then i use the same vim window to open /tmp/xyz/dir1/file1 ,and to execoute :pwd and :echo getcwd() commands :

To open /tmp/xyz/dir1/file1:

:e /tmp/xyz/dir1/file1

To execute :pwd :

   :pwd

The result is: /tmp/xyz

To execute :echo getcwd() :

  :echo getcwd()

The result is still : /tmp/xyz ; why is the result here not /tmp/xyz/dir1/ ?

Why is the value of pwd and cwd the same regardless of which file is opened?

  • This doesn't answer the question but might solve your problem: :echo resolve(expand('%:p')) – Bananach Jan 23 at 12:44
1

Yup, they are the same (but getcwd() can accepts some arguments so it is useful for Vim scripting), both return the current working directory. By default it is the directory where you executed Vim. To change it you need to execute :cd.

  • 3
    Also you can change the current working directory for the current window with :lcd. – Ralf Jan 23 at 15: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.