16

Let's say I have the following line in .emacs file.

(setq-default default-directory "~/Desktop/mag")

How can I check the value for `default-directory' in elisp?

Added

I asked this question as I need to check the value of default-directory based on this question.

The elisp code should change the default directory when I click C-x C-f, but I still get ~/, not ~/Desktop/mag. So, I need to check what value the default-directory has.

Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871
  • 1
    Could you clarify your question please? Clearly "just evaluate `default-directory`" is not the answer you want, so what does that not do that you need? – zwol Jul 29 '10 at 21:11

5 Answers5

25

If you're at the console you can type C-h v, which will prompt you for a variable name. Type in default-directory (or any other name) and you'll get a buffer with some info about that variable, including its value.

The elisp function you're running is describe-variable:

(describe-variable VARIABLE)

I figured this out by C-h k C-h v. C-h k shows you what function the next key or key sequence would call.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Paul Rubel
  • 26,632
  • 7
  • 60
  • 80
  • To combine Paul and monotux's answers : The sure/fast way to check if your var is set is to position the point over it and press `C-h v` : If your var is auto-selected by eldoc for info, then it IS set. Uber-handy indeed. – yPhil Mar 15 '12 at 10:06
  • [Documented in the GNU Emacs Manual](https://www.gnu.org/software/emacs/manual/html_node/eintr/See-variable-current-value.html) and bound to `C-h v`. – young_souvlaki Oct 13 '21 at 20:21
15

If you just want to check the value, you can run the following from the *scratch* buffer:

(print default-directory) <ctrl-j>

The *scratch* buffer allows you to evaluate lisp on the fly. You must hit ctrl-j after to evaluate.

Starkey
  • 9,673
  • 6
  • 31
  • 51
  • Sorry to say this, but I think my [answer](http://stackoverflow.com/a/20255760/1050454) is better. It is faster not having to switch buffer. Also, aren't some variables buffer-local? – Emanuel Berg Nov 28 '13 at 00:39
  • Aha, the OP wants it **in Elisp**. Sorry, then the above doesn't apply. – Emanuel Berg Nov 28 '13 at 00:49
8

As previously stated, C-h v is the easiest way to find out a variables value. To make it even better, place your cursor on the variable you want to know about, and then run C-h v, and it will default to the word under the cursor. Really handy.

monotux
  • 3,657
  • 28
  • 30
1

If you just want to see the variable value in the echo area (less of a mess), try:

(defun describe-variable-short (var)
  (interactive "vVariable: ")
  (message (format "%s: %s" (symbol-name var) (symbol-value var))) )
(global-set-key "\C-hV" 'describe-variable-short)
Emanuel Berg
  • 499
  • 4
  • 14
1

Try:

(print default-directory)

write the above code in one line inside of emacs, got to the end of the line and hit C-x C-e