10

In emacs, more often than i like, I end up with 4 windows or more.

I'll use that example:

+--------|-----------+
|        |           |
|   1    |   2       |
|        |           |
+--------+-----------+
|    3   |     4     |
|        +-----------+
|        |     5     |
+--------+-----------+

Lots of function can do action in other-window (usually same window pointed by C-x o). Specially useful are scroll-other-window. However, when 4 or more windows are opened, other window is unlikely to be the one I want.

Is there a easy way to manage to do set other-window without changing window/buffer layout ? Of course, it's doable using a bunch of C-x 4 b and C-b but I do not want to change the layout. For example:

  • if editing in window 4 in above picture, is there an easy way to set C-M-v to scroll the window 1?
  • Or simply, suppose the the focus in on window 3 , how to set other-windowto point to window 2 ?
7

If I read your question correctly, you are mainly interested in operating on buffers displayed in other windows quickly (while preserving the current window layout).

Changing the window that other-window points to is one way of achieving that, but I'd like to suggest an alternative workflow:

Unlike other-window, ace-window allows you to quickly jump to a specific window: When you call ace-window, it shows a unique number in each window. To jump to a specific window, you simply type the corresponding number. If you only have two windows, it puts point in the other window without asking you to type a number. This screencast demonstrates switching to another window, scrolling it, and switching back to the original window:

Demonstration of ace-window

If you bind ace-window to C-x o, you don't have to memorize an additional key binding, and it will feel like you are using a supercharged version of other-window.

Also, if you touch-type and/or prefer not to move your fingers too far away from the home row, you can instruct ace-window to "number" windows using letters instead of numbers (see below).

ace-window is available in MELPA. After installing it via M-x package-install RET ace-window RET, you can put the following in your init-file to set it up as described above:

(global-set-key (kbd "C-x o") 'ace-window)
(setq aw-keys '(?a ?b ?c ?d ?e ?f ?g ?h ?i))
  • Thanks for demo, I've seen this package on other sites and was wondering how it works.I actually can switch window easily as I have bound M-1 M-2 .. to select window number 1, 2 using the window-number package – Lompik Oct 10 '14 at 16:09
1

From a quick review of the source of other-window it looks like it's actually dependent on the window returned by next-window, which is defined in c. Unfortunately scroll-other-window is also defined in c, and presumably calls next-window from there. I don't think it's easy to override that.

However, switch-to-buffer-other-window appears to use pop-to-buffer, so you may have some luck there.

1

There is no easy way to manipulate the window list, so the result returned by other-window is more or less fixed. This is not a bad thing per se, as it allows for consistency when navigating windows. Regarding the two scenarios you described, with some elisp or macros you can suit your workflow.

if editing in window 4 in above picture, is there an easy way to set C-M-v to scroll the window 1?

You can use a simple snippet like the one below to use other-window the appropriate number of times, scroll down and move back to the original window.

 (defun my-scroll-down-other-window (&optional arg)
 "Like `scroll-other-window' but takes a prefix argument 
 that indicates which window to scroll."
  (interactive "p")
  (save-selected-window
    (other-window arg)
    (scroll-down)))

You can pass a prefix argument to specify which window to scroll. As you can infer, it uses the inbuilt mechanism in other-window to skip arg number of windows before scrolling. The save-selected-window ensures focus will return to the original window. The documentation of other-window can provide more information on modifying this snippet to suit your behavior.

Or simply, suppose the the focus in on window 3 , how to set other-window to point to window 2 ?

Again use a prefix-arg as in C-3 C-x o when calling other-window. The actual number to provide will vary depending upon which order you split them, and will not change as long as you do not create new windows. Alternately, you can use convenience packages such as window-number or ace-window from MELPA to switch selected windows fast.

  • right, no easy way to manipulate window list. I have a hard time using prefixes. Feels like it add another unreachable key and slows thing down – Lompik Oct 10 '14 at 16:15
  • That is what keyboard macros/keybindings are for. If this is a regular part of your work flow, you can easily define a prefix map based keybinding e.g. <f7> n to scroll the nth window. Create temporary one key bindings with keyboard macros. – Vamsi Oct 10 '14 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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