Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs, how to get directory of current buffer?

Tags:

emacs

elisp

In emacs there is buffer-file-name that gives the full path to a file. But is there a way to get only the directory of the file loaded in the current buffer?

like image 483
Leo Ufimtsev Avatar asked Jan 28 '15 15:01

Leo Ufimtsev


2 Answers

Sometimes default-directory for the current buffer may be set to something other than the current directory of the file the buffer is currently visiting, in which case the solution above wouldn't give what the asker was looking for.

In such cases, you can use the file-name-directory method, like so: (file-name-directory buffer-file-name)

Here is a link to the docs:

http://www.gnu.org/software/emacs/manual/html_node/elisp/File-Name-Components.html

like image 149
Sandy Avatar answered Sep 23 '22 11:09

Sandy


You can use the default-directory variable.

Documentation: Name of default directory of current buffer. Should end with slash. To interactively change the default directory, use command `cd'.

Note that expand-file-name will use default-directory by default, so sometimes you don't even need to mention it.

like image 24
abo-abo Avatar answered Sep 21 '22 11:09

abo-abo