I only use terminal (mac) for git, and I only use git for one directory. Is it possible to set the default directory (the directory when terminal is opened) to the directory where I use git, and if so how?
6 Answers
As of Mac OS X Lion 10.7, Terminal supports Resume and by default will automatically restore terminals you had open when you quit, restoring their working directories. So, you can just open a new terminal and cd to your git directory, then leave the window open when you Quit. Each time you reopen Terminal, the terminal will be there, in the same directory. (This works for bash by default. If you're using some other shell, you'll need to adapt the code in /etc/bashrc to your shell. I've posted code for zsh in my answer to Resume Zsh-Terminal (OS X Lion) on SuperUser.)
You can also arrange for Terminal to start a shell in a particular directory. You can customize or create a "Settings Profile" to issue a "cd" command when it starts:
Terminal > Preferences > Settings > [profile] > Shell > Startup > Run command
Enable "Run command" and "Run inside shell", then set the command to cd your_git_directory
. When you open a new terminal with that profile, it will go to your git directory.
I recommend you Duplicate the current default profile (if you've never changed it, the default is "Basic") using the Action ("gear") menu at the bottom of the profiles list, then customize that profile.
Finally, to have it automatically open a terminal with this profile when you open Terminal, set
Terminal > Preferences > Startup > On Startup, open
to your custom profile. (On Lion, Resume will restore windows that were open when you quit, rather than perform the startup action. As I mentioned, you can just leave this terminal open when you Quit and it will be restored when you open Terminal again. Or, you can press the Option modifier key when quitting; the Quit menu item will change to "Quit and Discard Windows" and the next time you open Terminal it will perform the startup action.)
-
Super useful thanks! I've configured Terminal with startup tabs each of my Xcode project and git submodule directories, titles, and colours. Each fires up with a git fetch and git status. Sweet! Apr 17, 2014 at 7:58
-
The "Run command" way doesn't seem to work if you use something besides "Shells open with" "default login shell" (needed for faster startup for instance apple.stackexchange.com/questions/41743/… ) ... [?] Anyway iterm2 seems to allow the desired behavior, back to it <sigh> Mar 19, 2015 at 21:33
-
@rogerdpack If you set either Shells open with: or Run command: to a command that Terminal can't find in
/etc/shells
it assumes it isn't a shell and disables behaviors like the "last login" banner and Run inside shell. That's the only reason that workaround works. You're probably better off avoiding that workaround and periodically purging the system log instead, especially if you check the log size first—if it isn't too big, there's no issue to workaround. (And please consider filing a bug report with Apple about the login performance.) Mar 20, 2015 at 4:32 -
2I would recommend having it cd into your desired directory and then following that up with a
clear
command so that the prompt looks fresh. Run this command on startup: "cd <path_to_directory> && clear" Jun 12, 2018 at 0:45
I like to have 'New windows open with: Same Working Directory'. All answers I've found for this question (many SO's) will break that setting by always going to the new home directory. Below is what I use at the top of my .profile (or .bashrc, etc).
export START="/Users/michael/my/starting/directory"
if [[ $PWD == $HOME ]]; then
cd $START
fi
This will see if you are in your HOME
directory only on launch, and if so change to your new START
directory. That way new windows won't automatically run this command.
The only caveat is if you're in your actual HOME
directory and open a new window, it will take you to START
. Which is expected.
-
10
-
2
-
-
Try echo "cd $directory" >> ~/.bash_profile
-
10
-
Since macOS Catalina (~2019) macOS defaults to zsh (not bash), so you'll want to edit/redirect into
~/.zprofile
– MintJul 10 at 22:59
Use a Window Group. Arrange your shell window(s) as you'd want them to be on startup - cd to the directory you want in each, set colors, Shell--Edit Title, etc. Then go to Window--"Save Windows as Group...". Give it a name, check "Use window group when Terminal starts". Next time you start, this arrangement will be your starting point.
I modified @Michael Ozeryansky's answer to solve the caveat he mentioned:
The only caveat is if you're in your actual HOME directory and open a new window, it will take you to START. Which is expected.
To make your second terminal window tab starts from the Home or Any other dir:
export START="/Users/michael/my/starting/directory"
export DIR = "path/to/directory"
if [[ $PWD == $HOME ]]; then
cd $START
else
cd $HOME // or any other dir: cd $DIR
fi
If you modify the .zshrc file, the “New Terminal in Folder” Service shortcut will not work on Mac. I do not recommend.