11

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?

1
  • 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
    Commented Dec 8, 2016 at 5:27

4 Answers 4

21

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

M-x chmod

which is an alias of set-file-modes

Then there is the question of how to get the filename. The default is the current file but the prompt is misleading as it shows the current directory. You can see the correct filename using "future history". The standard emacs completion (at least for emacs 29) has a history of completions, and it puts the filename as the first thing into the future. The effect of this is you type on a PC keyboard where Alt is the Meta key

Altx c h m o d Return to enter the command
Altn Return to pull the filename from the future history
+ x Return to switch on execute permissions

4
  • How to pass the name of current file from buffer ?
    – aartist
    Commented Jul 8, 2021 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
    Commented Jul 10, 2021 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
    Commented Jul 11, 2021 at 1:54
  • Just hit RET when prompted for the file. When you're visiting a file in the current buffer, that file is the default when you're prompted for the file name.
    – Drew
    Commented Jun 14, 2022 at 21:39
8

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).

3
  • Could you edit this to present the defaults first and the extensions after? I'm mashing keys over here and nothing is working.
    – daveloyall
    Commented Nov 8, 2019 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
    Commented Sep 3, 2021 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. Commented Sep 9, 2021 at 7:10
2
  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.
0

Expanding on the other answers, here is a piece of Elisp, allowing to automate this action:

(defun chmod-this-file ()
  "chmod the file corresponding to the current buffer."
  (interactive)
  (setq modes (read-file-modes "File modes (octal or symbolic): " buffer-file-name))
  (chmod buffer-file-name modes))
3
  • Or just use built-in command chmod. Just hit return when prompted for the file name - the file currently visited is the default.
    – Drew
    Commented Jun 14, 2022 at 21:37
  • 1
    Thanks; this is right. It did not appear clearly to me, owing to the completion framework used here. Commented Jun 15, 2022 at 12:40
  • Your answer is still useful.
    – Drew
    Commented Jun 15, 2022 at 14:21

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.