how can I change a default working directory/drive for shell in Emacs (on Windows)?

Normally, shell starts in the same directory as the file in current buffer. However, when my current file is on D: drive, it starts in c:. Manually changing drive to D: in shell brings me to my directory of course, but I want to avoid this extra step. Is it possible?


on windows:

  • When you enter M-x shell you are automatically placed in the default directory of emacs. In this case you should modify the default directory of emacs in the .emacs config file.

    (cd "C:/Users/username/directory/path")

  • However if you have a file open and you enter M-x shell from that window you will be placed in the directory of that file.

  • Re: second point - that's not what I observe when I edit a file on a D: drive. I'm placed in the C:\ root directory; when I manually change it to D: I'm indeed in the directory of my file.
    – Victor K.
    May 17 '10 at 3:28
  • Your cursor has to be in the same window. If you are editing a file on D:... and you enter "M-x shell" you will be brought to a shell that is in that directory. I just tested it again. It really works. Other issue can be if you have some default values set in your .emacs configuration file.
    – Dlf
    May 17 '10 at 14:04
  • My test environment: Windows Vista + Emacs 23.1.1 + my .emacs configuration file does not modify default directory for the shell. It works for me this way. What is your environment?
    – Dlf
    May 17 '10 at 15:06

I use eshell with esh-toggle loaded, and I have eshell-toggle-cd bound to a handy key.

Hit it once, you get a shell in the same directory as the file you have open. Hit it again, the shell buffer goes away and you're back to your original buffer.

Since most of my 'swap to shell' interruptions involve the file/directory in the current buffer, this saves a surprising amount of time.

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.