Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OS X / Open terminal with specified windows

Tags:

terminal

macos

Is it possible to open a terminal window with 3 tabs. Each tab should have different path.

Example:

Tab1: /etc
Tab2: /bin
Tab3: /www/ tail -f file.txt
like image 892
xpepermint Avatar asked Sep 11 '25 07:09

xpepermint


2 Answers

This is absolutely possible, but it will take some work on your part. The first thing you need is to set up each window/tab you want in your Settings:

Settings

I have 4 tabs that I open automagically every time I open Terminal. DB Shell, Editor, Server, and Shell. These are all within the Sasquatch (don't ask) project, thus the naming. Each of these should then have a unique command associated with them:

startup cmd

In this case, I'm executing vim. If you happen to have a specific directory you'd like to start off in, you can use something like vim ~/projects/main/. Really whatever you want to go in there is the command the shell will execute when it opens. Now you need to open all your windows/tabs:

  1. Close everything.
  2. Open a new window for each of your profiles.
    • Go to the Shell menu => New Tab/New Window => Select the profile you created above.
    • Repeat for each window or tab you want.
  3. Once you have all of your windows and/or tabs open, save them as a Window Group.
    • Go to the Window menu => Save Window As Group....
    • Give your Window Group a name (this is helpful later).
    • If you want this group to open every time you open Terminal, check the box at the bottom of this screen, and then hit save.
  4. Close out of all of your Windows/Tabs (time to test it!).
  5. Open your new Window Group.
    • Go to the Window menu => Open Window Group => Select the group you just made.

This should pop up all the windows you just had, in the same position. Each of the commands you set up in Settings should be launched in their respective tabs.

like image 94
Jack M. Avatar answered Sep 12 '25 21:09

Jack M.


As of Mac OS X Lion 10.7, Terminal makes this much easier to do, without creating new profiles for each command.

  1. By default, Terminal will remember and restore the current working directory for each terminal in a Window Group. (If the working directory has been communicated to Terminal using an escape sequence. The default shell, bash, will do this at every command prompt. For other shells, you'll need to adapt the code in /etc/bashrc.)

  2. If you create a terminal with Shell > New Command, Terminal will automatically run that command when a Window Group is opened. Terminal will automatically run a limited set of "safe" commands†, and when saving a Window Group there's an option to run all commands in the group.

Terminal also automatically does these for all windows when restarting Terminal with Resume enabled. So, you may not even have to create a Window Group, depending on your circumstances.

For your example case:

  1. Use Shell > New Command to run "tail -f /www/file.txt".
  2. Create a new tab and "cd /etc".
  3. Create a new tab and "cd /bin".
  4. Save them with Window > Save Windows as Group. Be sure to check the "Restore all commands" checkbox.

Each time you open that Window Group, it will recreate those windows and run the commands. If you need to run a command and specify the starting directory, in the New Command dialog check the "Run command inside a shell" checkbox and make the command "cd ; ".

Also note that you can tell Terminal to open your Window Group at startup with Terminal > Preferences > Startup > On startup, open > Window group. There's even a checkbox to set this when saving a new Window Group.

† The "safe" commands include anything listed in /etc/shells, plus: screen, tmux, emacs, vi/vim, nano, pico, and top. You can customize the list with "defaults write com.apple.Terminal RestorableCommands". Set it to an array of strings containing command names or full paths. Some commands have parameters that are "unsafe" to run automatically without user intervention, so by default these commands are only considered "safe" if they do not have any arguments. To make a command safe to run with arguments, add an asterisk, e.g., "top *" is in the default value for this preference.

like image 34
Chris Page Avatar answered Sep 12 '25 22:09

Chris Page