Highlight current active window
Asked Answered
O

4

8

Is there an easy way to change the background color of the active window to easily distinguish which window has the input?

I know of hiwin-mode (https://github.com/masutaka/hiwin-mode). But this mode has problems playing nicely with helm.

I also know of color-theme-buffer-local (https://github.com/vic/color-theme-buffer-local) and I am wondering if it can be customized to do what I want.

Orthohydrogen answered 18/10, 2015 at 6:47 Comment(0)
O
11

auto-dim-other-buffers.el

You might want to take a look at auto-dim-other-buffers.el, available from MELPA.

The auto-dim-other-buffers-mode is a global minor mode which makes non-current buffer less prominent making it more apparent which window has a focus.

The preferred way to install the mode is by installing a package from MELPA:

M-x package-install RET auto-dim-other-buffers RET

Once installed, the mode can be turned on (globally) with:

M-x auto-dim-other-buffers-mode RET

To make the mode enabled every time Emacs starts, add the following to Emacs initialisation file (~/.emacs or ~/.emacs.d/init.el):

(add-hook 'after-init-hook (lambda ()
  (when (fboundp 'auto-dim-other-buffers-mode)
    (auto-dim-other-buffers-mode t))))

To configure how dimmed buffers look like, customise auto-dim-other-buffers-face. This can be accomplished by:

M-x customize-face RET auto-dim-other-buffers-face RET

The auto-dim-other-buffers-mode is a global minor mode which makes non-current buffer less prominent making it more apparent which window has a focus.

For a given active window, all other windows that does not show the same buffer will be set to a custom background. I.e., active window and other windows showing the same buffer will have a background color that differs from the rest of the windows. I believe this last part is the specific behaviour you are looking for, based on your comment to the accepted answer.

"Thank you very much. This is very close to what I want. Except for the fact that having two windows opening the same buffer cause both windows to be marked as 'inactive'. It's not super important, but is there an easy way to resolve this?"

Overliberal answered 17/1, 2017 at 21:34 Comment(3)
This is a very nice mode. Thanks!Orthohydrogen
FYI. From its README: Note that despite it’s name, since Emacs 27 the mode operates on windows rather than buffers. I.e. selected window is highlighted and all other windows are dimmed even if they display the same buffer. I have not tried Emacs 27, but it seems its behavior is slightly changedAbrupt
Note that tthis can significanly slow down window switching.Skeptic
D
6
(defun highlight-selected-window ()
  "Highlight selected window with a different background color."
  (walk-windows (lambda (w)
                  (unless (eq w (selected-window)) 
                    (with-current-buffer (window-buffer w)
                      (buffer-face-set '(:background "#111"))))))
  (buffer-face-set 'default))

(add-hook 'buffer-list-update-hook 'highlight-selected-window)

Change the background color ("#111") to suit your taste.

Deconsecrate answered 18/10, 2015 at 10:19 Comment(3)
Thank you very much. This is very close to what I want. Except for the fact that having two windows opening the same buffer cause both windows to be marked as 'inactive'. It's not super important, but is there an easy way to resolve this?Orthohydrogen
How about using `clone-indirect-buffer-other-window' (C-x 4 c) to get the two windows? If that doesn't work for you, you can switch the two expressions to have both windows marked as active.Deconsecrate
I do eval-region on the above code, and then C-x C-b, but the buffer list has the same background, here in emacs 27.Atwater
T
0

I want just to expand the answer given by huaiyuan

His answer works very well for me except for one thing

If I zoom in inside one of the windows with C-x C-+, I will loose the zoom the moment I switch to another window.

To keep the zoom level in every window I am working on, I modified his last line of code so that it specifies the background color of the inactive window like this

(buffer-face-set '(:background "#000"))

where #000 is the RGB value for black

My Code

(defun highlight-selected-window ()
  "Highlight selected window with a different background color."
  (walk-windows (lambda (w)
                  (unless (eq w (selected-window))
                    (with-current-buffer (window-buffer w)
                      (buffer-face-set '(:background "#111"))))))
  (buffer-face-set '(:background "#000")))

(add-hook 'buffer-list-update-hook 'highlight-selected-window)
Tumulus answered 27/12, 2022 at 11:18 Comment(0)
E
0

In 2024, the most popular option to do this seems to be dimmer.el. It has some nice customization options and plays nicely with other modern plugins.

Erased answered 28/4 at 11:59 Comment(2)
similar to dimmer, you also have solaire-modeCarcinogen
thanks for the tip. Seems even more active and also more popular, so will check it out.Erased

© 2022 - 2024 — McMap. All rights reserved.