As of vim 8.0.1489, one may use getcwd(-1)
to retrieve the global directory.
Thus, the following will work to restore a window to the global directory and cause it to forget its local directory (as explained in the documentation):
if haslocaldir()
execute 'cd' getcwd(-1)
endif
This feature was available in neovim before it made it to vim. Prior to vim 8.0.1489, there was no simple way to determine the global directory (though there is haslocaldir()
to tell if a window has been issued an :lcd
). Two possible ways are mksession
and set cpo-=s
+new
. One practical option would be to wrap the :cd
command. For example,
au VimEnter * let g:my_project_dir = getcwd()
command! -nargs=? -complete=dir Cd execute 'cd' <q-args> | let g:my_project_dir=getcwd()
cnoreabbrev <expr> cd getcmdtype()==':' && getcmdline()=='cd' ? 'Cd' : 'cd'
When you want to restore a window to the global directory you can use
command! RestoreCwd execute 'cd' g:my_project_dir