Chibimagic's Weblog

Archive for February 2011

I hate all pagers: more, less, pg, the whole lot. I can’t read fixed width fonts for that long without my eyes bleeding and my soul crying. Plus:

  • no scroll wheel
  • no copy pasting long sections
  • search is horrendous
  • etc. etc.

So, next step? Pipe to local TextWrangler, of course!

function edit {
  command=""
  if [[ $1 ]]
  then
    if [[ ${1:0:1} = "/" ]]
    then
      abs_path="$1"
    else
      abs_path="`pwd`/$1"
    fi
    command="sftp://remote_user_name@remote_host/$abs_path"
  fi
  ssh -p 10022 local_user_name@localhost "edit $command"
}

And the single line version:


function edit { command=""; if [[ $1 ]]; then if [[ ${1:0:1} = "/" ]]; then abs_path="$1"; else abs_path="`pwd`/$1"; fi; command="sftp://remote_user_name@remote_host/$abs_path"; fi; ssh -p 10022 local_user_name@localhost "edit $command"; }

Now these all work:

edit file_in_this_directory.txt
edit /absolute/path/to/file.txt
man ls | edit

At work we do all our development on a remote server, but local tools are frequently better. I got tired of looking up the same path over and over in both my ssh session and TextWrangler’s FTP browser, and I wanted an option other than emacs/vim. I wanted to run a command on the remote machine to open that file in my local TextWrangler. I actually found a Stack Overflow question asking the exact same thing, but no one had a real answer. So I spent some time setting it and then wrote it up as an answer: http://stackoverflow.com/questions/1364826/opening-a-remote-file-with-textwrangler/4919234#4919234


How to open remote files in TextWrangler:

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/