From WikEmacs
Jump to navigation Jump to search
Description save and restore the state of Emacs accross sessions
Author name of author
Maintainer name of maintainer
Source no link provided
Part of Emacs yes

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.


Save and restore sessions manually

You can save the desktop manually with the command M-x desktop-save, and you can load a saved one with M-x desktop-read.

Save and restore sessions automatically

You can also enable automatic saving of the desktop when you exit Emacs, and automatic restoration of the last saved desktop when Emacs starts: add this line in your init file:

    (desktop-save-mode 1)

If you turn on desktop-save-mode in your init file, then when Emacs starts, it looks for a saved desktop in the current directory. (More precisely, it looks in the directories specified by desktop-path, and uses the first desktop it finds.) 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.


Clear the desktop

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.

Do not save given files or modes

You can specify buffers which should not be saved, by name or by mode, e.g.:

  (setq desktop-buffers-not-to-save
       (concat "\\("
  (add-to-list 'desktop-modes-not-to-save 'dired-mode)
  (add-to-list 'desktop-modes-not-to-save 'Info-mode)
  (add-to-list 'desktop-modes-not-to-save 'info-lookup-mode)
  (add-to-list 'desktop-modes-not-to-save 'fundamental-mode)

Desktops as Bookmarks

Library desktop.el provides for the possibility of having multiple saved desktops, but only one per directory. If you use Bookmark+, then you can have any number of desktops and use them as bookmarks, jumping from one to another at any time. You create a desktop bookmark using C-x r K (command M-x bmkp-set-desktop-bookmark).

Ordinary desktop files are used to record the information. The bookmark itself records only the location of the desktop file. Bookmarked desktops are intended to be used only as bookmarks, not in the ordinary way (e.g. at Emacs startup), so the locations of the desktop files used are unimportant. In particular, you can have any number of (bookmark) desktop files per directory.

Restore big sessions smoothly

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.

See also

See the page Session_management

Psession: desktop improved

psession, by Helm's author, also restores your last window configuration, the value of any var, history variables (as a replacement for the above mentioned savehist mode), optionally autosaves the session, etc.

Savehist: save minibuffer history

savehist, to save minibuffer history from one session to another.