sudo-save.el is a quick package to save files as root on the local box. If you have ‘sudo’ installed and you have permission to edit files (and can cache passwords) then Emacs will run chmod on the file before and after you save it. This comes in handy for a local laptop when TrampMode and HoboMode are overkill. On my machine at least these have characteristics that are undesirable (including IO block).
Thanks to DryiceLiu for saving this package, as the original url is dead. His mirror:
sudo-save must be loaded before opening the file you want to edit via sudo. Place sudo-save.el somewhere in your LoadPath. If you want it ready for use all the time, add
(require 'sudo-save) to your
.emacs. Else load it up before use with
M-x load-library RET sudo-save RET. Open files as usual with
C-x C-f and save files as usual with
It appears that sudo-save only works if sudo already has your password cached or you have
There is another sudo wrapper here:
It handles password queries from sudo and allows you to find files you could not otherwise open. – ScottVokes?
For sudo.el: Integration with standard file save hooks:
(defun sudo-before-save-hook () (set (make-local-variable 'sudo:file) (buffer-file-name)) (when sudo:file (unless(file-writable-p sudo:file) (set (make-local-variable 'sudo:old-owner-uid) (nth 2 (file-attributes sudo:file))) (when (numberp sudo:old-owner-uid) (unless (= (user-uid) sudo:old-owner-uid) (when (y-or-n-p (format "File %s is owned by %s, save it with sudo? " (file-name-nondirectory sudo:file) (user-login-name sudo:old-owner-uid))) (sudo-chown-file (int-to-string (user-uid)) (sudo-quoting sudo:file)) (add-hook 'after-save-hook (lambda () (sudo-chown-file (int-to-string sudo:old-owner-uid) (sudo-quoting sudo:file)) (if sudo-clear-password-always (sudo-kill-password-timeout))) nil ;; not append t ;; buffer local hook ))))))) (add-hook 'before-save-hook 'sudo-before-save-hook)