3

When I'm using

M-x gdb 

when lots of source files are open in emacs, the default directory in emacs is often different with the directory of my working copy to run that file i want to run. It's always painful to type the correct full path of the file which i want to debug. How could I solve this problem? Is there a way to change the current default directory to the working copy of my project?

4 Answers4

15

I kept editing my .emacs file, using

(setq default-directory "~/your/path") 

but found that whenever I restarted emacs, it kept defaulting to the 'original' directory.

It turned out the problem was that I had left on the default startup screen (GNU emacs 23). I think that the default welcome screen with the links to the tutorial etc, changes the default directory back to the one where the welcome screen files are to be found. I clicked on the 'customize startup' link, then switched off the welcome screen. Since then, when I open emacs it defaults to the scratch buffer, but the default-directory when I use C-x C-f is the one I set in the .emacs file.

Hope that helps someone out there, it was driving me crazy!

Kevin Panko
  • 7,346
  • 22
  • 44
  • 53
Ghufran
  • 151
  • 1
  • 2
  • 2
    ```(cd (getenv "HOME"))``` instead of ```(setq ...)``` works in my case (Windows 10). See http://emacs.stackexchange.com/questions/18723/setting-default-directory-does-not-change-find-file-directory/18724#18724?newreg=5f82fb84474e4e1ca2ba5dfb9ccd1ee3 – Hartmut Pfarr Jan 23 '17 at 23:04
2

The current working directory is always the location of the current buffer.

One option is to go into shell-mode and then cd to the preferred directory, then anything you open, or run from within that shell-buffer will default to that directory.

Brian Postow
  • 1,555
  • 2
  • 15
  • 22
  • it seems the directory changes randomly when i use the gdb in emacs, and i cannot get the correct directory. –  Feb 24 '11 at 09:37
  • It changes within the buffer? like you load a file, it's in directory X, you do some debugging, you load a file, and it's in some other directory Y? Does the program do any CDing itself? – Brian Postow Feb 24 '11 at 14:33
2

If you're in a buffer not attached to a file (like *scratch*, gnus' buffer...) you can use M-x cd to change the current directory. Just opening the directory will also do the trick.

In gdb, you can ajso just type cd /where/i/want/to/go

Rémi
  • 1,486
  • 9
  • 10
1

Normally, The current working directory is always the location of the current buffer. And gdb will use this directory automatically.

But I indeed had distressed experience that when M-x gdb, it always entered the ~ directory, and complained the target file couldn't be found.

Finally, I found this is caused by the buffer was changed unintended, and may be backed up. I realized this when I killed this buffer and reopened it. After kill and reopen the buffer, M-x gdb works well again.

From my experience, when this happens, exit emacs and restart it can't help fixing this. Just try killing the buffer and reopen the file.

bigstone1998
  • 111
  • 2