I want some files to be read-only when I open them. Maybe using some kind of header would work?

Parsing the extension would work too but I want to apply to, for example, some ".txt" files so is not very good to apply this rule to all "txt" files.

  • In addition to what @Nick says (which is the answer), can you not change the permissions of those files, themselves? If the file is read-only for the file system then the buffer is read-only when you visit it. – Drew Oct 4 '17 at 18:18
  • Tanks! I didin't think of that because I just want the readonly so I don't change a file when I open it by mistake. Sometimes I'd like to modify it and Nick's solution is more flexible. But it's not a bad idea! – hhaamm Oct 9 '17 at 21:35

If you can add a header to your files, the following should work (it should be the first line in the file):

-*- buffer-read-only: t -*-

If there is a comment convention (e.g. '# ' for a shell script), you should put it in a comment:

# -*- buffer-read-only: t -*-

For shell scripts that must have a hash-bang line as the first line, you can put the header above on line 2: that's the only exception to the rule AFAIK.

For more details, see File Variables which also describes a method to specify file local variables at the end of a file.

| improve this answer | |

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.