Emacs: "Find File" without changing the working directory

My c / C ++ projects have fairly simple directory structures that highlight src, include, bin, etc. I also tend to have a master makefile in the topmost directory. When working in Emacs, I always need to release Mx cd uppermost-dir so that my compilation shortcuts work as expected.

Is there a way to keep the current directory the same as the one from which I am running Emacs? That is, can I stop Emacs from changing its working directory when opening a file?

Alternatively, is there anything crucial that I am missing in a typical workflow with a directory hierarchy similar to this exclusively in Emacs?

+4
source share
5 answers

One way to do this:

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

This will be read whenever you open a file in a directory or its subdirectories.

To back up to the root folder and run make as a compilation command, just put this in the .dir-locals.el .

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

nil is a mode for setting local variables for (nil means any), therefore for this only for C ++ mode you can do it instead ...

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

Obviously, you can configure a list with a lot of parameters, for example, execute ant for java files, etc.

manual recording of emacs for directory locales

+7
source

Call make with the --directory argument to force it to change this directory before doing anything:

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

+5
source

Go to the project directory (where the Makefile is located) and call compilation:

 (defun my-compile () (interactive) (when-let (default-directory (locate-dominating-file default-directory "Makefile")) (call-interactively 'compile))) 
+4
source

The current directory associated with the buffer associated with the file is usually the directory containing the file. You can change it, but this is not necessary for what you want to do.

Set the compilation-directory variable through a local file variable (usually relative to the current file, for example "../.." ) or via .dir-locals.el .

+1
source

I used Mx compile <RET> cd /path/to/project && make -j8 , but I prefer the Ben method now.

+1
source

All Articles