0

I want to go to a specific directory after I start a shell. So I put these lines in my .emacs file:

(shell)
(shell-command "cd C:/MyStartUp/ThisDirectory")
(setq default-directory "C:/MyStartUp/ThisDirectory")

However, whenever I start emacs, the shell ends up in this directory c:\emacs-26.3\bin> How do I fix this?

0

In some circumstances, having the trailing forward slash for the default-directory is necessary. My recollection is that this may depend upon the OS. It is possible to pass commands to the shell buffer under-the-hood with comint-send-string and comint-send-input, but that appears to be unnecessary in this particular use-case. The beginning directory in the shell buffer will be inherited from the default-directory, which can be let-bound:

(let ((default-directory "C:/MyStartUp/ThisDirectory/"))
  (shell))
3
  • I did have the / in the end in my case, I forgot to add that when I posted here. After I used your command, my starting directory becomes c:\> but never goes to the default-directory. Although my M-x find-file does start to look for files in that default-directory. – tony Jul 29 '20 at 16:20
  • I just fired up my Windows version of Emacs and tested the code ... it works correctly "as-is" -- both with a trailing forward slash and also without a trailing forward slash. I would suggest starting Emacs with no configuration (e.g., Emacs -q) and then evaluate the code above, either in a *scratch* buffer or just by typing M-x eval-expression aka M-: Perhaps there is a typo in your path or something in your config that is causing problems. – lawlist Jul 29 '20 at 18:25
  • I found out what my problem was. The name ThisDirectory I used as an example actually contains space and - like OneDrive - ThisDirectory. So I incorrectly used \ for the space and other characters. It turns out not only it's not needed, it confused this shell command to go to the correct path. Thanks all for the help. – tony Jul 30 '20 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.