If I:

  • Start Emacs in my home directory (~)
  • Find a file in the ~/Projects/ruby-play directory with C-x C-f
  • Try to find another file with C-x C-f

The default directory in the file finder will be ~/Projects/ruby-play.

What I'd like to see is the default directory being ~ still.

Thank you very much!

P. S. Can I maybe use some external package for it? Will Projectile do it for me, for instance?

P. P. S. Since, apparently, I wasn't clear enough... I don't want Emacs to return to my home directory every time. I wish to preserve any directory I need (e. g., if I start Emacs in the Project directory, I want the file finder to start in the Project directory as well).

  • 1
    I don't know how to change this behaviour, but if you used Ido mode, then selecting the home directory when looking for a file to open would only require typing ~/.
    – wvxvw
    Commented Apr 30, 2015 at 9:25
  • @wvxvw Thanks! However, I'd like to have similar behavior if I, say, launched Emacs from the Projects directory (i.e., having the default directory always be Projects). Commented Apr 30, 2015 at 9:27
  • Would it be a solution to save the default directory in your init file to custom variable and then later access that custom variable instead of default-directory? Commented Apr 30, 2015 at 10:01
  • @HåkonHægland I'm sorry, I'm an Emacs newbie. How can I reassign the file finder dialogue to a custom directory? Commented Apr 30, 2015 at 10:03
  • Keep in mind that doing such a thing will break every single package relying on this very behaviour. I tried it out for fun and it broke smart-mode-line because it was using vc which relies on it for its widget to work normally. Please consider either using a package giving you a simple way to find a file in your home directory or something like projectile to do things from the project root.
    – wasamasa
    Commented Apr 30, 2015 at 15:55

1 Answer 1


default-directory is a buffer local variable which can be set by all sorts of things although it is always set on loading a file. The simplest dumb solution is to use the find-file-hook and set it back to what you want:

(add-hook 'find-file-hook #'(lambda () (setq default-directory (expand-file-name "~/"))))

However this does have the disadvantage of always setting it to your home directory. More complex tricks are possible, so for example I have a hook in my mailer to set the directory to the relevant source tree depending on the source of the email:

;; Set default directory when viewing messages
(defvar my-mailing-list-dir-mapping
  '( ("qemu-devel.nongnu.org" . "~/lsrc/qemu/qemu.git/")
     ("kvmarm.lists.cs.columbia.edu" . "~/lsrc/kvm/linux.git/") )
  "Mapping from mailing lists to source tree.")

(defvar my-maildir-mapping
  '( ("linaro/virtualization/.qemu" . "~/lsrc/qemu/qemu.git/")
     ("linaro/kernel" . "~/lsrc/kvm/linux.git/") )
  "Mapping from maildirs to source tree.")

(defun my-set-view-directory ()
  "Switch the `default-directory' depending on the mailing list we
  are in."
  (let ((list (mu4e-message-field (mu4e-message-at-point)
        (maildir (mu4e-message-field (mu4e-message-at-point)
    (setq default-directory
            (assoc-default list my-mailing-list-dir-mapping)
            (assoc-default maildir my-maildir-mapping 'string-match)

(add-hook 'mu4e-view-mode-hook 'my-set-view-directory)
  • However this does have the disadvantage of always setting it to your home directory. There really isn't anything stopping me from saving the project name in a variable though, right? Commented Apr 30, 2015 at 10:27
  • @art-solopov: of course not, just make sure it's declared as buffer local. Most of the project management extensions will already have some sort of project-root variable that points to the top of the tree already.
    – stsquad
    Commented Apr 30, 2015 at 10:35
  • Thank you @stsquad. I probably should to that before I try to implement a monstrosity of my own. =) Commented Apr 30, 2015 at 10:48

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.