I use GNU Emacs 24.3.1 and want to create a new buffer and execute that directly without using terminal and chmod. Is there a way to change the permission of a saved buffer (file) directly from emacs?

  • 3
    A new buffer is not a file, unless you save it to the hard drive and create a file. See the function set-file-modes. And to see an implementation of that function, have a look at dired-do-chmod.
    – lawlist
    Dec 8 '16 at 5:27

The easy to remember (if you are used to unix) way is

M-x chmod

which is an alias of set-file-modes

  • How to pass the name of current file from buffer ?
    – aartist
    Jul 8 '21 at 13:59
  • @aartist In general buffers do not have associated files, for example the messages and scratch buffers are created initially without them. The function or variable (Don't you love lisp-2 systems?) you want is buffer-file-name described in gnu.org/software/emacs/manual/html_node/elisp/…
    – icarus
    Jul 10 '21 at 2:23
  • agree.. I want to do chmod only if buffer has associated file with it, so ignoring other files for function to be applied or even activated.
    – aartist
    Jul 11 '21 at 1:54

The dired way

Create the file: C-x C-f

Save it to disk: C-x C-s

Open the file in dired mode with C-x C-j (Emacs 28).

Emacs < 28: If you have dired-x loaded [1], jump to the file in dired using C-x C-j. Otherwise, you can simply open the directory in dired with C-x C-f RET.

Enter WDired mode: C-x C-q

Edit the permissions attributes [2]. You might type SPACE or the respective letter.

Activate your changes: C-c C-c.

Type RET to edit your file.

Alternative workflow

Enter dired: C-x d

Create your file: M-! > your_file.name RET

Refresh the dired buffer: g

Search the file and type M (dired-do-chmode). Both octal numeric modes like ‘644’ and symbolic modes like ‘g+w’ are supported.

Type RET to edit your file.

[1]: In emacs init file: (add-hook 'dired-load-hook (function (lambda () (load "dired-x")))).

[2]: You'll need to make sure you've set wdired-allow-to-change-permissions to t. In your emacs init file: (setq wdired-allow-to-change-permissions t).

  • Could you edit this to present the defaults first and the extensions after? I'm mashing keys over here and nothing is working.
    – daveloyall
    Nov 8 '19 at 22:27
  • dired-x is very handy. Turn it on by adding to your init file: (add-hook 'dired-load-hook (function (lambda () (load "dired-x")))). In order to be able to edit the file perms in WDired, you also need to set (setq wdired-allow-to-change-permissions t). Then you should be able to directly edit the permissions fields.
    – josaphatv
    Sep 3 '21 at 17:36
  • Hello @josaphatv, Thank you very much for your suggestions I updated the answer with your improvements and updated them with Emacs 28 developments. Sep 9 '21 at 7:10
  1. Open the directory which contains the file. Emacs will automatically be in the dired major mode for this buffer.
  2. Optionaly press 's' to list last modified files at the top.
  3. Move the cursor to the file in question and press 'M' which will invoke dired-do-chmod.
  4. Enter the three digit permission code, like 744 for rwx-r-r.
  5. Press enter to confirm.

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.