Scrawls from Preston...

Powered by Pelican.

Thu 15 October 2009

Local editing of remote files

So found this buried under some dust. I had forgotten that I had ever set this up, but in adding some stuff to a .bash_profile on an older server found some code that I've spruced up bit for this post that lets you locally edit a remote file initiated via command in a remote SSH session.

I've been mostly using transmit/SFTP to do editing of remote files with my local editor of choice (TextMate). Before I switched to TextMate I was using TextWrangler. The problem with using Transmit (or any FTP client) for editing is that if you are already where you need to be in a SSH session - you have to navigate to that same location in the FTP client before opening the file for local editing.

One feature TextWrangler has that TextMate does not is the ability to open files from a SFTP URL and save them back remotely when you do CMD-S.

So putting the following in the .bash_profile on the server will let you execute "redit somefile" which will cause a reverse ssh command to be sent you telling TextWrangler to open the file.

Some prereqs:

* You have to have key based SSH auth working in BOTH directions

* You have to have the TextWrangler CLI tool (edit) installed

* Since there is a good chance that the local user you are logged in as is different than the one you are shelling into as, you have to hard code that - so unless your name is preston - you will have to change that.

`

SSH_CLIENT_ADDRESS=`echo $SSH_CONNECTION | cut -f1 -d" "`

export SSH_CLIENT_ADDRESS

SSH_HOST_ADDRESS=`echo $SSH_CONNECTION | cut -f3 -d" "`

export SSH_HOST_ADDRESS

ME=`whoami`

function redit() {

ssh preston@$SSH_CLIENT_ADDRESS edit \"sftp://$ME@$SSH_HOST_ADDRESS/$PWD/"$@"\"

}

`


http://ptone.com/dablog