patrickkidd
patrickkidd

Reputation: 3137

How to get emacs to automatically load and save desktop from initial directory?

I usually have 3-4 different projects that I work on at once. So I am trying to figure out how to get emacs to load the desktop from the folder that I open emacs from and also save to that file when I exit from that emacs instance.

All of the docs I have seen either describe how to get emacs to automatically open and save from a default location (which makes multiple desktops impossible), or to manually load and save the desktop to a specific directory (which I am doing now).

Thanks!

Upvotes: 6

Views: 2044

Answers (3)

Gary Taverner
Gary Taverner

Reputation: 21

For a really simple answer I put this at the end of my .emacs file. It works fine if you save the desktop in the project folder and start emacs from the project folder.

(desktop-change-dir default-directory)

Upvotes: 2

artscan
artscan

Reputation: 2350

Put this to your .emacs:

(setq your-own-path default-directory)
(if (file-exists-p
     (concat your-own-path ".emacs.desktop"))
    (desktop-read your-own-path))

(add-hook 'kill-emacs-hook
      `(lambda ()
        (desktop-save ,your-own-path t)))

Upd.: v. 2, ignore on demand.

(setq your-own-path default-directory)
(if (file-exists-p
     (concat your-own-path ".emacs.desktop"))
    (if (y-or-n-p "Read .emacs.desktop and add hook?")
    (progn
      (desktop-read your-own-path)
      (add-hook 'kill-emacs-hook
            `(lambda ()
               (desktop-save ,your-own-path t))))))

Upvotes: 7

I have developed a small set of functions to manage multiple desktops: desktop+

You might want to check it out. My workflow is not exactly the same as yours, though:

  1. I always run emacs from the same directory (I run it from a key binding in my window manager), meaning that I can not rely on the starting directory to know which desktop I want to work with

  2. the first time I work on a new project, I call M-xdesktop-create and provide a name. The desktop is then saved to a central location (under "~/.emacs.d/desktops" by default)

  3. each subsequent time I want to work with a saved desktop, I run M-xdesktop-load, and am provided with a list of saved sessions in which I can quickly retrieve the name of the desired session.

Sessions are always saved when emacs exits or you load another session.

Upvotes: 3

Related Questions