2

Sometimes I want to prevent myself from accidentally editing a buffer. How can I write-protect the buffer?

3 Answers 3

3

You can toggle read-only-mode on a buffer with C-x C-q: the mode line changes from -:--- README.org ... to -:%%- README.org ... (where README.org is the name of the file that the buffer is visiting, although read-only-mode applies to any buffer, including buffers that are not associated with files). That indicates that the buffer is read-only: trying to modify the buffer afterwards (e.g. by typing RET) elicits the message newline: Buffer is read-only: #<buffer README.org>.

See the description of read-only-mode with C-h f read-only-mode. There are several wrinkles described there that I do not describe above. For a description of the mode line (including the read-only bits described above), see the Mode line section in the Emacs manual.

read-only-mode is a minor mode and like all minor modes, running the mode function (with M-x read-only-mode or more simply with the keybinding C-x C-q) toggles the mode from off to on or vice versa. Underneath the covers, the mechanism uses the buffer-read-only buffer-local variable that @shynur describes in his answer but there is generally no need to muck around with that interactively. Changing read-only status from a program is a different matter: the doc string of the mode is a must-read in that case.

0
3

Buffer local variable: buffer-read-only


Usages:

  • Put it in your file:

    Your text...
    
    Local Variables:
    buffer-read-only: t
    End:
    

    Save your file and then M-x normal-mode.

  • Set it manually, say M-: (setq-local buffer-read-only t) (setq-local is unnecessary, but it is more readable than setq when you set a buffer local variable).

6
  • 1
    Great idea. You could even define a keybinding for (setq-local buffer-read-only t). Aug 4, 2023 at 10:22
  • 2
    C-x C-q is bound to read-only-mode, a minor mode, so you can use it to toggle the read-only state of a buffer.
    – NickD
    Aug 4, 2023 at 11:49
  • @NickD If you provide this as an answer, it would be very helpful. I could do it, but I don't want to lay claim to your idea. Aug 7, 2023 at 10:30
  • henning: NickD already told you the standard key binding for the standard command to do that. There's no reason to add custom code to this answer.
    – phils
    Aug 7, 2023 at 11:43
  • 1
    henning: Don't be so sure. Your code wasn't triggering the message, but it's setting the same variable: read-only-mode specifically toggles buffer-read-only (old-timers know this command as toggle-read-only; it was reframed as read-only-mode a while ago, but there was never a read-only-mode variable). Try alternating your command and read-only-mode and you'll see that they're both controlling the same thing. Other than the toggling behaviour, the main difference relates to the view-read-only user option (but that's not enabled by default).
    – phils
    Aug 7, 2023 at 14:00
0

Just toggle view-mode, which "provides commands for scrolling through the buffer conveniently but not for changing it". Typing q disables view-mode and returns to the previous buffer. Typing e disables view-mode and keeps the current buffer visible, e.g. for editing.

4
  • If it is possible for you to edit accidentally, the same is true for you to press e accidentally.
    – shynur
    Aug 4, 2023 at 9:33
  • That is true, but less likely. Assuming (somewhat unrealistically) that all keys on a keyboard have the same probably of being hit accidentally, and assuming the number of keys is 100, it is even 100 times less likely. But I really welcome better suggestions. Aug 4, 2023 at 10:12
  • "better suggestion" -- buffer-read-only
    – shynur
    Aug 4, 2023 at 10:13
  • Write it up, and if it works, I'll gladly accept. Aug 4, 2023 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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