I'm using toggle-scroll-bar to show scrollbar on my frame. But is there a way to show scrollbar only on the selected buffer?

I think I can use focus-in-hook to detect when a buffer is active, but I don't know how to show scrollbar on it.

  • You can use the pre-command-hook to record the selected-window in a variable of your choosing (probably global); and, you can compare that prerecorded value with the selected-window that is detected by the post-command-hook. Then, you can tun off scroll-bars in the old buffer/window, and turn it on in the new buffer/window. The arguments to the window scroll bars have changed between Emacs 24, 25 and 26; so, your current version of Emacs is needed to write up a proper solution. Personally, I have modified xdisp.c to automatically add/remove scroll bars and it is possible to ...
    – lawlist
    Sep 28 '18 at 6:37
  • Looking at the code in scroll-bar.el, it looks like scroll bars are all or nothing - either all windows in a frame have scroll bars, or none of them do. You can't have scroll bars on the left window and not on the right window if both are visible. So if that's what you're after I think you might be out of luck. On the other hand, if you want to turn the scroll bars whenever a particular buffer is active, @lawlist s approach might do what you need. In that case, I think window-configuration-change-hook will be more appropriorate than pre-command-hook
    – Tyler
    Sep 28 '18 at 16:56
  • 1
    @Tyler -- the Lisp function I was thinking of is set-window-scroll-bars, which can be used to control window scroll bars on individual windows; however, the arguments to that function have evolved over the past few Emacs versions -- that is why the solution will be based (in part) on whatever Emacs version is being used. Or, a condition that detects the possible arguments for the various versions and programmatically passes the right ones. Scroll bars can be turned on or off in the window, and width and stuff can be controlled too.
    – lawlist
    Sep 28 '18 at 22:37
  • The function window-scroll-bars can be used to see the lay of the land for purposes of deciding (in part) what to do with set-window-scroll-bars.
    – lawlist
    Sep 28 '18 at 22:43

This works for me on Emacs 27.0.50:

(defun update-scroll-bars ()
  (mapc (lambda (win)
          (set-window-scroll-bars win nil))
  (set-window-scroll-bars (selected-window) 10 'right))

(add-hook 'window-configuration-change-hook 'update-scroll-bars)
(add-hook 'buffer-list-update-hook 'update-scroll-bars)

window-configuration-change-hook runs whenever you add or remove windows (i.e., C-x 2, C-x 3, C-x k) and buffer-list-update-hook runs when you select a different window among the ones already present (i.e., C-x b). Using both seems to get the scroll bar changing consistently when I expect it to.

I don't actually use the scroll bar myself, but this provides a nice visual cue as to which window is selected.

  • I have tried it, and it work's like a charm. Thanks
    – rubenfa
    Oct 2 '18 at 16:49

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.