Changing the default folder in Emacs

77,209

Solution 1

You didn't say so, but it sounds like you're starting Emacs from a Windows shortcut.

The directory that you see with c-x c-f is the cwd, in Emacs terms, the default-directory (a variable).

When you start Emacs using an MS Windows shortcut, the default-directory is initially the folder (directory) specified in the "Start In" field of the shortcut properties. Right click the shortcut, select Properties, and type the path to your desktop in the Start In field.

If you're using Emacs from the command line, default-directory starts as the directory where you started Emacs (the cwd).

This approach is better than editing your .emacs file, since it will allow you to have more than one shortcuts with more than one starting directory, and it lets you have the normal command line behavior of Emacs if you need it.

CWD = current working directory = PWD = present working directory. It makes a lot more sense at the command line than in a GUI.

Solution 2

I think the line you need to add to your .emacs is is

(setq default-directory "C:/Documents and Settings/USER NAME/Desktop/" )

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.

Solution 3

You can type the 'cd' emacs command. ( M-x cd ) to change the default folder as a one off.

Solution 4

I've put

(cd "c:/cvsroot/")

in my .emacs and it did the job

Solution 5

The default folder is actually the same as the current working folder for the buffer, i.e. it can be different for every file you work with. Say that the file you are working with is located in C:\dir_a, then the working directory for that buffer will by default be C:\dir_a. You can change this with M-x cd and type in whatever directory you would like to be the default instead (and by default I mean the one that will show up when you do C-x C-f).

If you start emacs without opening a file, you will end up with the *scratch* buffer open. If you started emacs from a Windows shortcut, the working directory will be the same as that specified in the shortcut properties. If you started it from the command line, it will be the directory from where you started it. You can still change this default directory with M-x cd, also from the *scratch* buffer.

Finally, you can do as Vadim suggests and put

(cd "c:/dir_a/")

in your .emacs file, to make that directory the default no matter how you start emacs.

Share:
77,209

Related videos on Youtube

Anton
Author by

Anton

Game Developer and Programmer from Richmond VA.

Updated on July 08, 2022

Comments

  • Anton
    Anton almost 2 years

    I am fairly new to Emacs and I have been trying to figure out how to change the default folder for C-x C-f on start-up. For instance when I first load Emacs and hit C-x C-f its default folder is C:\emacs\emacs-21.3\bin, but I would rather it be the desktop. I believe there is some way to customize the .emacs file to do this, but I am still unsure what that is.

    Update: There are three solutions to the problem that I found to work, however I believe solution 3 is Windows only.

    • 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.

  • Anton
    Anton over 15 years
    Yes I am using emacs on windows.
  • Ami
    Ami over 10 years
    This works on MacOS too. I added this to my .emacs: (setq default-directory (concat (getenv "HOME") "/"))
  • Drew
    Drew over 10 years
    And if you want Emacs to start in Dired of that directory then, in addition to putting it in the Start In field, add it at the end of the Target field, after one or more spaces, enclosing it between double-quotes ("). E.g.: "d:\path\to\the\folder".
  • Rob_before_edits
    Rob_before_edits almost 8 years
    +1 for mentioning inhibiting the start-up screen. I found this to be key on my Windows 10 system. I posted my answer on the related question stackoverflow.com/a/38435697/823636