4

As explained in here, putting (setq default-directory "~/Desktop/mag" ) in .emacs is supposed to change the default directory.

When I do that with the emacs on my mac, it doesn't work. C-x C-f still shows ~/ not ~/Desktop/mag.

(cd "Users/smcho/Desktop/mag") also gives me this error - Error: No such directory found via CDPATH environment variable

What's wrong with them?

Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871
  • Is it possible you need a / at the front of the path in the `(cd "/Users/smcho/Desktop/mag")`? (No mac at my fingertips to check). Though I don't think this will have the effect you really want. – Trey Jackson Jul 29 '10 at 20:58

3 Answers3

15

The directory that appears in the prompt for C-x C-f ('find-file') comes from the value of default-directory, which is a buffer-local variable. When you first start Emacs, the initial buffer displayed is the GNU Emacs buffer. That buffer's default-directory is set from the variable command-line-default-directory.

So, try this:

(setq command-line-default-directory "~/Desktop/mag")
Jim Blandy
  • 1,536
  • 10
  • 17
6

The straight-forward answer to your question is:

(setq-default default-directory "~/Desktop/mag")

Reading the documentation for the variable (C-h v default-directory RET) you'll see:

Automatically becomes buffer-local when set in any fashion. This variable is safe as a file local variable if its value satisfies the predicate `stringp'.

That said, opening a file automatically sets the default-directory to the path of the file...

So, if you always want find-file to start at that directory, you can use this:

(global-set-key (kbd "C-x C-f") 'my-find-file)
(defun my-find-file ()
  "force a starting path"
  (interactive)
  (let ((default-directory "~/scratch/"))
    (call-interactively 'find-file)))

This question may be a duplicate of Preventing automatic change of default-directory. Though it's difficult to tell.

Community
  • 1
  • 1
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • I added the code in .emacs, but C-c C-f shows me the ~/ not ~/Desktop/mag. – prosseek Jul 29 '10 at 18:51
  • @prosseek You *always* want `C-c C-f` to start at `~/Desktop/mag`? That's slightly different, and I think already answered. I'll update my answer. – Trey Jackson Jul 29 '10 at 20:50
  • I'm sorry, I'm confused. C-x C-f was the key what I intended to use to open "~/Desktop/mag", but it's OK that I find a way to open it using C-c C-f. – prosseek Jul 29 '10 at 21:00
  • @prosseek - I'd wondered if it was `C-x C-f`, I'll update my answer. – Trey Jackson Jul 29 '10 at 22:04
3

In addition to the notes above regarding default-directory, I had to also prevent the emacs splash screen from starting in order to make subsequent commands like dired actually show their buffer when invoked from .emacs on startup:

   (setq inhibit-splash-screen t)
pnkfelix
  • 3,770
  • 29
  • 45