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.
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
That's all you need to do!
rails
running, and I try to open a new window via command-n, I end up back at my home directory.
Aug 20, 2013 at 22:08
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:
/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).
Mar 10, 2012 at 22:04
\e]7;file://hostname/percent-encoded-pathname\a
Mar 10, 2012 at 22:29
chpwd () {print -Pn "\e]2; %~/ \a"}
?
This is a very simple version which I used in bash and also works in zsh. It saves the current folder in a file, after every command (Doesn't hurt too much IMO) and opens a new terminal in the saved current folder.
add the following to .zshrc
# emulate bash PROMPT_COMMAND (only for zsh)
precmd() { eval "$PROMPT_COMMAND" }
# open new terminal in same dir
PROMPT_COMMAND='pwd > "${HOME}/.cwd"'
[[ -f "${HOME}/.cwd" ]] && cd "$(< ${HOME}/.cwd)"
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...
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)
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
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.
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).
Adding terminalapp to .zshrc didn't work for me so I looked for the plugin ~/.oh-my-zsh/plugins/terminalapp
and it tells me:
# This file is intentionally empty.
#
# The terminalapp plugin is deprecated and may be removed in a future release.
# Its functionality has been folded in to the core lib/termsupport.zsh, which
# is loaded for all users. You can remove terminalapp from your $plugins list
# once all your systems are updated to the current version of Oh My Zsh.
New tabs already open in the same folder (Cmd+T). For new windows (Cmd+N) the solution from Pieter is right:
Preferences > Profiles > Default > General > Working Directory > Reuse previous session's directory option
I don't have enough repu to comment on other's answers, just intend to comment about @tim 's answer, and answering @mareoraft 's question in a comment there:
I understand how this updates the titlebar, but I don't understand how this causes a new tab to open in the same directory as the previous tab. – mareoraft
Seems Terminal.app will infer working directory from what it is told, I'm not sure what things are like date back, but with Mojave, I just find that OSC 7 does the trick instead of OSC 2. OSC 7 is dedicated for pwd.
In my .zshrc under Mojave.
precmd () {
print -Pn "\e]2;\a" # OSC - clear previous msg in title bar
print -Pn "\e]7;%~\a" # OSC - send cwd for title bar, this also enables new tabs to reuse cwd
}