5

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.

2
  • 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
    Commented Oct 4, 2017 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
    Commented Oct 9, 2017 at 21:35

1 Answer 1

9

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.

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.