Previous | Up |

Scrolling read-only buffers with Spacebar in Emacs

A small usability improvement

If you are used to scrolling reading material with your spacebar and you dislike Emacs’ unhelpful default (showing Buffer is read-only: #<buffer blah.txt> in the minibuffer) when trying to do so in a read-only buffer, I have a simple solution:

1. Create a function that maps <SPC> to #'scroll-up-command when the current buffer is read-only:

(defun set-read-only-spacebar ()
  (interactive)
  (if buffer-read-only
      (keymap-local-set "<SPC>" #'scroll-up-command)
    (keymap-local-unset "<SPC>")))

2. Plug it into `read-only-mode-hook

(add-hook 'read-only-mode-hook #'set-read-only-spacebar)

The hook fires both when read-only-mode is turned on and off (with C-x C-q by default), so it’s important that the function check the dynamic variable buffer-read-only and set or unset the local keymap appropriately.

Previous | Up |