what to do when emacs hangs, freezes or crashes

Part of Living with Emacs

1. what to do when Emacs hangs

When Emacs hangs or freezes, usually it means that Emacs is stuck in a loop, and you should press C-g to get out. If C-g doesn’t do anything and your Emacs is still frozen, it is likely that you ran a command that synchronously called an external program which for some reason is not finishing its job. In that case, you can get out of the freeze by simply killing that external process. On MS Windows, you can do that from Task Manager. What if you don’t know which external process to kill? In that case, it’s time for the last resort: kill Emacs. By killing the Emacs process, would your unsaved work be gone? Not necessarily. We’ll come to that later.

On MS Windows, Emacs may hang when you try to use commands that rely on external tools like grep, find, ftp, diff, or latex if you have not done the job of installing related tools and configuring relevant user options.

1.1. exercises

Here’s some code for practice. Evaluate that and Emacs will hang.

(while t
  (print "haha"))

You can then press C-g to get out.

This one is for MS Windows only. Evaluate that and Emacs will hang again.

(call-process "notepad")

Pretend that you don’t see that notepad window. Open Task Manager and the process to kill is notepad.exe.

2. debugging

If you want to debug and find out what is causing Emacs to hang, use the command toggle-debug-on-quit.

If you want to debug a command that causes Emacs to hang where C-g doesn’t work, use M-x debug-on-entry RET call-process RET

3. what to do when Emacs crashes

If Emacs crashes often, you must be using an old version of Emacs or a very old laptop.

You were writing a PhD thesis with Emacs for hours and you never saved the file and then suddenly Emacs crashed. How to restore your unsaved work then? You just start Emacs and type M-x recover-session and you will see a special dired buffer. That buffer has the instruction “Move to the session you want to recover, then type C-c C-c to select it.”. So you press n or p to move to the item with the most recent timestamp and then press C-c C-c. What happens then is Emacs visits the thesis file, extracts your unsaved work from autosave data, and writes that into the buffer visiting the thesis file. You can now continue to work on your thesis but before continuing, this is probably a good time to save the file.

Who writes a PhD thesis with Emacs and not a word processor? Some users of org-mode and/or AUCTeX. This video http://www.youtube.com/watch?v=lsYdK0C2RvQ demonstrates producing a PDF file with code snippets in it using Emacs. That is going to come in handy when you write your papers.

3.1. exercises

Do some work with Emacs, don’t save anything, take a break, peel an apple with a peeler, cut it down with a knife, wash your knife, do the sharpening rod thing, eat your apple, now autosave has probably kicked in, manually crash your Emacs by killing the Emacs process from Task Manager, see if you can restore your unsaved work.

This entry was posted in Emacs and tagged , . Bookmark the permalink.

6 Responses to what to do when emacs hangs, freezes or crashes

  1. Sue D. Nymme says:

    Note: Under Windows at least, Ctrl-Break does what C-g does, except that it does not wait for Emacs’s periodic polling of keyboard input events. So it can interrupt some things that C-g cannot.

  2. You need to do `pkill -USR2 emacs` to unfreeze it.

  3. In OS X, `C-g` appears to be able to abort child processes. For example, evaluating this sexp:

    (call-process “/Applications/TextEdit.app/Contents/MacOS/TextEdit”)

    Open TextEdit and hangs Emacs, but pressing C-g triggers a bell and kills TextEdit. Of course if you want to be polite you should probably see what’s causing the hang, so you can do this:

    $ ps x | grep -i emacs
    $ brew install pstree
    $ pstree

    That will show you Emacs.app’s child processes and for the test above I saw only “ispell” and TextEdit.

  4. Pingback: What to do When Emacs Hangs or Crashes | Irreal

  5. Pingback: 2016-05-02 Emacs News - sacha chua :: living an awesome life

  6. Griper says:

    Useless! I have Emacs on Win7 freezing when I do the “ielm” command, which I have traced to a Lisp call to “start-process”. The process it’s trying to start, a program called “hexl”, never starts. No errors happen, it just freezes solid, and debugging will be useless since the “start-process” function is written in C. All because I wanted to get something to indent properly!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s