20

I know this has been posted many times, but the solutions are not working for me. I've tried the following solutions from this post (Changing the default folder in Emacs):

  • Solution 1: Add (cd "C:/Users/Name/Desktop") to the .emacs file
  • Solution 2: Add (setq default-directory "C:/Documents and Settings/USER NAME/Desktop/" ) to the .emacs file
  • Solution 3: Right click the emacs short cut, hit properties and change the start in field to the desired directory.

and only solution 3 is working. Unfortunately, I need to use either solution 1 or 2.

I believe that my .emacs file is correct, because my theme is being correctly loaded/unloaded when I add/remove (load-theme 'light-blue t) from the .emacs file.

Here is my MOST RECENT .emacs file:

(message "Default Dir: %S" default-directory)
(setq-default default-directory "C:/Users/Lucas/")
(message "Default Dir: %S" default-directory)
(setq-default indent-tabs-mode nil)
(add-hook 'ruby-mode-hook
      (lambda ()
        (define-key ruby-mode-map "\C-c#" 'comment-or-uncomment-region)
        )
 )  
(defadvice comment-or-uncomment-region (before slick-comment activate compile)
  "When called interactively with no active region, comment a single line instead."
  (interactive
   (if mark-active (list (region-beginning) (region-end))
     (list (line-beginning-position)
       (line-beginning-position 2)))))
(load-theme 'light-blue t)
;setq default-directory "~/")
(setq-default default-directory "C:/Users/Lucas/")

(defun xp-read-file-name (prompt &optional dir default-filename mustmatch initial predicate)
(setq last-nonmenu-event nil)
  (funcall (or read-file-name-function #'read-file-name-default)
           prompt dir default-filename mustmatch initial predicate))

(defun xp-save-as (filename &optional confirm)
  (interactive
   (list (if buffer-file-name
       (xp-read-file-name "Write file: " nil nil nil nil)
     (xp-read-file-name "Write file: " "C:/Users/Lucas/"
         (expand-file-name
          (file-name-nondirectory (buffer-name))
          "C:/Users/Lucas/")
         nil nil))
   (not current-prefix-arg)))
  (or (null filename) (string-equal filename "")
      (progn
  (if (file-directory-p filename)
      (setq filename (concat (file-name-as-directory filename)
           (file-name-nondirectory
            (or buffer-file-name (buffer-name))))))
  (and confirm
       (file-exists-p filename)
       (not (and (eq (framep-on-display) 'ns)
           (listp last-nonmenu-event)
           use-dialog-box))
       (or (y-or-n-p (format "File `%s' exists; overwrite? " filename))
     (error "Canceled")))
  (set-visited-file-name filename (not confirm))))
  (set-buffer-modified-p t)
  (and buffer-file-name
       (file-writable-p buffer-file-name)
       (setq buffer-read-only nil))
  (save-buffer)
  (vc-find-file-hook))
(defun comment-or-uncomment-region-or-line ()
    "Comments or uncomments the region or the current line if there's no active region."
    (interactive)
    (let (beg end)
        (if (region-active-p)
            (setq beg (region-beginning) end (region-end))
            (setq beg (line-beginning-position) end (line-end-position)))
        (comment-or-uncomment-region beg end)
        (next-line)))
(global-set-key (kbd "C-x C-;") 'comment-region)
(setq lazy-highlight-cleanup nil)
(message "Default Dir: %S" default-directory)

My emacs.exe is saved under C:\emacs\emacs-24.3\bin (which is also my default directory, but I want to change it to C:/Users/Lucas/), and my .emacs is under my home - C:\Users\Lucas.

UPDATE

I RECENTLY found out that my command (setq-default default-directory "C:/Users/Lucas/"), which should set my default-directory, is not performing. Immediately after startup, this is my *Messages* buffer:

Default Dir: "C:\\emacs\\emacs-24.3\\bin/" [3 times]
For information about GNU Emacs and the GNU system, type C-h C-a.

It behaves as if I am overriding the .emacs file. I double/triple checked, and I cannot find another .emacs in my path. Also, I am not running Emacs from a shortcut. Any suggestions would be appreciated. Thanks for all of the support.

Community
  • 1
  • 1
modulitos
  • 14,737
  • 16
  • 67
  • 110
  • ["Emacs will start in your desktop that way, unless you have a file open. It will usually start in the same directory as the file in your current buffer otherwise."](http://stackoverflow.com/a/65411/1309352) – Bleeding Fingers Jan 11 '14 at 10:15
  • What's the problem you're trying to solve? (i.e. why is the value of `default-directory` an issue?) – phils Jan 11 '14 at 10:54
  • I am trying to set the default working directory to `C:/Users/Lucas/`. My current setup seems to ignore the line `(setq default-directory "C:/Users/Lucas/")` from my `.emacs` file. – modulitos Jan 11 '14 at 11:44
  • Is your `HOME` environment variable set to a conflicting value? – SlowLearner Jan 11 '14 at 13:46
  • The fact that loading works is unrelated. There is a priori no connection between `load-path` and `default-directory` or, for that matter, `invocation-directory`. – Drew Jan 11 '14 at 18:17
  • @SlowLearner Under my `Environment Variables` (I'm using Windows 7), my HOME `System variable` is set to `C:\Users\Lucas`, which I believe is correct. – modulitos Jan 11 '14 at 21:52

7 Answers7

12

The problem for me was the Initial Start Up Screen of emacs.

It can be deactivated by:

;; no startup msg  
(setq inhibit-startup-message t)

It seems that the default welcome screen of emacs (with the tutorial links) forces you to start in your $HOME path.


After that you can either cd or setq default-directory like so:

(setq default-directory "~/")

or

(cd "~/")
DrDirk
  • 1,937
  • 3
  • 25
  • 36
  • 2
    Yes, you are right. I tried all other things mentioned above and none worked. If I started emacs from the search prompt, the default directory was System32. If I started from the shortcut on the desktop (to runemacs.com) it was opening the bin folder where emacs is installed. After disabling the Initial Startup Screen, the default directory worked. I uad to use (setq default-directory "C:\\Path\\To\\My Folder") – Nick_F Dec 04 '17 at 08:05
9

From what you write, it sounds like you are running windows. Have you verified that emacs is actually finding and loading .emacs from the directory C:\Users\Lucas? If it is, that means Windows understands your home directory is C:\Users\Lucas. You can check that by looking at the environment variable HOME. In emacs, try:

(getenv "HOME")

In your .emacs file, you could then put:

(setq default-directory "~/")
elemakil
  • 3,681
  • 28
  • 53
Jeffrey DeLeo
  • 1,672
  • 13
  • 19
  • This sounds like a good check - but how do I run `(getenv "HOME")`? Should I put `(getenv "HOME")` in my init (`.emacs`) file, or is there a way to run this straight from my editor as Lisp code? (Sorry, I am a bit new to Emacs) I eventually plan to use the `~/` directory to make my init file work across platforms. – modulitos Jan 11 '14 at 21:33
  • Also, I believe my HOME system variable is correctly set anyway. When I run `C-x C-f ~/.emacs` I am taken to my `.emacs` file under `c:/Users/Lucas/.emacs`. – modulitos Jan 11 '14 at 22:42
  • 2
    In emacs, go to the scratch buffer. It is in lisp interaction mode. Meaning, you can type in emacs-lisp and evaluate it. So just type in (getenv "HOME") and hit C-j (hold control key down and press 'j' character). Emacs wil evaluate what you typed in and return the result. – Jeffrey DeLeo Jan 12 '14 at 01:29
  • Thank you! And yes, it works :) - it returns "C:\\Users\\Lucas" – modulitos Jan 12 '14 at 21:52
1

Here's what I did, and worked with no problems:

setq default-directory "C:\\Path\\To\\Directory\\"

Saved and relaunched. I think in the windows env, you need to have double back-slashes.

blo0p3r
  • 6,790
  • 8
  • 49
  • 68
1

I just tested and it behaved fine with regards to Default-Directory. My settings are as follows:

emacs.exe in c:\bin\emacs\bin

%PATH% includes c:\bin\emacs\bin\

%HOME% set to %USERPROFILE (C:\Users\)

Direct from Run-box

Win+remacs -qRET

(message "%s" default-directory) => "C:\Users\jonpe/"

From cmd.exe after changing directory

Win+rcmdRET

cd c:\users\publicRET

emacs -qRET

(message "%s" default-directory) => "C:\Users\Public/"

Do you have emacs added to your %PATH% variable?

If I try to launch emacs by browsing to it's location then launching the default directory is set to the executable location.

Jonathan Leech-Pepin
  • 7,684
  • 2
  • 29
  • 45
1

I've been suffering similar weirdness with Emacs 24.3.1 on Windows 10. The solution is even weirder : customize Emacs to inhibit the startup screen. This can be done through the startup customization buttons, so you end up with the following in your .emacs :

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(inhibit-startup-screen t))

With the startup screen in place (eg. change t to nil above) the Windows shortcut "Start in" field (set to "E:\Users\Rob\") dominates and Ctrl-x Ctrl-f yields E:\Users\Rob/ (note the weird slash directions).

With the startup screen inhibited (t above) the .emacs

 (setq default-directory "E:/Users/Rob/me/")

dominates and Ctrl-x Ctrl-f yields E:/Users/Rob/me/ (all forward slashes).

Rob_before_edits
  • 1,163
  • 9
  • 13
0

Set your default-directory last in your init file, if you expect that setting to be useful after the init file is loaded.

If you also need it set earlier in the init file (e.g., so that some of your init-file code gets the right value) , then set it earlier too. When you do stuff in your init file, that stuff can change the variable value.

You can also use (setq-default default-directory "C:/Documents and Settings/USER NAME/Desktop/") (or whatever the directory name is), if you want that to be the default for every buffer.

Drew
  • 29,895
  • 7
  • 74
  • 104
  • I tried adding `(setq-default default-directory "C:/Users/Lucas/")` to my init file, and tried setting my `default-directory` at both the beginning and end of my init (`.emacs`) file, but the results are the same. Is there anything else I can try? – modulitos Jan 11 '14 at 21:52
  • Test what it is after your init file etc. have been loaded. If it is not what you set in your init file (at the end) then you will need to look past your init file. When I say at the end, I mean the end -- after your init file might load other libraries etc. If it is correct immediately after the init file is loaded, then something you are doing after that is changing it. – Drew Jan 11 '14 at 23:40
  • Do you have any suggestions on how I can test my `default-directory` at the end of my init file? Should I test this by somehow printing the `default-directory`? Or should I print my `default-directory` from the scratch buffer? I am a bit new to Emacs, so I want to make sure that I am starting with the correct approach :) – modulitos Jan 12 '14 at 23:34
  • Add to your `.emacs`, at the end (or anywhere): `(message "Default Dir: %S" default-directory)`. Then look in your `*Messages*` buffer after startup to see what it says. – Drew Jan 13 '14 at 01:18
  • Thanks- I tried this, and updated the OP with my new `.emacs` and `*Messages*` buffer. What can be blocking my `(setq-default default-directory "C:/Users/Lucas/")` command? – modulitos Jan 13 '14 at 07:56
  • No idea what that means. You don't say what the value is after your init file is loaded. If it is OK, then print it also at other placed in your init file, to see which part of your file is messing it up. If it is not OK, then either you are not in the right directory after startup or something else after your init file is changing the value again. – Drew Jan 13 '14 at 17:12
  • Note too that the value printed for `default-directory' seems to use both backslashes and a forward slash. – Drew Jan 13 '14 at 17:14
  • It sounds like Emacs is putting you in the MS Windows "start in" directory after your init file is loaded. Why not just set that in a Windows shortcut -- your solution #3?. – Drew Jan 13 '14 at 17:16
  • The windows shortcut works fine; I just couldn't figure out why my `.emacs` file wasn't able to do it (I still can't). I think I will just use the windows shortcut from now on, but it bewilders me since I can't seems to change my `default-directory` away from `C:\emacs\emacs-24.3\bin`. – modulitos Jan 13 '14 at 17:28
-1

I think you just need to use the right backslash (and escape them). This worked in my .emacs:

(setq default-directory "C:\windows")

Bart
  • 10,873
  • 7
  • 29
  • 23
  • I tried this, but results are no different. Of course, if I don't escape, then I get an error loading the init file. – modulitos Jan 11 '14 at 21:43