开发者

How do I make Emacs' other-window command ignore terminal windows?

开发者 https://www.devze.com 2023-02-09 04:36 出处:网络
Emacs does an okay job of being a window manager. I\'ve been splitting up my Emacs frame like this: +---------------------------+

Emacs does an okay job of being a window manager. I've been splitting up my Emacs frame like this:

+---------------------------+
|             |             |
|             |             |
|             |      B      |
|      A      |             |
|             |             |
|             |             |
|             |-------------|
|             |      C      |
+---------------------------+

C is usually a terminal with some kind of long-running process, like a web server or daemon. Occasionally I'll mov开发者_JS百科e the point there to restart the daemon, but most of the time I'd like to swap only between A and B. How can I make this convenient?


There's nothing built-in to do what you want. You can use the following code to do what you want (just customize the regular expression to match the name of the buffer(s) you want to avoid).

Note: the my-other-window doesn't implement all the features other-window does, that is left as an exercise for the reader.

my-other-window will try to switch to a window whose buffer doesn't match the avoid-window-regexp. If there's no such window available, then it just switches to the next one.

(require 'cl)
(defvar avoid-window-regexp "^[0-9]$")
(defun my-other-window ()
  "Similar to 'other-window, only try to avoid windows whose buffers match avoid-window-regexp"
  (interactive)
  (let* ((window-list (delq (selected-window) (window-list)))
         (filtered-window-list (remove-if
                                (lambda (w)
                                  (string-match-p avoid-window-regexp (buffer-name (window-buffer w))))
                                window-list)))
    (if filtered-window-list
        (select-window (car filtered-window-list))
      (and window-list
           (select-window (car window-list))))))

And bind it appropriately:

(global-set-key (kbd "C-x o") 'my-other-window)


This works for eshell:

(add-hook 'eshell-mode-hook (lambda () (set-window-parameter (first (window-list)) 'no-other-window t)))

Setting no-other-window to non-nil makes the standard other-window function skip this. See other-window docs with C-h C-f other-window

There might be a better way to get the current window, but I don't know it yet.


Not the exact answer but I use windmove-mode for that which allow you to use the arrow to visually move between windows and then doing Control-Left or Control-Right to move only between A and B.

Other than that you probably can redefine other-buffer function with something that take buffer-list ignoring some patters when switch-to-buffer to it but I didn't write the code handy.


For the lazy, I use something like this in my .emacs

(fset 'doublejump
      "\C-u2\C-xo")
(global-set-key (kbd "C-c o") 'doublejump)

and alternate between using single and double buffer switches.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号