5

Emacs has the function other-window that lets you jump between windows. The function's documentation states that it takes an optional argument that tells the function how many windows to jump ahead.

Once upon a time, I defined a keyboard shortcut to call (other-window -1) to jump to the previous window. But at some point (GNU Emacs 25, I think), this stopped working. At first, it did not bother me much, but over time it has become a real nuisance. So if anybody knows a way to replicate that behavior, I would be very grateful!

The function I wrote looks like this:

(defun previous-window ()
    (interactive nil)
    (other-window -1))

And it is bound to a command key like so:

(global-set-key "\C-xp" 'previous-window)

The error message I get:

other-window: Wrong number of arguments: (lambda nil (interactive nil) (other-window -1)), 3

When I run emacs -Q and enter the function and keybinding manually, I get the same error.

Also, I have a function to switch to the previous frame:

(defun prev-frame ()
    (interactive nil)
    (other-frame -1))

This function works as expected.

3
  • 4
    (other-window -1) works fine for me, and the docstring suggests it should. Try it without your init file (emacs -Q). If it works appropriately, it's something in your init file that is interfering.
    – Dan
    Aug 2, 2018 at 15:53
  • What @dan said. The question is unclear so far, so it risks being deleted. Specify what you mean by "stopped working": say what happens. Show your code that binds the key. Remember that a key needs to be bound to a command, which means a function that has an interactive spec.
    – Drew
    Aug 2, 2018 at 16:28
  • I can't reproduce in 26.1 as well.
    – user12563
    Aug 2, 2018 at 17:04

1 Answer 1

4

The problem is the function previous-window does already exist and is called by other-window. You can get info about it with C-h f previous-window (before you define your own previous-window):

previous-window is a built-in function in ‘C source code’.

(previous-window &optional WINDOW MINIBUF ALL-FRAMES)

...

So the fix is simply to call your own function some other name.

(defun myprevious-window ()
    (interactive)
    (other-window -1))

Note: it turns out there is also a function previous-frame so your second function would fail in the same way if you call it previous-frame instead of prev-frame.

2
  • 1
    Thank you! Yes, that did indeed fix it! Now excuse me while I facepalm myself really hard.
    – krylon
    Aug 3, 2018 at 17:53
  • Haha, frankly the error message is not very helpful, I wonder if it's because this is a C function.
    – JeanPierre
    Aug 3, 2018 at 18:00

Your Answer

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

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