I\'ve been Googling for a while looking for a simple way to do this, and I can\'t find one.
I have a custom terminal environment set up (zsh) with various aliases and fu
You can get what you want by modifying the BASH script found at http://www.entropy.ch/blog/Mac+OS+X/2008/06/27/Terminal-Tricks-“term”-revisited-with-tabs. Here is the script, taken from Marc Linyage's site www.entropy.ch/blog.
#!/bin/sh
#
# Open a new Mac OS X terminal window or tab in the current or another
# directory and optionally run a command in the new window or tab.
#
# - Without any arguments, the new terminal window opens in
# the current directory, i.e. the executed command is "cd $PWD".
# - If the first argument is a directory, the new terminal will "cd" into
# that directory before executing the remaining arguments as command.
# - The optional "-t" flag executes the command in a new tab
# instead of a new window.
# - The optional "-x" flag closes the new window or tab
# after the executed command finishes.
# - The optional "-p" flag takes an argument of the form x,y (e.g. 40,50) and
# positions the terminal window to the indicated location on the screen
# - The optional "-s" flag takes an argument of the form w,h (e.g. 800,400) and
# resizes the terminal window to the indicated width and height in pixels.
#
# Written by Marc Liyanage <http://www.entropy.ch>
#
# Version 2.1
#
set -e
while getopts xtp:s: OPTION; do
[ $OPTION = "x" ] && { EXIT='; exit'; }
[ $OPTION = "t" ] && { TAB=1; }
[ $OPTION = "p" ] && { POSITION="set position of window 1 to {$OPTARG}"; }
[ $OPTION = "s" ] && { SIZE="set size of window 1 to {$OPTARG}"; }
done
for (( $OPTIND; $OPTIND-1; OPTIND=$OPTIND-1 )); do shift; done
if [[ -d "$1" ]]; then WD=$(cd "$1"; pwd); shift; else WD=$PWD; fi
COMMAND="cd '$WD' && echo -n \$'\\\\ec';"
for i in "$@"; do
COMMAND="$COMMAND '$i'"
done
if [ $TAB ]; then
osascript 2>/dev/null <<EOF
tell application "System Events"
tell process "Terminal" to keystroke "t" using command down
end
tell application "Terminal"
activate
do script with command "$COMMAND $EXIT" in window 1
$POSITION
$SIZE
end tell
EOF
else
osascript <<EOF
tell application "Terminal"
activate
do script with command "$COMMAND $EXIT"
$POSITION
$SIZE
end tell
EOF
fi
In my answer here, I provided a function and an alias:
function cd () { command cd "$@"; echo "$PWD" > /tmp/CWD; }
export cd
alias cdp='cd $(cat /tmp/CWD)'
You should be able to put a (possibly conditional) statement at the end of your ~/.bashrc
or ~/.zshrc
to execute that alias.
OK, so as is my way I am answering my own question again (well at least getting close to answering it anyway)
I have found a less verbose script to the one above (courtesy of Dan Benjamin) that seems to do the trick, although both scripts output a similar error before successfully completing. I have dealt with that by adding clear to the end of the script so that's no big problem.
I say that I have nearly solved my own problem because my objective was to find a way to accomplish this with the Apple-t key command that has been burnt into my muscle memory as the shortcut for a new tab in anything, thanks to countless hours in various web browsers. The best I can manage with a script such as Dan's is t-return which isn't the biggest difference, but big enough that I will be slightly irked every time I issue said command. I know, I should let it go..... But I can't, which is probably how I got into this mess in the first place, endlessly fiddling with computers. I digress, here is the script I am using:
#!/bin/sh
# Make a new OS X Terminal tab with the current working directory.
if [ $# -ne 1 ]; then
PATHDIR=`pwd`
else
PATHDIR=$1
fi
/usr/bin/osascript <<EOF
activate application "Terminal"
tell application "System Events"
keystroke "t" using {command down}
end tell
tell application "Terminal"
repeat with win in windows
try
if get frontmost of win is true then
do script "cd $PATHDIR; clear" in (selected tab of win)
end if
end try
end repeat
end tell
EOF
clear
For completeness here is the error that gets spat out on the soliciting window if the trailing clear is omitted:
2009-10-20 01:30:38.714 osascript[20862:903] Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found. Did find:
/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper
osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" declares no loadable handlers.
tab 2 of window id 13942
I have a couple of scripts I use:
dup (New window with the working dir):
#!/bin/sh
pwd=`pwd`
osascript -e "tell application \"Terminal\" to do script \"cd $pwd; clear\"" > /dev/null
and tup (New tab with the same working dir):
#!/bin/sh
pwd=`pwd`
osascript -e "tell application \"Terminal\"" \
-e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
-e "do script \"cd $pwd; clear\" in front window" \
-e "end tell"
> /dev/null
One other solution without scripting is iTerm2, which has this feature built in. It has even more features that make it worth checking out too.