[emacs-berlin] Automatic sudo

Arne Brasseur arne.brasseur at gmail.com
Mon Aug 24 10:01:40 UTC 2015


Hi everyone,

I would love to get some feedback on this snippet. What it does is,
whenever you try to type in a read-only buffer, it will ask if you want to
re-open the file with sudo. I think it's pretty cool, and it turned out to
be relatively easy to set up.

(defadvice self-insert-command (around automagic-sudo activate)
   (condition-case nil
      ad-do-it
     (buffer-read-only
      (let ((path (buffer-file-name))
            (buff (current-buffer)))
        (when (and path (y-or-n-p "File is read-only, reopen with sudo?"))
          (find-file (concat "/sudo:root at localhost:" path))
          (kill-buffer buff))))))

The only downside I've noticed so far is that e.g. pressing enter does not
work, because that issues the command "newline" instead of
"self-insert-command". You could of course repeat this snippet for newline,
and indent-for-tab-command, etc. Does anyone have a better idea?

Cheers,
Arne

-- 
| @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/20150824/e19bf20a/attachment.html>


More information about the emacs-berlin mailing list