[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: sudo in emacs
From: |
Tassilo Horn |
Subject: |
Re: sudo in emacs |
Date: |
Tue, 10 Jan 2012 16:09:44 +0100 |
User-agent: |
Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.92 (gnu/linux) |
Chris Poole <lists@chrispoole.com> writes:
> On Thu, Jan 5, 2012 at 4:47 PM, Rajanikanth Jammalamadaka
> <rajanikanth@gmail.com> wrote:
>
>> Is there a way I can do this using tramp?
>
> On a file by file basis, I use this:
>
> (defun find-alternative-file-with-sudo ()
> "Open the current buffer with privileges given by `sudo'."
> (interactive)
> (when buffer-file-name
> (find-alternate-file
> (concat "/sudo:root@localhost:"
> buffer-file-name))))
First of all as an answer to the OP, you can just do
C-x C-f /sudo:userid@localhost:/foo/bar/baz.txt RET
Because I'm lazy, I have a similar hack in my .emacs:
--8<---------------cut here---------------start------------->8---
(defun th-find-file-sudo (file)
"Opens FILE with root privileges."
(interactive "F")
(set-buffer (find-file (concat "/sudo::" file))))
(defadvice find-file (around th-find-file activate)
"Open FILENAME using tramp's sudo method if it's read-only."
(if (and (not (file-writable-p (ad-get-arg 0)))
(not (file-remote-p (ad-get-arg 0)))
(y-or-n-p (concat "File "
(ad-get-arg 0)
" is read-only. Open it as root? ")))
(th-find-file-sudo (ad-get-arg 0))
ad-do-it))
--8<---------------cut here---------------end--------------->8---
So if I try to open a file for which I don't have permissions for, I'm
queried if I want to open it as root using the sudo tramp method.
Bye,
Tassilo