File modes are the filesystem permission bits for who can read, write or execute a file.
In Lisp there are functions
file-modes to get the modes and
set-file-modes to set them.
If you’re an old-school Unix user and just want to go “chmod 660” etc while editing a file, then you might like the following
(defun chmod (mode) "Set the mode of the current file, in octal, as per the chmod program. If the current file doesn't exist yet the buffer is saved to create it." (interactive "sMode (3 or 4 octal digits): ") (or (string-match "[0-3]?[0-7][0-7][0-7]" mode) (error "Invalid mode")) ;; make sure the file exists (unless (file-exists-p (buffer-file-name)) (save-buffer)) (set-file-modes (buffer-file-name) (string-to-number mode 8)))
If you want “a+r” etc as accepted by the actual
chmod program then you’re probably best to just run that. Lisp:misc-cmds.el has such a command.
But “a+r” and stuff is for whimps. A real man, or a real woman, or a real furry creature from Alpha Centauri should always use octal!
You can set the execute bit on a script with
M-x executable-set-magic. It adds or sets the
#! interpreter line at the start and sets up to make the file executable when you save it. You can customize which bits are set using the
executable-chmod variable. The default is 73 (0111 octal) adding executablility for everyone, you could change it to 64 (0100 octal) to do that only for yourself.
C-c : (
sh-set-shell) as a shortcut, prompting just for one of common shells (
See also MakingScriptsExecutableOnSave.
To make your file
filename readable by just you and your group, use:
(set-file-modes filename #o440) ; equivalent to Linux permission "0440"
The assigned mode, in this case octal
440, must be an integer. The lisp functions
file-attributes can be used to recover the current mode settings. Be aware that Windows filesystems offer less resolved access control, so the example presented may not work as intended in a Windows environment.