vote up 1 vote down star

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.

flag

Err... Doesn't work. Any help? – vgm64 Oct 12 at 3:17

1 Answer

vote up 3 vote down check

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|flag
Thanks for correcting my method. – vgm64 Oct 12 at 3:46

Your Answer

Get an OpenID
or
never shown

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