Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

osx Terminal new tab in same directory

I am not going to be the first to ask this but I have been trying to solve this googleing a lot and none of the resources has been useful. My problem is simple (to explain), I want to open a new tab in the same directory in the Terminal app. Now, every tab I open is opened in my ~ directory. I have also tried with iTerm2 but still the same problem. This is my .bash_profile:

# Git configuration
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /'
}

# Command line prompt
PS1='[\t] \[\e[0;32m\]\u@\h\[\e[m\]:\w \[\e[0;34m\]$(parse_git_branch)\[\e[m\]> '

# virtualenvwrapper
source /usr/local/bin/virtualenvwrapper.sh

# alias for mysql
alias mysql=/usr/local/mysql/bin/mysql
alias mysqladmin=/usr/local/mysql/bin/mysqladmin
alias mysqlstart='sudo /usr/local/mysql/support-files/mysql.server start'
alias mysqlstop='sudo /usr/local/mysql/support-files/mysql.server stop'

export PATH=$PATH:/usr/local/mysql/bin

Thank you very much for your help in advance.

like image 946
pypy Avatar asked May 04 '15 11:05

pypy


People also ask

How do I create a new tab in Terminal Mac?

In the Terminal app on your Mac, do one of the following: Press Command-T. Choose Shell > New Tab > New Tab with Profile. The name of the profile that opens is concatenated to the end of the New Tab with Profile menu item.

How do I open a second tab in Terminal?

For example, if you have your preferences set to open a new terminal in a new tab, then pressing New Terminal will open a new tab. On the other hand, if you hold down Ctrl and then press New Terminal, then a new window will be opened instead.

How do you copy a Terminal tab on a Mac?

You can now duplicate any iTerm tabs by pressing CMD + SHIFT + t at the same time on your keyboard.


3 Answers

So, I had this problem and actually just solved it. I know this post is old but if it helps anyone else I'll be happy.

I found out that if you have spaces in your hostname it breaks the New tab in the same directory feature that comes with the terminal.

Here are a couple examples. My .bash_profile is edited and changes how my prompt looks a little so I have two examples in each. You're gonna wanna focus on Kippins MacBook Pro part. This shouldn't have spaces!!!

BAD EXAMPLES!

Last login: Sat Oct 19 22:59:36 on ttys004
kippins@Kippins MacBook Pro:~$

Last login: Sat Oct 19 23:06:32 on ttys000
Kippins MacBook Pro:~ kippins$ 

GOOD EXAMPLES!

Last login: Sat Oct 19 22:59:36 on ttys004
kippins@Kippins-MacBook-Pro:~$

Last login: Sat Oct 19 23:06:32 on ttys000
Kippins-MacBook-Pro:~ kippins$ 

If you've already checked that your settings in Terminal > Preferences > General are correct. See the picture in the link below (My rep isn't high enough to embed pictures). You want New tabs open with: section to be set to Same Profile and Same Working Directory

Correct Terminal Settings

If you have spaces in your terminal hostname I think your problem is the same as mine and here's how I fixed it. In the terminal enter the following commands with the "Name-With-No-Spaces" part subbed in with a name of your choosing with no spaces!!! You'll need root access for this.

sudo scutil --set ComputerName "Name-With-No-Spaces"
sudo scutil --set LocalHostName "Name-With-No-Spaces"
sudo scutil --set HostName "Name-With-No-Spaces"
dscacheutil -flushcache

After doing all of this you'll need to restart your Mac. Hopefully, in the end, you'll have fixed your issue. I went through months of digging and had come across this post several times and I feel like the question most accurately describes what I was going through.

like image 140
Aaron Kippins Avatar answered Sep 24 '22 01:09

Aaron Kippins


Just the quoted part for PS1 works for me to open tabs in the same folder. The answer by grgarside in How to open a new Terminal tab in current working directory? suggests that something may have altered PROMPT_COMMAND; for instance in the virtualenvwrapper.sh script.

An easy way to troubleshoot this problem would be to start with a simpler .bash_profile (first empty, to verify that the open-with-same-pwd feature works), and then adding parts to see what breaks it.

like image 25
Thomas Dickey Avatar answered Sep 23 '22 01:09

Thomas Dickey


I had this issue and found that the problem was in the random zsh theme i had set up in .zshrc. After setting my theme to a specific value, new tabs were opened in the same directory.

like image 38
Nolan Conaway Avatar answered Sep 21 '22 01:09

Nolan Conaway