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/".
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).
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 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)
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 defun
inline if you like (it's better to define it at top-level, though, so the help system can find the definition). –
Vernavernacular 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
You have to redefine the environment variable HOME
to your new default directory.
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 –
Prisage © 2022 - 2024 — McMap. All rights reserved.