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
!
default-buffer
? Besides, you really should let-binddefault-directory
, such as,(let ((default-directory "~/.emacs.d/")) (call-interactively #'find-file))
, instead of modifyingdefault-directory
viasetq
orcd
. In my opinion,default-directory
should not be changed by user. – xuchunyang Sep 15 '18 at 19:40default-buffer
was a typo. Was supposed to bedefault-directory
. I have corrected the question. – Lorem Ipsum Sep 17 '18 at 12:16default-directory
isn't "reset" according to the buffer, but rather it is a buffer-local value, so it has an independent value in every buffer. FWIW I am awfully doubtful that what you're asking for is a good idea -- it goes against the expectations for the variable, and as such I strongly suspect (but am not certain) that you would encounter undesirable side-effects. Can you elaborate on the specific things which you find problematic with the default behaviour? – phils Sep 17 '18 at 14:33my-find-file
,my-write-file
, and maybe a few others might be all you need? If that's the case, your approach might be safer than trying to truly change the value ofdefault-directory
globally – Tyler Sep 17 '18 at 18:38