Archives / Search ›

Terminal.app cloning script

I’ve been doing some Java programming locally on my Mac recently, and one thing I became frustrated at was navigating Java hierarchies in Terminal. Here are a few zsh snippets I wrote to help me:

clone() {
  local cmd ws
  if [ $# -eq 0 ]; then
    cmd="cd \"${PWD:q}\""
  else
    cmd="${@:q}"
  fi
  ws="{ WindowSettings = ( { ExecutionString = \"clear;${cmd:q}\"; } ); }"
  plutil -convert xml1 =(print ${ws:q}) -o /tmp/$$.term && \
  open /tmp/$$.term && \
  rm /tmp/$$.term
}

This one, if invoked with no arguments, creates a new Terminal window changed into the current directory. With arguments, it executes the named command in a new window.

After writing it, I did a quick Google search to see if anyone else had written something similar, and found this post by Marc Liyanage. Like I did originally, his version uses osascript, which is rather slow to start; so instead I write a temporary terminal settings file and open it. The plutil invocation is there to make sure I don’t end up creating an invalid plist; managing the quoting is a bit painful even with the power of zsh. Feel free to replace open with my launch utility, which will make it even faster.

The second one is rather simple and self-explanatory:

# ctrl-up arrow inserts "../"
bindkey -s '\eOa' '../'

When typing a long string of ../../../../, I tend to get the dots and slashes mixed up. Control-arrow mappings are not standardized (the one above is from rxvt), so you’ll have to configure it the Terminal inspector if you want to use it. With zsh‘s AUTO_CD option, you don’t need the cd to change directories, either.

The last one is extremely specialized, but if you need it, you need it:

upload() {
  local sddisk=${$(diskutil list | grep miniSD)[(w)-1]}
  local bndl
  if [ $# -eq 1 ]; then
    bndl="$1"
  else
    bndl=work/3.0/*.bndl
  fi
  print upload bundle ${~bndl} | pbcopy && \
	htconsole && [ ! -z $sddisk ] && disktool -e $sddisk
}

This one helps you upload bundles to the hiptop3 when you’ve got USB mass storage support enabled. It’ll copy the upload command to the clipboard, and unmount the device after htconsole exits. Replace “miniSD” with the volume name of your SD card.

4 comments on “Terminal.app cloning script”

  1. 28 July 2006 | 9:17 AM

    Now it has too many backslashes for me. Fixing them, I still can’t get it to work. Not sure why; the .term file is created. `open`ing it or double-clicking it does nothing, while opening it via Terminal’s Open menu does work. Probably my problem.

  2. 28 July 2006 | 10:02 AM

    Fixed now, as is the post explaining how to modify WordPress. My brain was pretty fried yesterday.

    The “Terminal not opening things” problem happens to me intermittently too. Quitting and restarting Terminal always fixes it. Please file a bug if you haven’t already—it’s been around for a while, and hopefully it’ll be fixed in 10.5 (guess I’ll find out in a few weeks).

  3. 28 July 2006 | 10:41 AM

    You’re right: restarting Terminal did it. Thanks for the tips!

  4. 6 September 2006 | 3:12 AM

    And for those that don’t use zsh as their shell, just create a “term” file as follows, chmod’d to u+x and placed somewhere in your path:


    #!/bin/zsh
    local cmd ws
    if [ $# -eq 0 ]; then
    cmd="cd \"${PWD:q}\""
    else
    cmd="${@:q}"
    fi
    ws="{ WindowSettings = ( { ExecutionString = \"clear;${cmd:q}\"; } ); }"
    plutil -convert xml1 =(print ${ws:q}) -o /tmp/$$.term && \
    open /tmp/$$.term && \
    rm /tmp/$$.term

Leave a reply