How can I Open Multiple Customized Terminal Shells?

How can I customized Terminal so that when I open the application it opens several shell tabs? If possible I would like to also have it run a different environment script for each tab.

Example: Run this command source bin/activate which is a Python virtualenv setup command.


Solution 1:

Add this to your ~/.bash_profile if you use bash or ~/.zshrc if you use zsh, changing the content of the COMMANDS array:

function openterms() {
    COMMANDS=("source bin/activate" "add another here" "continue likewise")
    for command in $COMMANDS
        do osascript -e \
        "tell application \"Terminal\" to do script with command \"$command\""
    done
}

Now calling openterms in a shell will open all the other shells you wanted.

Solution 2:

I'm using Keyboard Maestro. I once recorded a macro that sets up Terminal (I'm using iTerm, but it doesn't matter) the way I like and made this macro to be triggered by launching iTerm.

So when I launch, it is set up as I wanted. Two windows, five tabs in one for different folders with each Tab named appropriately, and another window with two tabs connected to my server, one of them launches a monitoring script. All of this is done within 2 seconds that the Terminal is launched, which previouosly would take a minute or more.

Now I'm not that afraid to close iTerm if I need since I can reopen it at the same state without any effort.

By the way, Keyboard Maestro is good for many other things too. I recently bought it with discount on mupromo.com, where I've discovered it for the first time but it's worth its full price, too, in my opinion.

Solution 3:

As Josh K mentioned, you can create a Window Group for opening one or more windows with specific layouts and appearances. In addition, "settings profiles" (Terminal > Preferences > Settings) can specify a command to run when a terminal is created with that profile. You can create a separate profile for each command you wish to run, then create windows with those profiles. When you save them to a window group and open them again, those commands will run in their appropriate windows. You can even have Terminal open a specific window group at startup.

As of Mac OS X Lion 10.7, there are several new improvements in this area:

  1. Resume will automatically restore windows that were open when you quit Terminal, so using a window group for this is no longer strictly necessary (although they allow you to re-create a specific setup if you've closed the windows).

  2. If you create a new terminal using Shell > New Command, since Terminal knows what command you ran, it can restore it by running it again. Terminal will now automatically restore "safe" commands† when Resuming.

  3. Terminal will also restore these commands if you save these kinds of terminals into a window group. In addition, when saving a window group, there's an option to restore all commands, not just the "safe" ones. So, you no longer need to create a separate settings profile for each command you want to run.

† "Safe" commands include anything listed in /etc/shells, emacs, vi/vim, nano, pico, top, screen, tmux, and any command created with Shell > New Remote Connection (e.g., ssh sessions). Note that only top is considered "safe" when used with arguments; the others support arguments that may do things you wouldn't want them to without user confirmation, so they are not considered "safe" for automatic restoration unless they are invoked without arguments. You can customize the list of "safe" commands with "defaults write com.apple.Terminal RestorableCommands". The value is a list of strings containing the command names. To indicate that arguments are allowed, add an asterisk after a command. e.g., "top *" makes top safe when used with arguments, but "top" does not.

Solution 4:

Alternatively, you can also use screen to open multiple tabs on a single window. screen is very flexible and can configure to run any scripts during the terminal startup.

More info.