Say you are in /very/cool/and/deeply/nested/folder . And you want to open a new terminal tab in the same folder.

How would you do that?

I use Mac OS and Zsh.

link|improve this question

feedback

5 Answers

up vote 3 down vote accepted

Use Oh-My-Zsh and add the 'osx' plugin in your ~/.zshrc like:

plugins=(git osx)

That's all you need to do!

link|improve this answer
does this also work in iTerm ? – ahmy 1 hour ago
feedback

Another option now available in Mac OS X Lion is using the built-in feature. It uses 'escape sequences' to find out the current directory. For me it works if I use these commands in my .zshrc:

precmd () {print -Pn "\e]2; %~/ \a"}
preexec () {print -Pn "\e]2; %~/ \a"}

it is also possible to use PS1 (for Bash, from this wiki):

export PS1="\[\e]2;\u@\H \w\a\e[32;1m\]>\[\e[0m\] "

where \e]2; is the escape sequence to print things in the titlebar. It seems that Terminal.app is getting its information from there.

More information:

link|improve this answer
also, I think oh-my-zsh has this by default. I've been using this for a week or so now and it works pretty much out of the box. – Tim Aug 8 '11 at 7:18
As of Mac OS X Lion 10.7, Terminal will display the working directory using the “proxy” icon in the title bar, has options to create new terminals at the same directory, and supports Resuming terminals. As a convenience, Terminal will look at the contents of the window/tab titles to see if they contain a valid pathname. However, in /etc/bashrc you’ll see that it also supports a new escape code for informing Terminal of the working directory using a file: URL, which can handle all valid pathnames via percent-encoding (the window/tab titles can only contain a subset of ASCII characters). – Chris Page Mar 10 at 22:04
Also note that the support for pathnames in the titles can be slow if the title contains any other text or if the title contains slashes but is not a valid pathname, because it has to test every substring that looks like a pathname to see if it’s a valid directory. – Chris Page Mar 10 at 22:09
The escape sequence for setting the working directory is the same basic code as for setting the titles—Operating System Command (OSC)—with code 7 instead of 0-2: \e]7;file://hostname/percent-encoded-pathname\a – Chris Page Mar 10 at 22:29
There is also a code for setting the “represented file”, which you can use to set the window proxy icon to, for example, the file being displayed or edited. It’s the same sequence as for the working directory, but with code 6 instead of 7. For example, I have Emacs and Less configured to set it to the current file. – Chris Page Mar 10 at 22:30
show 1 more comment
feedback

This is how you do it in bash.

This shell script will tell (quiet literally, using Applescript) Terminal.app to open a new tab then switch to the current directory:

#!/bin/bash
osascript -e 'tell application "Terminal"' \
-e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' \
-e "do script with command \"cd `pwd`;clear\" in selected tab of the front window" \
-e 'end tell' &> /dev/null

… put the above shell script in a directory in your $PATH (i.e. /usr/local/bin) and make sure it’s executable:

$ chmod +x /usr/local/bin/nt

(source)

link|improve this answer
I did exactly has you said. But nothing happens when I do 'nt' – Nerian Jan 13 '11 at 20:13
I am using Visor too, if that changes anything. visor.binaryage.com – Nerian Jan 13 '11 at 20:15
@Daniel: I did what I understood you said. But I didn't work. Perhaps I didn't understood what you meant, could please make an answer? – Nerian Jan 15 '11 at 21:28
Note that as of Mac OS X Lion 10.7, by default Terminal will start new tabs in the same working directory as the previous tab. So you only need to arrange to create the tab now. (If you’re using bash. If you’re using another shell, look at the code in /etc/bashrc for how to tell Terminal about the current working directory.) – Chris Page Mar 10 at 22:22
feedback

gdirs seems like a way to almost do it: new tab, then gdirs to select the deep directory and voila. My first idea was to make the directory stack shared among all tabs and do cd ~1 after the new tab, but I cannot find how to do that, as it seems each instance of zsh keeps its own. History sharing goes via a common file, so maybe that could be done here too...

link|improve this answer
feedback

@Nifle's code does work. In ~/bin…

mate nt #paste code, save
chmod +x nt
source ~/.profile

In my .profile I have the following:

export PATH=~/bin:$PATH

If you don't have something like that, nt won't work.

link|improve this answer
That's why @Nifle told to place the script in a directory already on the $PATH. No changes necessary then. – Daniel Beck Jun 10 '11 at 1:27
feedback

Your Answer

 
or
required, but never shown

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