With a little side of applesauce...

Thursday, October 27, 2011

EMACS - TRAMP remote file access over SSH

TRAMP is the main reason that I left Eclipse and returned to EMACS. The Eclipse remote file access over SSH was clunky, (or possibly non-existent at the time). TRAMP is simple, (once I give you some tips), and is much faster than Eclipse. It also works on all platforms, (though you will need to use Cygwin on Windows).

First, I add the following to my .emacs:
;;;;;;;
;; tramp
(require 'tramp)
(setq tramp-default-method "scp")
;;;;;;;;


This makes sure it is available to EMACS, (though it may not be necessary now that TRAMP is bundled in newer versions of EMACS), and sets the default method to SCP.

Second, you will want to set up key-based authentication as described in my post, "ssh authentication through public/private keypairs". This is not necessary, (and may be prohibited by your security policies), but it makes your remote file interaction seamless. If you don't setup key-based authentication, TRAMP will prompt you everytime that you login to the remote machine, so no worries.

Now, start EMACS, and hit C-x C-f and type the following and hit :
/scp:speeves@www.example.com:/home/speeves/


TRAMP will negotiate the SSH handshake, and prompt you for a password if you didn't setup key-based authentication. You should now see a listing of contents in your remote directory, (Dired). At this point, navigate and open/close and edit files just as if you were on your local machine. Very cool!

Don't forget to bookmark oft-used locations, (C-x r m)!

NOTES:

1. I always leave a local file open in EMACS, (usually .emacs), as sql-mode will try to search for psql on the remote machine when your current buffer is remote. Therefore, I switch to my local file before I start a new SQL-mode buffer.

2. There can be a short delay before the remote file is written if there are network issues, or if the SSH handshake takes longer than normal. Try reloading your web browser again if your changes didn't show. (Or, make sure that you saved your changes :) ).

3. I reload any buffer that is edited via a different program. For instance, I am working in EMACS, but visit the remote file using VIM in a regular shell session. Remember that the buffer is local and will overwrite any changes made locally to the file, if you save the buffer without reloading.

4. Some commands in Eshell don't work correctly with TRAMP, (ie grep). Therefore, I usually don't use Eshell for 'real' shell interaction with remote files. (Hence point #3).

No comments: