Emacs: Writing to read-only files

Just found this little gem regarding read-only buffers in Emacs:

A buffer visiting a write-protected file is normally read-only.

Here, the purpose is to inform the user that editing the buffer with the aim of saving it in the file may be futile or undesirable. The user who wants to change the buffer text despite this can do so after clearing the read-only flag with C-x C-q.

This is cool, because Perforce makes files read-only by default (to encourage you to open them before you edit them, though you can do this after the fact if you’re careful) and because I’ve been making the files for my web site read-only as a small measure to deter site defacement. I had been using vim for editing my web site files because w! can write to read-only files, but now it looks like I have a convenient way to use Emacs as well.

7 thoughts on “Emacs: Writing to read-only files

  1. I use C-x C-q every few days for roughly the same reason. IIRC, the nearby docs describe a key for “forget this file and open another” which is what you’re supposed to use when you misspelled a filename and got a blank page 🙂

    But, after many (11?) years in emacs, I only JUST TONIGHT learned the key for goto-line. The new emacs release on feisty finally told me after I did M-x goto-line for the millionth time. It’s M-g g (M-g M-g works too).

  2. You know you can modify your .emacs file to create keyboard shortcuts to whatever command you want. I always use C-q for goto-line, since I have Caps Lock remapped to Control.

  3. I use this setting for goto line, placed in my custom.el file:

    (global-set-key (kbd “C-x l”) ‘goto-line)

  4. This is fine and dandy, I use it a lot. However, the command only affects the buffer everytime I save the file, Emacs asks me if I want to overwrite a read-only file. Is there an easy way to remove the write only flag from the file?

  5. Well stupid me, decided to RTFM. dired-do-chmod would be the command to alter the file.

Leave a Reply

Your email address will not be published.