Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: 'Find file' without changing working directory

Tags:

emacs

elisp

My c/c++ projects tend to have fairly straightforward directory structures which separate out src, include, bin, etc. I also tend to have a master makefile in the uppermost directory. When working like this in Emacs, I always have to issue M-x cd uppermost-dir in order for my compilation shortcuts to work as expected.

Is there a way to keep the current directory the same as the one from which I launch Emacs? That is, can I stop Emacs from changing it's working directory when I open a file?

Alternatively, is there something crucial I'm missing about the typical workflow with a directory hierarchiy like this exclusively in Emacs?

like image 558
Karl Avatar asked Dec 02 '22 02:12

Karl


2 Answers

One way you can do this:

Make a file in the root directory of your project call .dir-locals.el

This will be read whenever you open a file in the directory or it's sub-directories.

In order to back up to the root folder and run make as your compile command, just put this in the .dir-locals.el file.

((nil . ((compile-command . "cd ~/mycode/c/; make"))))

nil is the mode to set local variables for (nil means any), so to do this for only C++ mode you could do this instead ...

((c++-mode . ((compile-command . "cd ~/mycode/c/; make"))))

Obviously you can set up a list with more options, say running ant for java files and so on.

emacs manual entry for directory locals

like image 55
justinhj Avatar answered Dec 19 '22 03:12

justinhj


Invoke make with the --directory argument to force it to change to that directory before doing anything:

make --directory /path/to/your/project

like image 24
Ben Avatar answered Dec 19 '22 03:12

Ben