7

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?

5 Answers 5

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

1
  • 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? Jul 19, 2010 at 17:58
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

6
  • Nice +1. I guess this could be remixed with John T's answer to get one single script. Apr 3, 2010 at 17:58
  • @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 token do'` Apr 3, 2010 at 19:18
  • @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. Apr 3, 2010 at 20:17
  • 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 $. Apr 4, 2010 at 1:46
  • 1
    That'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. Apr 5, 2010 at 14:50
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
3
  • awesome and necessary addition to this question. AT the time, I had bigger goals, but now this is more than sufficient. Sep 18, 2013 at 16:24
  • So in my .zshrc I have alias shell_here='open -a terminal .' Fun! Sep 18, 2013 at 16:29
  • This is the best answer imo. No scripts necessary, makes a simple process work simply
    – Unome
    Mar 22, 2015 at 21:20
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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .