Spawn a new terminal window (Mac OS X)?

7

4

I am often working in a process in the Terminal and I've set some variables and used cd and so forth. Then I'd like to open another Terminal window from there. Is there any way to do this?

Dan Rosenstark

Posted 2010-04-03T17:06:54.073

Reputation: 5 718

Answers

6

You could use this little script to do what you want:

#!/bin/sh 
osascript <<END 
tell app "Terminal" to do script "cd \"`pwd`\"" 
END

place it in one of the folders in your path, make it executable (chmod +x filename) and run rehash. You can then run the name of this script to open a new terminal window in the same directory.

Src

John T

Posted 2010-04-03T17:06:54.073

Reputation: 149 037

I'm using this, but how would I get it to work without having to run the script. I would like new terminal windows to inherit the last place I cd'ed to. Any easy ideas, or should I open a new question? – Dan Rosenstark – 2010-07-19T17:58:32.327

7

What you could do is the following.

Get the current environment in your clipboard:

env | pbcopy

Open up a new Terminal window and export those environment variables

for env in `pbpaste`; do export $env; done

And to ease the process, you could always alias it, like so

alias get_env="env | pbcopy"
alias set_env="for env in `pbpaste`; do export $env; done"

So that all you have to do is

get_env Command+N set_env

Loïc Wolff

Posted 2010-04-03T17:06:54.073

Reputation: 1 947

Nice +1. I guess this could be remixed with John T's answer to get one single script. – Dan Rosenstark – 2010-04-03T17:58:29.940

@dex, thanks for this. I've mashed it together with John T's solution, but I'm getting some errors in the pbpaste part (though it works). syntax error near unexpected tokendo'` – Dan Rosenstark – 2010-04-03T19:18:02.837

@yar it might have to do with the fact that I made a typo ("or" instead of "for") in the second alias line. Sorry about that. – Loïc Wolff – 2010-04-03T20:17:19.133

the problem is that the $env cannot be blank and also some other ones do not work, like, TERM_PROGRAM=Apple_Terminal. I guess they all need to have a $. – Dan Rosenstark – 2010-04-04T01:46:45.617

Actually, this only seems to work. the pwd doesn't actually change the directory, it just confused the pwd! – Dan Rosenstark – 2010-04-05T12:32:53.907

1That's what I found too. The solution would be to add cd \pwd`` at the end of the script. If that doesn't work, use sed or awk to get the value of the PWD line. – Loïc Wolff – 2010-04-05T14:50:10.340

6

open -a Terminal .

should do the trick. It simply opens the current directory . with the application Terminal. Of course, you can use any relative or absolute path instead of . such as :

open -a Terminal ..           # Parent directory
open -a Terminal ~/Documents  # User's documents
open -a Terminal /Library     # System library

Nicolas

Posted 2010-04-03T17:06:54.073

Reputation: 310

This is the best answer imo. No scripts necessary, makes a simple process work simply – Unome – 2015-03-22T21:20:13.747

awesome and necessary addition to this question. AT the time, I had bigger goals, but now this is more than sufficient. – Dan Rosenstark – 2013-09-18T16:24:00.213

So in my .zshrc I have alias shell_here='open -a terminal .' Fun! – Dan Rosenstark – 2013-09-18T16:29:45.793

1

You could go to preferences -> general tab and look at the setting 'open new tab with', you can set terminal up to open a new tab in the current working directory.

Patrick van Efferen

Posted 2010-04-03T17:06:54.073

Reputation: 111

0

To open a new Terminal window you can do open -n /Applications/Utilities/Terminal.app or also /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal & Though I don't know if they will accept an argument such as cd ~/Documents/ Both these make completely new instances though, so to go between them on the keyboard you have to do cmd+tab and not cmd+` And if you are going to cmd+tab it always put's the new instance at the end of the queue, so you may have to do shift+cmd+tab

lavamunky

Posted 2010-04-03T17:06:54.073

Reputation: 314