While working on the command-line, we always find ourselves in situations where we have to open a new tab and cd to the path we were working on.
On a Mac, we can use some Applescript to help with that.
Paste it to your ~/.profile:
# open a new tab on Terminal with the current working dir
function newtab {
osascript -e "
tell application \"System Events\" to tell process \"Terminal\" to keystroke \"t\" using command down
tell application \"Terminal\" to do script \"cd \\\"$(PWD)\\\"\" in selected tab of the front window
" > /dev/null 2>&1
}
Now you just need to type “newtab”
Nice! I’ve always wanted this in Terminal (on a case-by-case basis, at least), and this works great. Thanks for sharing.
Thanks for the tip.
The cd part does not work on my system when there is a space in the path, though.
So I changed the script to accommodate for space in the path, as follows :
# open a new tab on Terminal with the current working dir
function newtab {
osascript -e ”
tell application \”System Events\” to tell process \”Terminal\” to keystroke \”t\” using command down
tell application \”Terminal\” to do script \”cd \\\”$(PWD)\\\”\” in selected tab of the front window
” > /dev/null 2>&1
}
and now it works. Just wanted to share this with people who might have the same problem.
Arach
True. I’ll update the script. Thank you.
thnx