Emacs Desktops

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.

My Preference

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:

  1. C-c d c for desktop-clear
  2. C-c d d for desktop-change-dir
  3. C-c d s for desktop-save

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:

  1. It will automatically save your current session as a desktop when you close Emacs.

  2. 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 desktop-path.

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.


One thought on “Emacs Desktops

  1. Tom Willemse, a programmer who many times has graciously helped me with PHP Mode for Emacs, created a useful package for managing a registry of desktops:


    It does a great job at automating another part of the desktop process which I do manually: it helps you keep track of where your Emacs desktops actually are. I use a rigid directory structure for my projects and desktop files, so remembering the location of each desktop has simply become the result of a habit in my workflow. But Tom’s desktop registry package is still useful even in that situation, and is especially more useful to users who spread around desktop files more liberally.

Add Your Thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s