Take the 2-minute tour ×

I like to have my coding buffers read-only when I am not editing them (like Vi modes edit, read-only). For that, I can use C-x C-q for turning a emacs buffer on/off the writable status. Seems that this write-locking feature has some inheritance from RCS or similar way of file-loking/revision-system so working with files under Version Control does not work:

C-x C-q

I don't want to check in/out I want only to prevent "cat-typing". I can write a macro flip-flop find-file-read-only '/'find-file ' and give some key-binding to it but I am sure there would be a solution inside emacs or .el written already. Any suggestion?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

You can use view-mode instead.

share|improve this answer
thanks that is what I wanted. Also I have realised that C-x C-q works in emacs22 (my clusters where in emacs21 until recently). They give you the warning/suggestion to use c-x v v but it locks the file anyway. Now I only need to remap c-x c-q to view-mode. –  Pablo Marin-Garcia Mar 1 '11 at 13:54
The warning is just telling you that the behaviour changed. C-x C-q used to change its behaviour if the directory was under version control, but in Emacs 22 it was decided that leaving it with its normal behaviour was more useful, especially since modern vc systems do not require explicit checkout by default, so the association between being read-only and version control is no longer relevant. –  JSON Mar 2 '11 at 6:19
add comment

or you can hook find-file

(add-hook 'find-file-hook
  '(lambda ()
     (when (and (buffer-file-name)
        (file-writable-p (buffer-file-name)))
       (message "Toggle to read-only for existing file")
       (toggle-read-only 1))))

and using C-x C-q to change the read-only state of the file

share|improve this answer
thanks abden for your code –  Pablo Marin-Garcia Apr 26 '11 at 12:46
add comment

You can set a file variable, settting the value of buffer-read-only to a positive value.

You need to set it on the first 2 lines of a file. For example, here I set it to one into a python buffer:

# -*- coding: utf-8 ; buffer-read-only: 1 ; -*-

Like that, you won't need to remember after each opening to set it manually.

share|improve this answer
add comment

Your Answer


