Tuesday, 9 December 2008

Saving Emacs Sessions

There is something very cool about just having a screen of code.

As I've been working with Emacs I'm discovering more and more features. Today it occurred to me that it would be useful to be able to exit Emacs and having everything as it was upon return.

The following is from the GNU Emacs Manual:

51 Saving Emacs Sessions

Use the desktop library to save the state of Emacs from one session to another. Once you save the Emacs desktop—the buffers, their file names, major modes, buffer positions, and so on—then subsequent Emacs sessions reload the saved desktop.

You can save the desktop manually with the command M-x desktop-save. You can also enable automatic saving of the desktop when you exit Emacs, and automatic restoration of the last saved desktop when Emacs starts: use the Customization buffer (see Easy Customization) to set desktop-save-mode to t for future sessions, or add this line in your ~/.emacs file:

(desktop-save-mode 1)

If you turn on desktop-save-mode in your ~/.emacs, then when Emacs starts, it looks for a saved desktop in the current directory. Thus, you can have separate saved desktops in different directories, and the starting directory determines which one Emacs reloads. You can save the current desktop and reload one saved in another directory by typing M-x desktop-change-dir. Typing M-x desktop-revert reverts to the desktop previously reloaded.

Specify the option `--no-desktop' on the command line when you don't want it to reload any saved desktop. This turns off desktop-save-mode for the current session. Starting Emacs with the `--no-init-file' option also disables desktop reloading, since it bypasses the .emacs init file, where desktop-save-mode is usually turned on.

By default, all the buffers in the desktop are restored at one go. However, this may be slow if there are a lot of buffers in the desktop. You can specify the maximum number of buffers to restore immediately with the variable desktop-restore-eager; the remaining buffers are restored “lazily,” when Emacs is idle.

Type M-x desktop-clear to empty the Emacs desktop. This kills all buffers except for internal ones, and clears the global variables listed in desktop-globals-to-clear. If you want this to preserve certain buffers, customize the variable desktop-clear-preserve-buffers-regexp, whose value is a regular expression matching the names of buffers not to kill.

If you want to save minibuffer history from one session to another, use the savehist library.

No comments:

Post a Comment