I would like to create a command like cd - (lets call it cdp) that will change directories to the last changed-to directory from another terminal window similar to the option to open a new terminal in the directory that previous window/tab was in (I don't see that option in Mac OS X terminal).

To do so I figure I could alter cd with something like alias cd='cd $1;echo $PWD > /tmp/CWD' and then add

alias cdp='cd  `cat /tmp/CWD`

Can someone key in a better solution? Or, fill me in on an existing program, feature, etc., please let me know. On Mac OS X 10.6 with default terminal. Thanks.

link|improve this question

feedback

2 Answers

up vote 4 down vote accepted

Aliases don't accept parameters. You'll have to use a function. You should also use the command builtin.

function cd () { command cd "$@"; echo "$PWD" > /tmp/CWD; }

alias cdp='cd $(cat /tmp/CWD)'

See this for another approach that's specific to OS X. It's a script that can launch a new Terminal window or tab opening with its current directory the same as that of the current Terminal window or tab.

link|improve this answer
Thanks for correcting my method. – vgm64 Oct 12 '09 at 3:46
feedback

I like the solution at http://hints.macworld.com/article.php?story=20051231110014263 better:

Open new xterm windows in current Terminal directory Authored by: TomP on Jan 04, '06 10:05:12PM

As a slightly more flexible alternative, I have a little shell script ("openterminal.sh") that will open a new Terminal window in the same directory as the Terminal session from which it was invoked. Here's the script:

#!/bin/sh
# 
# Open another terminal window for the current directory
#
# Copyright 2004 by Tom Pollard - All rights reserved.
#
#set -x
CWD=`pwd`
osascript<<END
set thePath to "$CWD"
set myPath to (POSIX file thePath as alias)
try
    tell application "Terminal"
        activate
        do script with command "cd \"" & thePath & "\""
    end tell
end try
END

I have this aliased as 'ot'. So, when I want another Terminal window open to the same directory as some other Terminal window (not necessarily the last one I used or opened, I just say 'ot' in that Terminal session.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.