[emacs-berlin] Automatic sudo

Arne Brasseur arne.brasseur at gmail.com
Wed Aug 26 20:32:00 UTC 2015

Here's the latest version. I switched to the new style of advising
functions, which is actually convenient because now I can use the same
definition to advise multiple functions. It also now keeps track of point,
and retries the attempted command.

(setq last-command-event) is needed because, when prompted for a password,
and then re-executing self-insert-command afterwards, it will insert the
<return> that terminated the password into the buffer, instead of the key
that triggered this advice.

(defun autosudo--edit-command-wrapper (orig-fun &rest args)
  "Ask to re-open a file with sudo if it's read-only and you try to edit it.
Will maintain the position of point, and insert the typed character after
switching to the sudo version."
  (if buffer-read-only
      (let ((path (buffer-file-name))
            (buff (current-buffer))
            (point (point))
            (event last-command-event))
        (when (and path (y-or-n-p "File is read-only, reopen with sudo?"))
          (find-file (concat "/sudo:root at localhost:" path))
          (goto-char point)
          (kill-buffer buff)
          (setq last-command-event event)))))

(advice-add 'self-insert-command :before #'autosudo--edit-command-wrapper)
(advice-add 'newline :before #'autosudo--edit-command-wrapper)
(advice-add 'yank :before #'autosudo--edit-command-wrapper)
(advice-add 'indent-for-tab-command :before

So this is already pretty close. It works fine for self-insert-command and
for tab, but not for newline and yank. Any ideas what the difference would

I also like the idea to make it work on remote servers, but that might be a
next step.

I'd like to make this a repo on Github under Emacs Berlin, and submit it to
MELPA, since I try to avoid custom functions in my config that aren't on
(M)ELPA :)

Til, your version is also cool, for instance Magnar Sveen and Spacemacs
include this function

(defun sudo-edit (&optional arg)
  (interactive "p")
  (if (or arg (not buffer-file-name))
      (find-file (concat "/sudo:root at localhost:" (ido-read-file-name "File:
    (find-alternate-file (concat "/sudo:root at localhost:"

But I like the automatic version personally.


On 24 August 2015 at 23:30, Rasmus <rasmus at gmx.us> wrote:

> Arne Brasseur <arne.brasseur at gmail.com> writes:
> > (defadvice self-insert-command (around automagic-sudo activate)
> Perhaps you'd also want to look into the "nadvice" style of advising,
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Named-Functions.html
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Porting-old-advices.html
> Rasmus
> --
> Hooray!
> _______________________________________________
> emacs-berlin mailing list
> emacs-berlin at emacs-berlin.org
> https://mailb.org/mailman/listinfo/emacs-berlin

| @plexus <https://twitter.com/plexus>  | arnebrasseur.net | The Happy
Lambda <https://leanpub.com/happylambda> |
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailb.org/pipermail/emacs-berlin/attachments/20150826/67e5ea37/attachment.html>

More information about the emacs-berlin mailing list