emacs "Find File:" default path
Asked Answered
O

4

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/".

Oas answered 24/6, 2011 at 5:58 Comment(0)
A
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).

Adobe answered 24/6, 2011 at 9:5 Comment(4)
You should still generally use forward slashes in file paths in NTEmacs. It knows what to do.Grandson
@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.Adobe
This answer does not work for me (and others). See the discussion under @loudandclear answer.Sepoy
@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.Adobe
B
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)

Bukovina answered 24/6, 2011 at 9:6 Comment(4)
@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.Platysma
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.Vernavernacular
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).Vernavernacular
the accepted answer doesn't work for me in windows. This answer works.Kalong
S
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

Sabadilla answered 20/8, 2011 at 1:41 Comment(0)
T
0

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

Talky answered 24/6, 2011 at 8:5 Comment(3)
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. -1Prisage
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 LinuxTalky
Well, I'll admit that I don't know how things are on Windows.Prisage

© 2022 - 2024 — McMap. All rights reserved.