Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

9 Answers 9

up vote 18 down vote accepted

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

plugins=(osx)

If you use OSX's Terminal App, you also need to add the terminalapp plugin too: credit

plugins=(osx terminalapp)

If you use iTerm you need to set a configuration option (Note that you may not need the zsh plugins for this to work): credit

Preferences > Profiles > Default > General > Working Directory > Reuse previous session's directory option

iTerm2 Preferences panel update to reuse previous session directory.

That's all you need to do!

share|improve this answer
4  
Using oh-my-zsh is generally a bad idea. it comes with annoying default options and clutters your zsh with things you probably do not need or understand. –  etarion Oct 24 '12 at 12:27
3  
Many, many people would disagree with this assertion. I certainly do. –  Nate Dec 14 '12 at 15:15
7  
It is not working somehow for me... still not remembering the directory I was at. –  Tom Roggero Mar 24 '13 at 3:12
2  
This works for me unless I'm running a process in the console. If I have a process, say rails running, and I try to open a new window via command-n, I end up back at my home directory. –  YWCA Hello Aug 20 '13 at 22:08
2  
You have to add terminalapp plugin if you are using oh-my-zsh so it will be plugins=(git osx terminalapp) –  nothing-special-here Mar 27 at 8:59

If you want the directory to change automatically when a new tab is opened use the dirpersist plugin.

The osx plugin only does save the last directory but you have to run the command tab to open a new tab, which is not always possible (if, say, you're running something in your current tab).

share|improve this answer

You have to add terminalapp plugin if you are using oh-my-zsh so it will be

.zshrc

plugins=(git osx terminalapp)
share|improve this answer
    
This is the real solution... I recommend for everyone. –  szines Apr 9 at 23:13

Per Pieter's comment above, once the plugins=(git osx) plugins are installed, you can just type tab and it will open a new tab in your current directory.

share|improve this answer

If you need to open this new tab right now, without changing your config files or installing new plugins, run this:

pwd | pbcopy

Then open a new Terminal tab manually (with ⌘T), and in the new tab:

cd "`pbpaste`"

Warning: this will overwrite the contents of the system clipboard.


An alternative, longer method that does not overwrite the clipboard:

pwd > $TMPDIR/wd

Open your new tab.

cd "$(cat $TMPDIR/wd)"
rm -f $TMPDIR/wd
share|improve this answer

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:

share|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 '12 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 '12 at 22:09
2  
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 '12 at 22:29
3  
Why use precmd and preexec? Why not just chpwd () {print -Pn "\e]2; %~/ \a"}? –  Nick Apr 18 '12 at 15:53

@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.

share|improve this answer
1  
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

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...

share|improve this answer

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)

share|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 '12 at 22:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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