Tramp is a page that Emacs uses to access remote files. Tramp is very easy to use: chances are it’s turned on in your version of emacs without you knowing about it even if you do. It requires very minimal customization, although there are many options if you do M-x customize-group tramp: the only line in my init.el file that customizes tramp is the following, which tells tramp to use ssh for accessing remote files:
(setq tramp-default-method "ssh")
To use tramp to edit a file on a remote server, just use C-x C-f to find the file as normal, but then enter //ssh:username@hostname:path/to/file. This will open the file as if it were on your computer. Saving the file will save it to the remote computer. This is very useful if either the file is on a computer that doesn’t have emacs installed or has a very slow connection and so you’d rather that emacs be running on your computer.
Tramp can also be used to access files on your computer as another user. This is typically used to edit files as root when you are running as a regular user - this prevents having to run a sudo’d emacs and is a lot safer than the alternative. There are two functions I use to help with this: one gotten from the Emacs Starter Kit, and another one I wrote:
(defun sudo-edit (&optional arg) (interactive "p") (if arg (find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: "))) (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name)))) (defun sudo-edit-current-file () (interactive) (find-alternate-file (concat "/sudo:root@localhost:" (buffer-file-name (current-buffer))))) (global-set-key (kbd "C-c C-r") 'sudo-edit-current-file)
These two functions are just easy ways of opening files as root. The first function, sudo-edit, will prompt for a filename and then open that file as sudo. sudo-edit-current-file, will open the current file as sudo without prompting. sudo-edit-current-file will automatically close the non-sudo’d buffer; sudo-edit will only do this if given a prefix argument. This is useful fairly frequently, so I gave it a keybinding of C-c C-r to just open the current buffer as sudo.
Dude, that rocks! Thanks very much for the tip…that’s gone right into my .emacs.
A little update:
(defun sudo-edit-current-file ()
(interactive)
(let ((pos (point)))
(find-alternate-file (concat “/sudo:root@localhost:” (buffer-file-name (current-buffer))))
(goto-char pos)))
Tramp is indeed awesome. I used mg, a micro emacs, from the command-line w/ sudo for quite a while. When I learned about tramp, I switched to
EDITOR=”/ usr/bin/emacsclient -t”
alias edit=$EDITOR
function sedit {
if [[ $# != 1 ]]; then
echo wrong number of arguments;
return;
fi
case `dirname “$1″` in
/*) edit /sudo::”$1″;;
*) edit /sudo::”${PWD}/$1″
esac
}
which I still have in one of bash’s config files. Maybe somebody finds this useful.
Btw, you can replace
(let ((pos (point)))
(find-alternate-file (concat “/sudo:root@localhost:” (buffer-file-name (current-buffer))))
(goto-char pos))
with
(save-excursion
(find-alternate-file (concat “/sudo:root@localhost:” (buffer-file-name (current-buffer)))))
Nevermind that last comment. That’d only work w/ the same buffer and the same file.
My problem has always been: How do I use Tramp to open another file using sudo on ANOTHER computer. I’m a sysadmin and would like to be able to use emacs a lot more than I do. Here you show us how to edit files on another computer and how to edit files as root on the local computer. I need to combine these two.
I
Tracy: If you have the root password, you should just be able to do:
C-x C-f //ssh:root@hostname:path/to/file
Right. But we like sudo. Logging in as root is discouraged and some machines (Ubuntu especially) don’t even have a root password. It is nice not having a root password because then you don’t have to worry about memorizing/securing it. We just use sudo for everything. Even for console access our machines drop you to a root prompt in single user mode, never need the password.
I’m wondering if you have ever had any problem with using tramp and pabbrev-mode in emacs 23. Tramp version is 2.1.15 in emacs 23 and is extremely slow comparing to 2.0.58-pre. When the pabbrev-mode try to scavenge words in a opened file, tramp keeps giving some file error, complaining about invalid lisp expression, virtually makes me unable to edit the file while in pabbrev-mode with tramp. Have you experienced something like this?
Ritchie: I just opened a file on a remote machine with tramp and got this error, but only the first time it was opened. doing pabbrev-scavenge-buffer works, and after that pabbrev works normally, but I don’t know what as causing it.
Sometimes it does gives just a couple of errors, but other times it just keeps coming. Well… hopefully they’ll fix it in the future.
[...] reader suggested this improvement in the comments of this post. This will open the current file as sudo at the current position; the old function would lose the [...]
Tracy,
I’m searching for a solution to the same issue. If I find anything, I’ll leave a reply here.
For Tracy and others who might be interested.
After some experimentation, I have had success with the method described in the Tramp user manual for using sudo on a remote host: http://www.gnu.org/software/tramp/#Multi_002dhops.
For example, let’s say you connect to the domain yellow.chalk with the user manerva:
(add-to-list ‘tramp-default-proxies-alist
‘(”yellow.chalk” “\\`root\\’” “/ssh:manerva@%h:”))
Using the above rule, you would do C-x C-f /sudo:yellow.chalk:
You would then be prompted first to login via ssh, and subsequently, if all goes well, you will get the sudo prompt. You can then find a file as usual.