2

I want to debug my init file, by exiting from loading the file in the middle so if something is wrong after that place it will not be loaded. How can I exit from .emacs file?

  • 2
    Emacs will exit from your init file immediately once it finds something is wrong, so if you put (1 + 1) to the beginning of your init file, the rest will not be loaded at all. is this what you want? BTW, I don't think this method will help you to debug your init file at all. – xuchunyang Jan 9 '16 at 11:23
  • @xuchunyang that help, but I think I used a function that was exiting from the emacs file some time ago when I was debugging init file but I don't remember what function it was. – jcubic Jan 9 '16 at 11:34
  • 1
    You'd be better served by --debug-init option you can add when starting Emacs. – wvxvw Jan 9 '16 at 14:29
  • @wvxvw I always have that enabled. – jcubic Jan 9 '16 at 15:55
7

You can use this to exit from your init file:

(with-current-buffer " *load*"
  (goto-char (point-max)))

Emacs uses the load function to execute lisp file through a temporary buffer, that's " *load*", if the name is not already taken. The above code moves the point to the end of the buffer, thus the read function will not read further code.

For example, the last sexp of foo.el is unbalanced, but there is no error when running it, since Emacs can't go that far, it exits early:

~$ cat foo.el 
(message "Loading %s..." load-file-name)

(with-current-buffer " *load*"
  (goto-char (point-max)))

(this line does not matter
~$ emacs --batch --load foo.el
Loading /home/xcy/foo.el...
~$

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.