Is it possible to set the permissions on a buffer before it's written to a file? Or is it necessary to change the permissions after writing it?

(technically this would allow some small time-frame for another user to read the file, I would rather avoid this).

Note that this is for a package that writes files, so I rather not change the default system/emacs wide.

  • Not sure what the scenario is: who is the owner of the emacs process? Is there a shared directory that it is writing into? Why can't you have private directories? All of this seems more appropriate to Unix & Linux SE than to Emacs SE.
    – NickD
    Dec 16, 2021 at 23:39
  • The directory isn't shared, it's in ~/.config/emacs - would this be a case where permissions should be 600?
    – ideasman42
    Dec 17, 2021 at 0:16
  • 2
    So you chmod the directory to 700 and you are done - once and for all.
    – NickD
    Dec 17, 2021 at 2:48
  • 1
    When I want to create a new read-only file once in a while I save it already when it's empty; change the protection of the saved file; and then continue to edit it. The autosave file, the backup file, and the next save will all be protected. It's a bit clumsy perhaps, but is enough if this is something seldom done.
    – pst
    Dec 17, 2021 at 7:08

1 Answer 1


Change your umask so that only your user can read newly–created files. Note that this can be done in the shell before you start Emacs (shells usually provide a command called umask for this purpose), or while Emacs is running by calling set-default-file-modes. See chapter 26.7 Changing File Names and Attributes.

Note that all processes have a umask, so you can use this solution with all programs, not just Emacs.

Edit: your modification changes the nature of the question quite significantly. However, if you take another look at chapter 26.7 Changing File Names and Attributes of the Emacs Lisp manual, you will find a macro called with-file-modes that allows you to do exactly what you want.

Also, NickD’s suggestion to simply change the permissions of the cache directory is a sound one. It doesn’t matter much if the files in a directory are readable or not if other users cannot traverse into it.

  • An issue with this is it applies to all files, I'm have a bug report in a package I maintain that cache files should only be readable by the user. (updating question)
    – ideasman42
    Dec 16, 2021 at 22:35
  • +1 for with-file-modes.
    – NickD
    Dec 17, 2021 at 14:24

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.