Watch the video on YouTube!
There's a question I see pretty often in the System Crafters Discord:
"How do I get a buffer list for only the files or buffers I want to see?"
In Emacs, running switch-to-buffer or ibuffer shows you every single buffer that is open in Emacs which can be a little overwhelming in long-running Emacs sessions.
The perspective.el package provides a convenient way to define individual "perspectives" for managing separate sets of buffers and windows. You can think of perspectives like the "workspace" concept you see in some window managers.
There are other alternatives to this package that have a different take on the workspace concept, we may cover them in future videos.
I've been using perspective.el for a few months now and it has become an important part of my workflow!
If you use use-package with package.el, you can copy the following Emacs Lisp into your init.el file and execute it for the first time with C-M-x.
(use-package perspective :ensure t ; use `:straight t` if using straight.el! :bind (("C-x k" . persp-kill-buffer*)) :init (persp-mode))
This will install the package and enable it immediately when Emacs starts up.
TIP: By default the prefix for perspective.el bindings is C-x x but you can change it by setting the variable persp-mode-prefix-key.
We'll try out perspective.el in a basic Emacs configuration so that we can learn how to use it!
By default, perspective.el will create a perspective called "main" in every frame that Emacs creates. In the very first frame, all of Emacs' starting buffers will be in the "main" perspective of that frame.
NOTE: When you create a new frame, a new "main" perspective will be created but it is NOT the same as the "main" perspective in other frames! They have the same name but they are actually different.
Whenever you open a file or some other buffer inside of a perspective, the buffer then becomes attached to that perspective and won't show up in other perspectives unless it is explicitly added to them.
TIP: The name of the default perspective can be customized with the variable persp-initial-frame-name.
To list all buffers in the current perspective, use the normal switch-to-buffer command! This should work great with Emacs' built-in completion systems, Helm, and Vertico.
You can also choose from all open buffers and automatically switch to the perspective that "owns" the buffer with persp-switch-to-buffer (C-x x b).
Having only one perspective isn't very useful, though, so let's create another!
The persp-switch command (C-x x s) will prompt you for the name of a perspective to switch to. If you enter a name for a perspective that doesn't exist yet, it will be created.
We can now list the buffers of this new perspective using switch-to-buffer or any of the commands mentioned before.
TIP: All perspectives get their own *scratch* buffer! This can be useful for keeping temporary notes within a specific workspace.
You can select from a list of all perspectives for the current frame using the persp-switch command (C-x x s).
There are also a couple of helpful commands for cycling between perspectives:
This is a good time to point out that the list of perspectives for the current frame are displayed in the mode line!
You can also select between perspectives using the number row keys with C-x x 1 through C-x x 0 (10).
Sometimes you'll end up in this situation: you open a file in a perspective and then realize that you should have opened it in a more appropriate perspective.
There are a few ways to get that buffer into the desired perspective:
If you want to remove a buffer from a perspective, you can use the persp-remove (C-x x k) command. Once you select a buffer, it will only be removed from the current perspective.
If you're done with a particular perspective, you can "kill" it using persp-kill (C-x x c).
The documentation for persp-kill says that only buffers associated with the killed perspective and no others will be killed. That hasn't been true in my experience, they all seem to remain active, just not visible in other perspectives!
In a very recent commit a new command named persp-kill-others was added for quickly killing all perspectives in the frame except the current one.
That was just a brief overview of the perspective.el package to get you started! In future videos we'll cover more ways to customize the your perspective workflow and also how to integrate it with EXWM and Projectile!