I spent a long time using GNU Emacs before I ever started taking advantage of saving sessions as ‘desktops’. It provides that same functionality that other IDEs often call ‘projects’. That is, grouping together a number of related files and buffers that you can later load en masse. Today I wanted to talk about how my workflow benefits from using the desktop commands in Emacs along with other benefits that you may find useful.
I prefer to manage desktops manually. In other words, I do not like to use
desktop-save-mode, which I will discuss later. Instead I use three key-bindings for desktops:
C-c d cfor
C-c d dfor
C-c d sfor
When saving a desktop, i.e. saving a list of all open buffers, files, their major modes, et cetera, Emacs creates a file it calls
.emacs.desktop that goes into a directory, which
desktop-save prompts for. So let’s say I am working on a programming project. As I start creating and opening files I will periodically run
desktop-save to take a ‘snapshot’ of my current Emacs session, saving it at the top-level directory of that project. When I am done working on that project I can switch to another desktop by using
desktop-change-dir, which is like opening a project file in Visual Studio or Eclipse. Or if I just want a blank slate I use
desktop-clear to close (almost) every file and buffer.
Automating Some of That
If you enable
desktop-save-mode then Emacs will do two things for you:
It will automatically save your current session as a desktop when you close Emacs.
It will automatically restore that session when you start Emacs, assuming the desktop file is in the current directory. If not there it will use the first one it finds in
A lot of people find this useful so I recommend trying it out. The reason I do not personally prefer it is because I do not always want to save my session when exiting. For example, I have a desktop file for this blog; when I switch to it Emacs opens up some buffers like the directory where I write my drafts and my list of ideas about subjects to write. But it does not load any previous articles because I am not interested in reloading them, and by not using
desktop-save-mode I avoid accidentally saving them as part of that desktop. That takes a little bit more manual effort though, which is why I suggest people try out
desktop-save-mode first and then decide if they do not want that automatic behavior.
There are other options available as well. Emacs has sufficient built-in support for session management for my needs, but as you will see from the Emacs Wiki there are nice supplements out there for users who need additional functionality. If you use Emacs frequently—for whatever purpose—then you ought to spend a little time with the desktop functionality. You may come to greatly appreciate it, as I have.