2

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 Apr 30 '15 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). – art-solopov Apr 30 '15 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? – Håkon Hægland Apr 30 '15 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? – art-solopov Apr 30 '15 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 Apr 30 '15 at 15:55
5

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."
  (interactive)
  (let ((list (mu4e-message-field (mu4e-message-at-point)
                                  :mailing-list))
        (maildir (mu4e-message-field (mu4e-message-at-point)
                                     :maildir)))
    (setq default-directory
          (expand-file-name
           (or
            (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? – art-solopov Apr 30 '15 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 Apr 30 '15 at 10:35
  • Thank you @stsquad. I probably should to that before I try to implement a monstrosity of my own. =) – art-solopov Apr 30 '15 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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