You may already be familiar with the open command in Darwin (OS X’s flavor of Unix). It allows you to open files and URLs in their default app or one that you specify. In its simplest form:

$ open http://brettterpstra.com
$ open shell-tricks-open-command.md

Those will open the url or file in whatever you have set as the default browser or editor for Markdown files. You can specify what application to use with -a, or by Bundle Identifier using -b.

$ open -a "Safari" http://brettterpstra.com
$ open -b com.multimarkdown.composer.mac shell-tricks-open-command.md

You can also use it with application-specific urls, such as Dash’s dash: handler (I’ve detailed this before):

open dash://man:open

These are the uses most people who use the command are familiar with. You can open an app without a document with just the -a or -b flags, too. I alias open -a to just o on my system, so I can just type o tweetbot and launch the app. I’ve also set up bash completion for this.

A fresh start

A couple of lesser-known features are pretty handy. First, the -F switch opens the specified application “fresh,” meaning no “persistent” windows are restored. Note that this erases the “persistent state” of the app, but leaves unsaved “Untitled” documents alone. It’s a great shortcut for opening an app that may have a document crashing it when it first opens. It doesn’t always work, but it’s usually the first thing I try in those situations. It’s just so easy.

Opening piped input

The -f command takes input from a STDIN pipe, creates a temporary file with it, then opens that in the specified app. You may have seen this yesterday in my tip for opening man pages in Preview. That trick uses PostScript output, but you can do the same with any type of data. Here’s a trick for pasting your current clipboard text to Marked.

pbpaste | open -f -a Marked\ 2

Of course, Marked 2 can do that with just ⌘⇧V, so it’s less handy. Here’s a better one: combine a bunch of Markdown files in a folder into one temporary document with Markdown horizontal rules between them. I use this to quickly view all of my QuickQuestion answers from my nvALT folder (these filenames always start with “??”, adjust as needed):

awk 'FNR==1{print "\n---\n"}1' \?\?*.md | open -f -a Marked\ 2

The awk trick is kind of cool on its own. When you just use cat, you don’t get any separation between files, meaning that using it for Markdown output isn’t great. The command awk 'FNR==1{print "\n---\n1' will take all of the input files and print them with --- and newlines (or whatever you specify in the print command) between each one.

Editors

The -e switch causes open to automatically use TextEdit. A better option would be -t, which allegedly uses your default editor for text files, as determined by LaunchServices. Unfortunately, that always still opens TextEdit for me. Instead, I just alias edit to my $EDITOR script. You can also use the -W switch to cause open to wait for the opened application to exit before exiting the command. The -n switch causes a new instance of the application to open – even if the app is already running – so the combination of -Wn effectively turns any app into a valid $EDITOR setting.

Finder

In Spotlight, hitting ⌘↩ (Command Return) on a selected file reveals it in Finder instead of opening it. open has a -R switch that does the same. You could always use open -a Finder [filename], too. I have open -a Finder aliased to f, so I can just type f filename or f . to open a file, folder, or the current directory in Finder.

Other handiness

The -g flag is another handy one. It will open the target application in the background, so it doesn’t steal window focus. I find this especially handy if I’m using iTerm2 in visor mode, allowing me to open links and web pages without having the visor slide up. If you use something like Choosy, opening a URL directly can cause the visor window to lose focus, but the Choosy popup to disappear immediately as focus switches. I alias chrome and safari to use the open -g -a [appname] in order to bypass Choosy when necessary.

Cocoa developers should check out the -h option, but I won’t go into it here.

Lastly, you can pass arguments to an app specified with -a or -b using the --args flag. Anything after that flag will be passed as arguments to the application, not the open command.

The open command is one of OS X’s special gems. You won’t find it on other flavors of Unix. Cherish it.

Ryan Irelan has produced a series of shell trick videos based on BrettTerpstra.com posts. Readers can get 10% off using the coupon code TERPSTRA.