Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Open new Terminal window at current Terminal directory UNIX
Have you ever wanted a second terminal window located in the same directory that you're currently working in? Perhaps one holds a list of files in a grep search results; the other, an editing window for the particular file you're editing. The typical way of doing this is to open a new terminal window (Command-N), then issue the cd command with the path to the location you wish to jump to.

macosxhints.com reader Impatient1 sent in a script to automate this process, but I had some trouble making it work while testing the hint ... so I posted it to this thread on the forum site, asking for assistance. After a few back and forths, reader Paul McCann took what was a 20 line program and turned it into four simple lines of code:
  #!/bin/sh 
  osascript <<END 
  tell app "Terminal" to do script "cd \"`pwd`\"" 
  END
Just create this script somewhere on your path (I called it nt, for new term), set it to executable with chmod 755 nt, type rehash to add it to the list of known programs, and you're done.

Now, whenever you need a new Terminal window set to the current Terminal window's active directory, just type nt and you've got one! Thanks to Paul and the others who contributed to the thread for finding such a simple solution!
    •    
  • Currently 3.00 / 5
  You rated: 5 / 5 (5 votes cast)
 
[9,300 views]  

Open new Terminal window at current Terminal directory | 7 comments | Create New Account
Click here to return to the 'Open new Terminal window at current Terminal directory' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Open new Terminal window at current Terminal directory
Authored by: lpp on May 08, '03 11:06:33AM

This still fails with directories containing non ASCII characters in
their names, such as:

MÄhhh wühl müch dörch!°

(I'm not sure how much of that will translate properly into this
text field).

Anyway, I'm still looking for something that will work in those
cases (I wrote the "Terminal Here Plugin" CMM, available on
VersionTracker, that does what this does, but it too fails in these
odd named cases). Note that the "Open Terminal Here" plugin
script handles these cases.

Regards,

Lynn



[ Reply to This | # ]
Open new Terminal window at current Terminal directory
Authored by: nobody on May 10, '03 12:08:38PM

This one fail not when directories contain strange chars:
#!/bin/sh
echo $PWD > ~/.tmpdirstorepath
osascript <<END
tell application "Terminal" to do script "cd \"\`cat ~/.tmpdirstorepath\`\""
END



[ Reply to This | # ]
Open new Terminal window at current Terminal directory
Authored by: lpp on May 10, '03 05:25:15PM

Yes, that's the method that "Open Terminal Here" employs. I had hoped to avoid doing someting similar, but may not have a choice with Terminal Here Plugin.

Regards,

Lynn



[ Reply to This | # ]
Open new Terminal window at current Terminal directory
Authored by: nobody on May 10, '03 06:18:19PM

I admit, this is indeed based your approach. I did not understand your perl script, but saw the .OpenTerminalHere file I began to try out the same way.
first I tried to:
\"`cat ~/.tmpdirstorepath`\""
which did not work.
then I also escaped the `:
\"\`cat ~/.tmpdirstorepath\`\""
This also works in the terminal, using for example:
cd \"\`$PWD\`\""
But:
tell application "Terminal" to do script "cd \"\`echo $PWD\`\""
does not work for pathnames which have öäü ....
Looks like a bug of Terminal?



[ Reply to This | # ]
Open new Terminal window at current Terminal directory
Authored by: hughescr on May 08, '03 05:51:21PM

You can shorten the script further by using the "one liner" flag ("-e") to osascript, along with better shell quoting:

osascript -e 'tell app "Terminal" to do script "cd \"'`pwd`'\""'



[ Reply to This | # ]
Open new Terminal window at current Terminal directory
Authored by: pmccann on May 08, '03 08:12:52PM

Does this eliminate the problem with non-ascii characters
mentioned above? The extra layer of quoting looks worthwhile,
but I'm getting an error: "syntax error: Expected expression but
found ?Äúwith?Äù. (-2741)" when executing the line as printed
here. Maybe another problem with backslashes being eaten upon
submission?

(For what it's worth, the one-liner versions are in the referenced
thread; the heredoc version's my favourite in that it allows for
easier quoting and lets you just cut and paste applescript
fragments from the Script Editor rather than have to use the
ugly hack of multiple -e invocations.)



[ Reply to This | # ]
Open new Terminal window at current Terminal directory
Authored by: Impatient1 on May 09, '03 05:05:25AM

I waited a couple days and just assumed it was rejected or something since I never saw it show up; I never received any email from 'griffman' either. Anyway, there is some missing context in some of this. In the original message, I mentioned that this was written for MacOS X 10.1 and that changes might be required for Jaguar. As such, the shell tested against was 'zsh', not 'bash'. The sed line must have gotten trampled by the forum software when I pasted it for the sixth time trying to get the embedded tag delimiters in the comment section to show up correctly - it was really late. The two corrections are:

dot-term:
<string>cd $DIR; history -c; clear</string>

shell script:
sed "s#\$DIR#\"$DIR/\"#" $SKELETON_TERM_PATH>"$RUN_TERM_PATH"

Using the dot-term file allowed me to set additional options (default window size, font, etc). The script also allowed creating new terminal windows with arbitrary paths, though the current directory was its mainstay:

$ newterm /usr/include
$ newterm $JAVA_HOME

And I think it's wonderful that much of its functionality was reduced to a one-line AppleScript; it sucks that the AS versions posted here don't work for me under 10.1 (NSInternalScriptError (8), whatever that means). I had used the 'Open Terminal Here' script but did the job from the Finder, not inside Terminal.

Apologies to the non-US crowd but it was originally written for me, not an international setup.

Still think it was worth a story credit...



[ Reply to This | # ]