I'm using the windmove package to move my cursor between windows. In addition, I want to define a function to move twice, e.g. down first and right second. The following function only do the "down" move but not the "right" move. Any suggestions?

(defun windmove-diagonal ()
  • If you (or another forum participant) would like to write a custom function to move diagonally, then consider having a look at the non-interactive function window-in-direction -- I've never tried, but it looks like (from the doc-string at least) that you can write a function that locates a window diagonally -- either by reference to a corner of a window, or by reference to which window is used as a reference point. M-x describe-function RET window-in-direction RET.
    – lawlist
    May 27, 2016 at 1:04

2 Answers 2


After reading the code in the windmove.el, I found following solution. Thanks for your suggestion, @lawlist

(defun windmove-diagonal (&optional arg)
  (interactive "P")
  (windmove-jump 'left 'down arg))

(defun windmove-jump (dir dir2 &optional arg window)
  (let* ((other-window (windmove-find-other-window dir arg window))
        (other-window2 (windmove-find-other-window dir2 arg other-window)))

    (cond ((null other-window2)
       (error "No window %s from selected window" dir))
      ((and (window-minibuffer-p other-window)
            (not (minibuffer-window-active-p other-window)))
       (error "Minibuffer is inactive"))
       (select-window other-window2)))))

Your code works for me.

I suspect what's happening is that the position of the cursor after moving "down" is not such that you can reach the next desired window by moving "right".

You should be able to compare by performing the commands manually (without moving point in any of the affected buffers).

  • I do the two moves one by one manually, and it works. Which version of emacs are you using? I'm in emacs 24.3. May 27, 2016 at 3:21
  • That certainly seems odd. I'm running 24.5.
    – phils
    May 27, 2016 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.