I want a "default directory" which is constant across all contexts (buffers, packages, etc). That is, when working on multiple files, opening a new terminal, doing anything which requires a starting directory, I want that starting directory to always be the same.
How can I set a global default directory without having to write an arbitrary number of wrapper functions?
To illustrate the problem, suppose my project has the following structure:
.
└───my-project
│ bar.py
│ foo.py
│
└───tests
test_bar.py
test_foo.py
If I'm working on test_foo.py
and I do find-file
(C-x C-f
) to
open foo.py
, the prompt will start at my-project/tests
. I have to
go up a level. Now suppose I'm editing foo.py
and I want to look at
test_bar.py
. When I do find-file
within the foo.py
buffer, the
prompt will be at my-project
. I need to navigate to
tests
. Similar things happen when using eshell
and other
applications which have a default directory.
According to this, this, and this, the starting directory is handled
by default-directory
. They all indicate that to change the starting
directory, you need to do something like,
(setq default-directory "/my/default/path")
or
(cd "/my/default/path")
The trouble is, default-directory
is buffer local. You need to constantly re-assign default-directory
.
In the case of find-file
, I overcome this by creating a wrapper
which changes the directory to the default before calling find-file
. I then reassign the keybinding:
(defun my-set-global-default-directory (new-default-directory)
"Set my-global-default-directory to NEW-DEFAULT-DIRECTORY."
(interactive "DNew global default directory: ")
(setq my-global-default-directory new-default-directory))
(defun my-find-file ()
(interactive)
(cd my-global-default-directory)
(call-interactively 'find-file))
(global-set-key (kbd "C-x C-f") 'my-find-file)
This works for find-file
, but for this to work universally, I would
need to manually wrap any function that uses default-directory
!