10

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?

hasanghaforian
  • 215
  • 4
  • 11
  • 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 08 '16 at 05:27

4 Answers4

18

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

icarus
  • 1,904
  • 1
  • 10
  • 15
  • How to pass the name of current file from buffer ? – aartist Jul 08 '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 https://www.gnu.org/software/emacs/manual/html_node/elisp/Buffer-File-Name.html – icarus Jul 10 '21 at 02: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 01: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 Jun 14 '22 at 21:39
7

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

Dieter.Wilhelm
  • 1,836
  • 14
  • 25
  • 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 08 '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 03 '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. – Dieter.Wilhelm Sep 09 '21 at 07: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.
StefanQ
  • 141
  • 2
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))
Wild Pottok
  • 105
  • 3