Emacs' windows navigations and some Emacs zen
As I was looking more into widows management in Emacs, particularly viewing System Crafters' video about the topic which I mentioned in my previous post, I learned a couple of things (or maybe re-learned? I probably used to know about them at some point in the past).
One of my issues with windows in Emacs right now is that whenever I open Dired, it takes over my current window. I often just want to view my files in the directory my current buffer is in.
The answer is simple: instead of opening Dired (C-x d
), just “jump” into Dired in another window, which is dired-jump-other-window
or C-x 4 C-j
. This improves my workflows twice: first, I usually want to open Dired in the directory the buffer I’m working in is, and second, I want it in another window. With this command, I don’t need to specify the directory; it just takes me where I want1.
Also, as I was writing this post, I realized another problem with Emacs windows placement was actually my problem all along (user error dance, anyone?): C-x b
is bound to consult-buffer-other-window
on my system, which does what it says, opening the buffer I want in the other window. I thought this was the command to open a buffer, forgetting that there’s such a thing as opening a buffer in the other window. Using the logic I just learned, it would probably make more sense to bind consult-buffer-other-window
to C-x 4 b
(for me, it runs the default Emacs switch-to-buffer-other-window
, which I don’t use2) and then consult-buffer
(which is not bound to anything for me) to be C-x b
. This will disrupt my muscle memory for a while, but eventually it will lead to a better workflow.
Another nice thing I didn’t know and learned from watching this video is that I can scroll up and down in the other window while still keeping the focus on the current buffer with M-pgup
and M-pgdn
. This is nice if I want to read help documentation while working on something, reading definitions with Emacs' built-in dictionary, or maybe using Occur or something similar, where just reading for reference is enough.
Then we have a built-in way to move between windows that I was looking for a while: windmove. Turns out it’s been there since Emacs 21, which means that for me, it was there all this time, and I didn’t know about it. Windmove lets you switch between windows using the shift and arrow keys by default, so that S-up
would move you one window up, while S-down
will move you down. You need to enable it and then enable its default keybindings, or assign it alternative keybindings.
Looking at these key-bindings, I can see why it’s turned off by default. Shift plus an arrow key is used extensively in org-mode, for example, so you can’t use these keys. Using something else, say, C+M+left
or C+M+right
, is not good either because on Linux, this will “swish” your workplaces (“spaces” in macOS), while the meta key plus an arrow is also taken by something else in Emacs… you get the idea, finding the keys to use is getting a bit problematic. You need to figure out what you can give up, or find a creative shortcut that works for you specifically. However, this seems too useful to pass up, so I think I’m going to dedicate keys to it very soon.
The video also goes over windmove’s sibling, windmove-swap-states-[direction]
, which switches the windows in the direction you tell it. So if I am working on this post in a window that is above a list of links I’m using as reference in the window below in another buffer, I can use windmove-swap-states-down
to switch. Now I will be working in the lower window, and the list of links will be in the window above. This is good if I want my list of files in Dired to the window to the right, but dired-jump-other-window
opened it in the window below, for example. Nice, it saves a few C-x 0
and C-x 1
maneuvers.
Then there are the external packages, of course.
ace window is probably the most popular one, and this video provides a brief demonstration of it. The idea is that it numbers the windows for you on the screen, and then you select a number to jump to a window. Another package I learned about is winum, which is built on top of this idea, but it displays the window number in the minibuffer of each window all the time, and then it’s a matter of C-x #
to jump to that window. It integrates into Emacs better in my opinion, and if you use many windows, it probably makes sense. I usually have no more than four windows open at once, so using the built-in windmove
is probably better.
Alright, I’m off to rebind some keys and do some tweaking! Then we will see how much I need to mess with Emacs’s built-in logic for windows placement. It might just make a little more sense now.
Footnotes
1: it’s building on dired-jump
(C-x C-j
), which opens dired in the directory the buffer I’m in. So, if I’m working on bills.org
, which is under ~/Documents/personal/
, dired-jump will open dired inside ~/Documents/personal/
. The problem for me is that it will take over my current buffer (where I have bills.org
open), which I don’t want; this is where dired-jump-other-window
comes in: it will open Dired in a separate window.
2: this Emacs keys and windows management rabbit hole makes me re-appriciate Emacs’s default key binding. They’re there for a reason. They might not make sense at start… or maybe for several years, depending on how often and how much you use Emacs and what for, but now I see how C-x 4
is an entire family of key combinations tied to “other window” functions and then other key bindings start to make sense. For some of you veteran Emacs users, this might be obvious, but for me, that’s a slap on the forehead with a “Ohhh! So this is why this keybinding is like this!”