Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the default directory of emacs with 'cocoa emacs'

Tags:

emacs

macos

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?

like image 447
prosseek Avatar asked Nov 28 '22 11:11

prosseek


2 Answers

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")
like image 139
Jim Blandy Avatar answered Dec 06 '22 22:12

Jim Blandy


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.

like image 33
Trey Jackson Avatar answered Dec 06 '22 22:12

Trey Jackson