Yi Tang Data Science and Emacs

Move Between Windows in Emacs using windmove

Table of Contents

Started Seeing

The good thing about Emacs is that you can always tweak it to suit your needs. For years I’ve been doing it for productivity reasons. Now for the first time, I’m doing it for health reasons.

Life can be sht sometimes, when I was in my mid 20s, I was reshaping every aspects of my life for good. But optician told me my vision can only get worse. I wasn’t paying much attention, busy with my first job and learning.

Last month, I was told my right eye’s vision got whole point worse, whatever that means. Now I’m wearing a new pair of glasses, seeing the world in 4K using both eyes, noticing so much details. It makes the world so vibrate and exciting. It comes with a price though, my eyes get tired quickly, and it become so easy to get annoyed by little things.

One of them is switching windows in Emacs. Even though I am in the period of calibrating to the new glasses, I decided to take some actions.

Ace-Window

Depends on the complexity of the tasks, I usually have about 4-8 windows laid on my 32 inch monitor. If that’s not enough, I would have an additional frame of similar windows layout, doubling the number of windows to 8-16.

So I found myself switching between windows all the time. The action itself is straightforward with ace-window.

The process can be breakdwon into five steps:

  1. Invoke ace-window command by pressing F2 key,
  2. The Emacs buffers fade-in,
  3. A red number pops-up at the top left corner of each window,
  4. I press the number key to switch the window it associates with,
  5. After that, the content in each Emacs buffer are brought back.

This gif from ace-window git repo demonstrates the process. img

This approach depends on visual feedback - I have to look at the corner of the window to see the number. Also, the screen flashes twice during the process.

I tried removing the background dimming, increase the font size of the number to make it easier to see, and bunch of other tweaks.

In the end, my eyes were not satisfied.

Windmove

So I started looking for alternative approaches and found windmove which is built-in.

The idea is simple - keep move to the adjacent window by move left, right, up, or down until it arrives at the window I want.

So it uses the relative location between windows instead of assigning each window a unique number and then using the number for switching.

Is it really better? Well with this approach, I use my eyes a lot less as I do not have to look for the number. Plus, I feel this is more nature as I do not need to work out the directions, somehow I just know I need to move right twice or whatever to get to the destination.

The only issue I had so far is the conflicts with org-mode’s calendar. I like the keybinding in org-mode, so I disabled windmove in org-mode’s calendar with the help from this stackoverflow question.

The following five lines of code is all I need to use windmove.

(windmove-default-keybindings)
(define-key org-read-date-minibuffer-local-map (kbd "<left>") (lambda () (interactive) (org-eval-in-calendar '(calendar-backward-day 1))))
(define-key org-read-date-minibuffer-local-map (kbd "<right>") (lambda () (interactive) (org-eval-in-calendar '(calendar-forward-day 1))))
(define-key org-read-date-minibuffer-local-map (kbd "<up>") (lambda () (interactive) (org-eval-in-calendar '(calendar-backward-week 1))))
(define-key org-read-date-minibuffer-local-map (kbd "<down>") (lambda () (interactive) (org-eval-in-calendar '(calendar-forward-week 1))))

I created an git branch for switching from ace-window to windmove. I would try it for a month before merge it into master branch.

Back to where it started

After using it for few days, I realised this is the very package I used for switch windows back in 2014 when I started learning Emacs. I later then switched to ace-window because it looks pretty cool.

Life is changing, my perspectives are changing, so is my Emacs configuration. This time, it goes back to where I started 8 years ago.

If you have any questions or comments, please post them below. If you liked this post, you can share it with your followers or follow me on Twitter!
comments powered by Disqus