I use shell mode, often. I use the c-xc-f. Most of the time, the start directory for looking for file, correspond to the PWD of the emacs shell buffer. Can i set that it is always the case/or reset it?
If you want the shell to always start from a specific directory, you can use the
shell-mode-hook. Code stored in a mode hook is executed after the mode is loaded.
We want to send a command to change the directory once the shell process has started. There isn't a stock function to send commands to a shell process. However, there's a great StackOverflow response which provides one1:
(defun sh-send-command (command) "Send COMMAND to current shell process. Create shell process if none exists." (let ((proc (get-process "shell")) pbuf) (unless proc (let ((currbuff (current-buffer))) (shell) (switch-to-buffer currbuff) (setq proc (get-process "shell")) )) (setq pbuff (process-buffer proc)) (setq command-and-go (concat command "\n")) (with-current-buffer pbuff (goto-char (process-mark proc)) (insert command-and-go) (move-marker (process-mark proc) (point)) ) (process-send-string proc command-and-go) ))
This can be used in the
(add-hook 'shell-mode-hook (lambda () (interactive) (sh-send-command "cd /path/to/start/at")))
This creates an anonymous function which calls the shell command
cd to change directory to the given path. Note that you'll need to quote a path containing spaces. To do this, escape the inner quotes using a backslash:
If you're interested in setting a default directory across all of Emacs, and not just
shell, you might be interested in this SO question: set a global default directory.
1If you like this function, give user VitoshKa an upvote!