Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question

5 Answers 5

up vote 2 down vote accepted

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.

share|improve this answer

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.

Edit: Incidentally, it's 45% off today (Aug 31) only on MacZOT.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Does this post describe what you want to do?

share|improve this answer

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.