Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a remote file with TextWrangler

My current solution for editing files on a remote web server is to use Fetch to browse the remote machine and TextWrangler to make the edits. But since I'm getting more comfortable navigating the command line on the remote machine (but not comfortable enough to use VIM...), I'd like to be able to type something like 'open filename.txt' on the remote machine and have TextWrangler open up on my local machine. I've heard the term "reverse tunneling" tossed around as an option, but I have no idea what to do next. Any suggestions are greatly appreciated - thanks!

like image 378
Mike Avatar asked Sep 01 '09 21:09

Mike


3 Answers

Personally, I use Cyberduck as my S/FTP browser. In Cyberduck's preferences, you can define a default text editor to edit remote files. Now I can just hit Cmd+K when I have a file selected, and it will open up in TextWrangler. Whenever I save, the changes are automatically transferred to the remote file.

like image 136
Alan Avatar answered Nov 10 '22 05:11

Alan


I was actually looking to do the same thing, and no one had written it up, so I figured this out today.

There's 2 required and 3 optional parts to this:

  1. Enable ssh login on both computers (required)
  2. Set up an ssh tunnel from the remote machine to your machine (required)
  3. Set up an alias for the ssh tunnel (optional)
  4. Set up an alias for TextWrangler on the remote machine (optional)
  5. Set up ssh keys so you don't have to enter your password every time (optional)

You need to be able to ssh from local to remote to run the commands, and you need to be able to ssh from remote to local so it can send commands to TextWrangler.

To set up the ssh tunnel, you need to run a command on your local machine like:

ssh -f -N -R 10022:localhost:22 [username on remote machine]@[remote machine hostname]

The -f and -N flags put ssh into the background and leave you on your machine. The -R flag binds a port on the remote computer to a port on your local computer. Anything contacting the remote machine on port 10022 will be sent to port 22 on your local computer. The remote port can be anything you want, but you should choose a port > 1024 to avoid conflicts and so you don't have to be root. I chose 10022 because it's similar to ssh's default port of 22. Replace the brackets with your username and machine name.

You'll need to run that once after you log in. To make the command easier on yourself, you can add an alias in your bash profile. Add the following to your local ~/.bash_profile:

alias open-tunnel='ssh -f -N -R 10022:localhost:22 [username on remote machine]@[remote machine hostname]'

Of course, you can choose whatever alias name you like.

Once you've set up the tunnel, you can use a command like this on the remote machine:

ssh -p 10022 [username on local machine]@localhost "edit sftp://[username on remote machine]@[remote machine hostname]//absolute/path/to/file.txt"

The -p flag says to use port 10022 (or whichever port you chose earlier). This will cause the remote machine to connect to your local machine and execute the command in the double quotes without opening an interactive ssh session. The command in the quotes is the command you would run on your local machine to open the remote file in TextWrangler.

To make the command easier on yourself, you can add a function in your bash profile. Add the following to your remote ~/.bash_profile:

function edit { if [[ ${1:0:1} = "/" ]]; then abs_path="$1"; else abs_path="`pwd`/$1"; fi; ssh -p 10022 [username on local machine]@localhost "edit sftp://[username on remote machine]@[remote machine hostname]/$abs_path"; }

This is assuming that you don't have the TextWrangler command line tools installed on the remote machine. If you do, you should name the function something other than edit. For example, tw. Here, ${1:0:1} looks at the first character of the first parameter of the function, which should be the file path. If it doesn't begin with /, we figure out the absolute path by adding the current working directory (pwd) to the beginning. Now, if you're on the remote machine in /home/jdoe/some/directory/ and you run edit some/other/directory/file.txt, the following will be executed on your local machine:

edit sftp://[username on remote machine]@[remote machine hostname]//home/jdoe/some/directory/some/other/directory/file.txt

Lastly, you should set up ssh keys in both directions so you're not prompted for a password every single time. Here's a guide someone else wrote: http://pkeck.myweb.uga.edu/ssh/

like image 27
chibimagic Avatar answered Nov 10 '22 04:11

chibimagic


I dont think this will allow opening from the command-line, but

Eclipse with Remote-System-Explorer also supports editing of files via ssh connection

like image 2
cghamburg Avatar answered Nov 10 '22 04:11

cghamburg