Is is possible to get the original working directory of vim after changing the local working directory?

Let's say I open vim with parent directory:


and then I open a file and change the working directory:


autocmd BufEnter * silent! lcd %:p:h

GOAL: I want to get the directory name:




Is this possible?

2 Answers 2



If your project is under git's control then you use Fugitive.vim.

:echo fugitive#repo().tree()

To edit a file relative to the git root you an use :Gedit and a /:

:Gedit /tasks/mytask.js

For more help see:

:h fugitive
:h :Gedit


If you are using Projectionist.vim then you can get the inner most directory by doing the following:

:echo projectionist#path()

Projectionist also provides :Cd to change the directory from the project root. :ProjectDo {cmd} will run command, {cmd}, from the project root.

For more help see:

:h projectionist
:h :Cd
:h :ProjectDo
:h projectionist#path()

Low-tech solution

Forget "auto-cd-ing" to the current file's directory. Just set your current path to your project's root. Use a nice fuzzyfinder like CtrlP to navigate your files.

You can use %:h to get the current file's directory:

:e %:h

For more help see:

:h 'autochdir'
:h c_%
:h filename-modifiers


Personally I use projectionist for my project needs, however I also keep the current working directory at my project root and never change it. This keeps things simple.

If you do insist on "auto-cd-ing" then I would recommend you look into 'autochdir. See :h 'autochdir'.

:echo system("pwd")

should give you the information you want.

  • Not sure if I have a plugin that is interfering or not. But it's just returning the same info if I did plain pwd. I just ended up creating a global variable store the first opened file. let g:projectDir = get(g:, 'projectDir', expand('%:p:h'))
    – Jon49
    Dec 2, 2015 at 20:54
  • When Vim runs an external command, either through :! or system(), it uses the working directory that was set via :cd or :lcd, so this won't work.
    – jamessan
    Dec 4, 2015 at 13:35

Your Answer

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

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