macOS shell command to create a new Terminal Window

Of course, you can easily create a new Terminal window from the ‘Shell’ menu or by using the ⌘N (or ⌘T) keyboard shortcut. But in some cases, it can be more useful to use a shell command.

New windows created with the keyboard shortcut or from the menu will always have the home directory ~ as the current working directory. What I want, is a new window that defaults to current working directory or a custom directory that I can provide with an argument:

> new           # opens a new terminal window at the current working directory
> new ~/Desktop # opens a new terminal window at ~/Desktop

No luck with AppleScript

After my last success using AppleScript, I thought this would be the best solution again. Unfortunately, this particular piece of the AppleScript dictionary is broken. The make new window or make new tab commands fail with errors and I have tried several combinations.

After some web searching, it looks like this has been broken for a long time. I filed an issue in Feedback Assistant.

You can create a new Terminal window with AppleScript using the do script command in the Terminal dictionary. (Not to be confused with do shell script.) So this AppleScript, sort of does what I want, but seems cumbersome.

tell application "Terminal"
    do script "cd ~/Desktop"
end tell

If you know of a better way to create a new Terminal window or, even better, a Terminal tab with AppleScript, then please let me know. (No UI Scripting solutions – those have their own issues.) I have a few other ideas where this might come in useful.

Enter the open command

During those web searches, I also found suggestions to use the open command, instead:

> open -a Terminal ~/Documents

Will open a new Terminal window with ~/Documents as the working directory. This is already really close to what I wanted.

I created this function in my shell configuration file (bash, zsh):

# creates a new terminal window
function new() {
    if [[ $# -eq 0 ]]; then
        open -a "Terminal" "$PWD"
    else
        open -a "Terminal" "$@"
    fi
}

With this, I can now type

> new Projects/desktoppr

and get a new Terminal window there. This is very useful when combined with the history substitution variable !$ (last argument of previous command):

> mkdir Projects/great_new_tool
> new !$

And an unexpected, but useful side effect is that the new function can also open an ssh session in a new window:

> new ssh://username@computer.example.com

Hope you find this useful, too!

Published by

ab

Mac Admin, Consultant, and Author

6 thoughts on “macOS shell command to create a new Terminal Window”

    1. Wonderful! I hadn’t thought of testing that. If you switch back and forth between Terminal and iTerm, then you can change the function to this:

      # creates a new terminal window
      function new() {
          if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
              app_name="iTerm"
          else
              app_name="Terminal"
          fi
          
          if [[ $# -eq 0 ]]; then
              open -a "$app_name" "$PWD"
          else
              open -a "$app_name" "$@"
          fi
      }
      1. I had to set the preference “Force this profile to always open in a new window, never a tab” in Preferences→Appearance→Window for iTerm to open a new window rather than a tab in the same window.

  1. I use a free utility called Go2Shell, which puts a button on the Finder toolbar to do same. It also has a GUI that lest you set which Terminal app you want and the exact command you want to execute in that app upon opening the window. The most recent release is January 2019, but it is 64-bit and is running fine for me in Mojave and Catalina. https://zipzapmac.com/Go2Shell

  2. Hi there,

    Great stuff.

    I’d like to do this in a shell script and immediately run commands in the new window. Any idea how this can be done?

    1. You can do this with do script AppleScript command, e.g. osascript -e 'tell app "Terminal" to do script "sw_vers"'

Comments are closed.