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.