23

I have this set in my init.el

(desktop-save-mode 1)

This works great, only I was wondering:

  • how can I change it to save the .emacs.desktop files into ~/.emacs.d instead of ~/

  • how can I stop it from asking me if I want to save (only occurs the first time I close emacs after a reboot, from then on it assumes yes, which is what I always want to happen)

tobeannounced
  • 1,999
  • 1
  • 20
  • 30

3 Answers3

46

I use the following, which works for me:

;; Automatically save and restore sessions
(setq desktop-dirname             "~/.emacs.d/desktop/"
      desktop-base-file-name      "emacs.desktop"
      desktop-base-lock-name      "lock"
      desktop-path                (list desktop-dirname)
      desktop-save                t
      desktop-files-not-to-save   "^$" ;reload tramp paths
      desktop-load-locked-desktop nil
      desktop-auto-save-timeout   30)
(desktop-save-mode 1)

Well, I actually set (desktop-save-mode 0) and then use M-x my-desktop to kick things off:

(defun my-desktop ()
  "Load the desktop and enable autosaving"
  (interactive)
  (let ((desktop-load-locked-desktop "ask"))
    (desktop-read)
    (desktop-save-mode 1)))

But that's because my session is frequently in excess of 100 files, mostly via tramp, and so I prefer to make loading it a manual task, and not clobber the desktop file otherwise :)

I recommend checking out the Emacs Wiki: http://www.emacswiki.org/emacs/DeskTop

There are some useful enhancements to the default functionality. In particular, I recommend adding some method of auto-saving your desktop mid-session, as it's really annoying if your system crashes when Emacs has been running for several days, and your desktop hasn't been saved in the interim.

Since Emacs 24.4 the desktop file is auto-saved periodically by default. See the desktop-auto-save-timeout variable (which I've also added to the block above). Thanks to GDP2 and Dexter Morgan for their comments on this.

Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
phils
  • 71,335
  • 11
  • 153
  • 198
  • 1
    Thank you very much kind sir :) Works fantastically. Two additions I made: (could you just tell me if they are obviously wrong?) desktop-files-not-to-save "*magit" ;ignore magit files, and also (add-hook 'auto-save-hook (lambda () (desktop-save-in-desktop-dir))) – tobeannounced Dec 20 '10 at 08:47
  • The auto-save hook looks fine. `desktop-files-not-to-save` needs to be a regular expression, so that looks wrong. If you're matching things ending in "magit" then use `".*magit$"` – phils Dec 20 '10 at 20:09
  • Due to this answer's age, it seems that the last paragraph is no longer accurate. As of at least Emacs 25.1.1, enabling `desktop-save-mode` sets an idle timer that calls `desktop-auto-save` every 5 secs. – GDP2 Apr 05 '17 at 06:56
  • 1
    in Emacs 25, setting the variable `desktop-auto-save-timeout` to **n** seconds will make sure a desktop file is saved every **n** seconds. E.g. `(setq desktop-auto-save-timeout 15)`. The default is 30 seconds. – n1k31t4 Apr 09 '17 at 19:34
3

how can I change it to save the .emacs.desktop files into ~/.emacs.d instead of ~/

Customize desktop-dirname variable.

how can I stop it from asking me if I want to save

Customize desktop-save variable.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Cheers for that, I tried adding this with no luck: (desktop-save-mode t) (setq desktop-dirname "~/.emacs.d/") – tobeannounced Dec 19 '10 at 00:17
  • @tobeannounced: `customize` is *not* equivalent to `setq`. Read the help for the variables. – jfs Dec 19 '10 at 02:45
  • 1
    setting desktop-dirname seems to be insufficient; I couldn't get it to work until I _also_ changed desktop-path :/ self.herp_derp() – allyourcode Mar 15 '16 at 19:35
0

Wanted to share how my sessions are organized.

Requirements:

  1. Send file from terminal to custom emacs session
  2. Restore session from terminal
  3. Save session

Solution:

  1. Install package(plugin) workgroups2 -> https://github.com/pashinin/workgroups2

  2. Add following lisp code to your ~/.emacs.d/init.el or ~/.emacs:

->

(setq server-socket-dir "~/.emacs-local/server")

(defun nk-server-start (custom-server)
    ; (nk-server-start "abe")
    (setq server-name custom-server)
    (server-start) ; run emacs server
    (setq wg-session-file (concat "~/.emacs-local/sessions/" custom-server))
    ; (setq wg-session-file "~/.emacs-local/sessions/foo")
    (workgroups-mode 1)
    (wg-switch-to-workgroup custom-server)
    )
; Run file in specific server (foo)
    ; emacsclient -n callback.sh -s ~/.emacs-local/server/foo

; Show server name in title bar
(setq frame-title-format '("" "%b @ " server-name))
    ; https://www.emacswiki.org/emacs/FrameTitle

; ;; What to do on Emacs exit / workgroups-mode exit?
(setq wg-emacs-exit-save-behavior           'save)      ; Options: 'save 'ask nil
(setq wg-workgroups-mode-exit-save-behavior 'save)      ; Options: 'save 'ask nil

Function nk-server-start is called when emacs is started. It has one argument - sesssion name.

We can start emacs-session foo by running following command from terminal:

setsid emacs --eval '(nk-server-start "foo")' &

If we want to open file in session foo from terminal we need to run:

setsid emacsclient -n -s ~/.emacs-local/server/foo file.txt >> /dev/null &

When we close session, all buffers,settings, etc. are saved in file ~/.emacs-local/sessions/foo

When we run command setsid emacs --eval '(nk-server-start "foo")' & next time, all buffers will be restored

Because commands are large and I am lazy :) I made some scripts and added them to my $PATH in order to ease this:

em_start_foo.sh - Run session, used only once to start session

#!/bin/bash

setsid emacs --eval '(nk-server-start "foo")' &

em_foo.sh - Add files to session

#!/bin/bash

setsid emacsclient -n -s ~/.emacs-local/server/foo "$@" >> /dev/null &

Now we just run from terminal:

$ em_start_foo.sh                 # start foo session
$ em_foo.sh file_1.txt            # open file_1.txt in foo session
$ em_foo.sh file_2.txt file_3.txt # open file_2.txt and file_3.txt in foo session

Multiple sessions can be run in parallel of course.

Let's say we created also scripts em_start_foo_2.sh, em_start_foo_2.sh, em_start_foo_3.sh, em_start_foo_3.sh (somewhere in $PATH of course)

Then we can do something like this:

$ em_start_foo.sh          # start foo session
$ em_start_foo_2.sh        # start foo_2 session in separate emacs
$ em_foo.sh file_1.txt     # open file_1.txt in foo session
$ em_foo_2.sh a.txt b.txt  # open a.txt and b.txt in foo_2 session

$ em_start_foo_3.sh        # start foo_3 session
$ em_foo_3.sh tmp.txt      # open tmp.txt in foo_3 session

##### Close emacs foo_2 from gui - session is automatically saved ###

$ em_start_foo_2.sh        # start foo_2 session with all buffers restored

Package workgroups2 is really great!

My emacs init file with session options is available at: https://github.com/nexayq/dot_files/blob/master/emacs/dot_emacs_d/init.el

nexayq
  • 606
  • 1
  • 9
  • 13