14

I am using emacs on windows. I would like to know how to change the default "Find File:" path in emacs i.e. When we press "C-x C-f" I want the default file path to point to my Documents directory and not to "c:\emacs-**\bin/".

Shishir Pandey
  • 832
  • 1
  • 12
  • 23

4 Answers4

17

In a buffer that is visiting a file, the default path you see when you visit a new file (C-x C-f) is the directory that contains the current buffer's file.

In order to override the value "c:\emacs-**\bin/" with something more sensible, set the default-directory variable in your .emacs file:

(setq default-directory "/path/to/documents/directory/")

Note that the path value should end with a slash (or backslash on Windows).

However, you might also want to consider changing the value of your HOME environment variable, as by default, this is what the variable default-directory points at at startup (unless set to some other value like shown above).

Thomas
  • 17,016
  • 4
  • 46
  • 70
  • You should still generally use forward slashes in file paths in NTEmacs. It knows what to do. – phils Jun 25 '11 at 03:54
  • @phils: I suppose the reason to prefer slashes over backward slashes is that backslashs are also the escape character in strings, thus you always have to write path names like this: "c:\\foo\\bar\\" which becomes rather ugly. – Thomas Jun 25 '11 at 10:14
  • 3
    This answer does not work for me (and others). See the discussion under @loudandclear answer. – styrofoam fly Mar 15 '17 at 19:35
  • @styrofoamfly The answer does work depending on what exactly you're after. If you want `C-x C-f` to *always* look in a specific default directory first, I agree that it probably makes sense to bind that shortcut to your own custom function. If, however, you're happy with Emacs standard behavior *except* when you do `C-x C-f` for the first time after starting it (where it uses a directory you don't like), then my answer will help you. – Thomas Mar 16 '17 at 06:45
6

This shall do it:

(global-set-key (kbd "C-x C-f")  (lambda () (interactive)
                                     (cd "somePathHere")
                                     (call-interactively 'find-file)))

(replace somePathHere with the path to your documents directory)

loudandclear
  • 2,306
  • 1
  • 17
  • 21
  • @sabotero someone did -1 because this is a poor, inelegant solution. Emacs has mechanisms to address OPs question, default-directory. Remapping keys to a new function is NOT the way to address this simple task. – Chris Warth Mar 31 '15 at 17:06
  • 4
    This is a sensible answer. The OP presumably wants `C-x C-f` to always start in a certain directory. Setting `default-directory` will *not* do this, since it's buffer-local and will change as soon as you read from or write to a different directory. You'd have to add hooks to `find-file`, `dired` and anything else that sets it. And changing a setting in every buffer is hardly an "elegant" way to change the behaviour of one keybinding! If you want a keybinding to do something different, rebinding it to a custom wrapper is simple, foolproof and won't interfere with anything else. – pyrocrasty Oct 19 '16 at 15:47
  • Although, if you're binding a function to a key, you should at least give it a name. You can use `defun` inline if you like (it's better to define it at top-level, though, so the help system can find the definition). – pyrocrasty Oct 19 '16 at 15:54
  • the accepted answer doesn't work for me in windows. This answer works. – CS Pei Apr 29 '17 at 21:29
5

Variable 'default-directory' is the "current" directory (for the current buffer). Command 'cd' changes directories, and visiting any file or directory (e.g. with Dired) changed the 'default-directory' for that buffer.

You can start Emacs in a given directory, by passing that directory on the command line. You can use a Windows shortcut to do this too. And you can have the shortcut visit that directory in Dired.

Example shortcut info:

Target: C:\Emacs\bin\runemacs.exe "C:\my\favorite\folder"

Start in: C:\my\favorite\folder

Drew
  • 29,895
  • 7
  • 74
  • 104
0

You have to redefine the environment variable HOME to your new default directory.

Giann
  • 3,142
  • 3
  • 23
  • 33
  • No. `HOME` is used for a lot of things other than just your Emacs default directory. Messing with it will probably make many things behave unpredictably. -1 – Noufal Ibrahim Jun 24 '11 at 10:10
  • 1
    Valid comment. But since Emacs seems to use its own directory now, it means the HOME variable isn't defined yet. Defining it on 'My Documents' won't hurt. I wouldn't have say that if he was on Linux – Giann Jun 24 '11 at 12:28
  • Well, I'll admit that I don't know how things are on Windows. – Noufal Ibrahim Jun 24 '11 at 12:29