Using the open -a Safari Command in the macOS Terminal

This article provides a concise guide for developers on using the open -a Safari command in the macOS Terminal. You will learn the exact syntax required to launch external URLs, open local HTML files for testing, utilize helpful command flags, and set up shell aliases to speed up your web development workflow.

Understanding the Command Syntax

macOS includes a built-in open command that allows you to open files, directories, and URLs from the command line just as if you had double-clicked them in the Finder.

By default, the open command uses whatever application is set as your system default. Adding the -a flag specifies a particular application to use instead, overriding the system default. Therefore, open -a Safari tells macOS to explicitly use the Safari browser to handle the target file or URL.

Launching Live URLs

To open a live website in Safari directly from the Terminal, pass the full URL as an argument at the end of the command. You must include the protocol (http:// or https://), or the Terminal will assume you are looking for a local file.

open -a Safari https://example.com

To open multiple URLs simultaneously in separate tabs, list them all separated by a single space:

open -a Safari https://example.com https://localhost:3000

Opening Local HTML Files

For web developers testing local builds, you can pass the path to an HTML file instead of a URL.

If you are already in the directory containing the file, simply use the filename:

open -a Safari index.html

If the file is in a different directory, provide the relative or absolute path:

open -a Safari ./dist/index.html

To open all HTML files within your current directory in Safari at once, use a wildcard:

open -a Safari *.html

Useful Command Flags

You can combine the -a Safari command with other open flags to control how the browser behaves when launched:

  • Open in the background (-g): This launches the file or URL in Safari without bringing the browser window to the foreground, allowing you to keep typing in the Terminal uninterrupted.

    open -g -a Safari index.html
  • Open a fresh instance (-F): This opens Safari without restoring any previously open windows, which is useful for testing in a clean state.

    open -F -a Safari https://localhost:8080

Creating a Terminal Alias for Speed

Typing open -a Safari repeatedly can slow down your workflow. You can shorten this command by adding an alias to your shell configuration file (usually ~/.zshrc on modern macOS).

  1. Open your configuration file in a text editor or append the alias directly using this command:

    echo 'alias safari="open -a Safari"' >> ~/.zshrc
  2. Reload your terminal configuration:

    source ~/.zshrc

Now, you can launch local files and URLs using just the word safari:

safari index.html
safari https://example.com