GNU Emacs 24.3.1 and want to create a new buffer and execute that directly without using
chmod. Is there a way to change the permission of a saved buffer (file) directly from emacs?
The easy to remember (if you are used to unix) way is
which is an alias of
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
The dired way
Create the file:
Save it to disk:
Open the file in dired mode with
C-x C-j (Emacs 28).
Emacs < 28: If you have
dired-x loaded , 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:
Edit the permissions attributes . You might type
SPACE or the respective letter.
Activate your changes:
RET to edit your file.
Create your file:
M-! > your_file.name
Refresh the dired buffer:
Search the file and type
M (dired-do-chmode). Both octal numeric modes like ‘644’ and symbolic modes like ‘g+w’
RET to edit your file.
: In emacs init file:
(add-hook 'dired-load-hook (function (lambda () (load "dired-x")))).
: You'll need to make sure you've set
t. In your emacs init file:
(setq wdired-allow-to-change-permissions t).
- Open the directory which contains the file. Emacs will automatically be in the dired major mode for this buffer.
- Optionaly press 's' to list last modified files at the top.
- Move the cursor to the file in question and press 'M' which will invoke
- Enter the three digit permission code, like
- Press enter to confirm.
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))