Gaining some perspective in Emacs

2 minute read

A clean desk is a clean mind. If you are like me and do a lot of work inside of Emacs you will have a buffer list that is quite large. Pressing C-x b will show you a long list of buffers you can switch to. It would be really nice if we could limit the buffers to just those that you are using for your current project, right?

As always, there is a package to do just that!

Install the perspective package via M-x list-packages and then select perspective and press i x to install it. Emacs will go ahead and download the file, extract it, compile it and place it somewhere where Emacs can find it each time you start it up.

The basic addition to your init.el file will be to load up the perspective code.

;;; Setup perspectives, or workspaces, to switch between
(require 'perspective)

;; Enable perspective mode
(persp-mode t)

As always there is a small modification to how it all works. What we want is to define workspaces with a starter file, this could be a build script or a project todo list or even a lisp file that is the central place for your project. In order to do this we will first define a macro to deal with our special use-case.

;; loading code for our custom perspectives
;; taken from Magnar Sveen
(defmacro custom-persp (name &rest body)
  `(let ((initialize (not (gethash ,name perspectives-hash)))
         (current-perspective persp-curr))
     (persp-switch ,name)
     (when initialize ,@body)
     (setq persp-last current-perspective)))

;; Jump to last perspective
;; taken from Magnar Sveen
(defun custom-persp-last ()
  (interactive)
  (persp-switch (persp-name persp-last)))

;; Easily switch to your last perspective
(define-key persp-mode-map (kbd "C-x p -") 'custom-persp-last)

Lets go ahead and define a perspective for editing your emacs init.el file. We do this by defining a defun that can be called interactively. In this defun we use the custom-persp macro in which we give the perspective a name, "emacs" in this case, and we specify the action to take when switching to this perspective. In my case that would be to open (find-file) my Emacs init.el file.

(defun custom-persp/emacs ()
  (interactive)
  (custom-persp "emacs"
                (find-file "~/.emacs.d/init.el")))

(define-key persp-mode-map (kbd "C-x p e") 'custom-persp/emacs)

Now, when you press C-x p e you will be greeted by your init.el file and the rest of your buffer list will be empty. If you open other files as part of your Emacs configuration, they will be added to the buffer list of this perspective, but not any other. How cool is that?

By invoking M-x persp-switch, or C-x p - if you want to switch to your last perspective, you can switch back to the main perspective where all your other files were.

In the emacs mode line (for which I use powerline) you will see the different perspectives that you have open in your session.

modeline

In my case you see [emacs|main|pao] with emacs being highlighted to indicate that it is the active perspective.

Tags: ,

Updated: