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?
-
`shell-dirtrack-mode` ought to be enabled and providing this behaviour by default in `shell` buffers. Do you have it disabled? – phils Jan 29 '19 at 19:35
-
If it *is* enabled, but not working, see if `M-x shell-resync-dirs` helps (use when the shell buffer is at the shell prompt). – phils Jan 29 '19 at 19:40
-
Failing all of that, see `C-h f shell-directory-tracker` and `C-h i g (emacs)Directory Tracking` and start investigating the related variables -- chances will be that your particular shell output is not being recognised by Emacs, and you'll need to do some tweaking to resolve that. – phils Jan 29 '19 at 19:44
1 Answers
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 shell-mode-hook
like:
(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!

- 4,327
- 2
- 14
- 35
-
Hi. My apolgies. I want that if I change a directory in the shell, and, from the shell, execute open file, that the directory shall be the new directory that the shell is. – user1134991 Jan 30 '19 at 09:04
-
No need to apologize! Thank you for more detail. I'm confused now, though. How does the standard behavior differ from what you expect? Do you want the `find-file` prompt to always match what directory `*shell*` is in? – Lorem Ipsum Jan 30 '19 at 13:48
-
-
-
If the answer worked for you, please click the check mark ✓ beneath the up/down vote arrows to accept it. That gives me points which make me happy! :) – Lorem Ipsum Feb 05 '19 at 14:21