dcsimg

Emacs Remote Editing with Tramp

Die-hard Emacs users often make a point of doing as much work as possible directly in their favorite editor. Indeed, mighty Emacs lets you do pretty much anything on your local machine. But as the world has become more and more network-centric, odds are that you'll need to edit a file on a remote machine. Sure, you can start a shell within Emacs, ssh to the remote machine, and edit the file. But Emacs doesn't perform terminal emulation very well, so your choice of editors is quite limited.

http://savannah.nongnu.org/projects/tramp

Die-hard Emacs users often make a point of doing as much work as possible directly in their favorite editor. Indeed, mighty Emacs lets you do pretty much anything on your local machine. But as the world has become more and more network-centric, odds are that you’ll need to edit a file on a remote machine. Sure, you can start a shell within Emacs, ssh to the remote machine, and edit the file. But Emacs doesn’t perform terminal emulation very well, so your choice of editors is quite limited.

Another option is to use ange-ftp mode, which has been part of Emacs for a long, long time. But FTP is not a secure protocol, and hacks like ange-ftp over ssh (http://parallel.hpc.unsw.edu.au/rks/ange-ftp-over-ssh.html) leave something to be desired.

What’d be really useful is an Emacs mode that makes accessing files using today’s secure tools (like ssh and scp) both transparent and intuitive. Enter Tramp. Tramp stands for “Transparent Remote (file) Access, Multiple Protocol.” It provides that transparent interface to remote filesystems that you’ve been craving. In fact, Tramp is so useful it’s included in Emacs version 21.4 and later.

Tramp is hosted on GNU’s Savannah, a SourceForge clone. You’ll find the latest version of Tramp available for download at http://savannah.nongnu.org/download/tramp. Once downloaded and unpacked, it installs like nearly every other piece of software.


$ ./configure; make; sudo make install

However, if you need to install Tramp in a non-standard location (such as your home directory), you can either use configure –prefix /some/directory, or provide specific directories for the lisp and info files:


$ sudo make lispdir=$HOME/elisp \
/infodir=$HOME/info install

If you install Tramp is a non-standard place, add two lines to your ~/.emacs file so that Emacs knows where to find it:


(add-to-list ‘load path”~/emacs/tramp/lisp/”)
(add-to-list ‘Info-default-directory-list
“~/emacs/tramp/info/”)

In either case, you should tell Emacs to load Tramp when it starts up. You may also want to set Tramp’s default access method. Here’s how to set the default to scp/ssh:


(require ‘tramp)
(set tramp-default-method “scp”)

That’s it. With Tramp installed and configured, let’s put it to work.

Suppose you have a home page on a remote server that you’d like to edit locally. To use Tramp, you need to format the “filename” in a way that Tramp can recognize and parse it. For example, if your file lives on jeremy.zawodny.com (unlikely) in /home/ jzawodn/public_html/index.html, you’d ask Emacs to open (C-x C-f) /jeremy.zawodny.com: /home/jzawodn/public_ html/index.html.

If /home/jzawodn happens to be your home directory, the previous filename can be shortened to: /jeremy.zawodny.com: public_html/index.html.

As you can see, the path is composed of two parts, sort of like a URL: the first part is the remote hostname preceded by a slash and followed by a colon; the second part is the path to the remote file on that host.

After you hit RETURN, you’ll see a bunch of messages flash by in the Emacs mini-buffer as Tramp figures out the best way to interact with the remote machine and retrieve the file. Tramp ultimately retrieves a copy of the file, stores it locally in /tmp, and opens a buffer containing the copied file. Then any time you write the buffer back to disk, Tramp takes care of copying it back over the network for you. That’s really all there is to it.

But wait! There’s more!

Remote file access is wonderful, but Tramp goes well beyond providing the basics. For example, remote directory browsing and filename completion work too. So if you open…


/jeremy.zawodny.com:public_html/

… and hit TAB, Emacs provides a list of all files in the public_html directory — just like it would locally. If you hit ENTER, it produces a directory listing you can navigate.

But that’s not all! Remote version control works too. If you’re editing a file that’s under RCS or CVS control, it works just as you’d expect: hitting C-x v v asks vc-mode to perform the next logical action, again just as it works on local files.

It’s really no surprise that Tramp’s now being distributed as part of Emacs. It’s a remarkably full-featured extension that makes working with remote files and directories secure, easy, and best of all, very Emacs-like.



Do you have an idea for a project we should feature? Drop a note to diy@linux-mag.com and let us know.

Fatal error: Call to undefined function aa_author_bios() in /opt/apache/dms/b2b/linux-mag.com/site/www/htdocs/wp-content/themes/linuxmag/single.php on line 62