I run Emacs in batch mode to execute some scripting actions. I'd like to get paths of files relative to where Emacs got started in batch mode:

(file-relative-name (buffer-file-name) directory-where-emacs-was-started)

I do not know how to find out directory-where-emacs-was-started. default-directory does not work because it defaults to buffer file directory and (default-value 'default-directory) is nil.

How to find out what I'm looking for?

Edit: clarification, I need as elisp variable the value that pwd will print just before running Emacs in shell invocation like this:

pwd && emacs

You can use command-line-default-directory, it is intended when using Emacs in batch mode, or when processing command line options:

Default directory to use for command line arguments. This is normally copied from `default-directory' when Emacs starts.

It retains its value throughout the session.

| improve this answer | |

If by "directory-where-emacs-was-started" you mean the directory where Emacs is located, then it is the value of this variable: invocation-directory:

The directory in which the Emacs executable was found, to run it. The value is nil if that directory's name is not known.

Depending on what you mean, you might also want to look at exec-directory or even the value of default-directory immediately after Emacs starts.

| improve this answer | |
  • Neither invocation-directory nor exec-directory are what I'm looking for. Question clarified. – Gracjan Polak May 14 '16 at 15:51
  • Your clarification confirms that what you want is "the value of default-directory immediately after Emacs starts." – Drew May 14 '16 at 21:51
  • @Drew, which isn't what invocation-directory contains. It contains the directory of the Emacs binary, not the original current directory of the user. – Lindydancer May 16 '16 at 11:49
  • @Lindydancer: Yes, of course. My answer said clearly "IF" what you mean is the directory where Emacs is located..." And the answer includes exec-directory and default-directory, "depending on what you [OP] mean". OP's clarification of the question shows that what was meant/requested is "default-directory immediately after Emacs starts". – Drew May 16 '16 at 14:08
  • @Drew -- Ah, I interpreted your comment (see "Your clarification...") that you still thought your answer was valid after the clarification. – Lindydancer May 16 '16 at 14:35

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.