If you are a Terminal user, then you very likely spend a fair amount of time editing various text files, be they configuration files, scripts, or data files you might use for your work. For a seasoned Terminal veteran, the various text editors like vi, emacs, and nano, may offer all the tools needed for getting the job done, but for those less familiar, managing these tools can be cumbersome. Luckily, in OS X there is a quick way to make use of GUI-based programs for handling pretty much any document you might encounter in the Terminal.
In general, when you wish to edit a file in a terminal-based program, you will target it via standard input, such as the following to open a file in nano:
Tophers-Retina-MBP:~ tkessler$ nano /path/to/file
open -e /path/to/file
The “open” command acts just as if you had double-clicked the file in the Finder, where the “default” program will be used for opening the file. For instance, if the file you are opening is an HTML file, then if you issue the following command, then it will open with your default browser (likely Safari):
The key here is to use the “-e” flag (as shown above), which will tell the “open” command to edit the file in TextEdit, instead of its default editor. This will allow you to modify the file using graphical tools, that may be far more intuitive than Terminal-based editors.
While the “-e” flag will specify TextEdit, if you have other text editors that you prefer (such as TextWrangler, or BBEdit), then you can specify them in two ways:
- Directly by name using the -a flag:
open -a BBEdit /path/to/file
- As the default editor with the -t flag:
open -t /path/to/file
This last option will work similar to the “-e” flag, except that instead of specifically targeting TextEdit, the system will open the default text handler on your system. For a fresh OS X installation, TextEdit is this handler; however, if you have installed a different program and subsequently set it to be the default handler for text files, then using the “-t” flag will open this program instead.
Beyond specifying text editors with the “open” command, you can use it to open pretty much any file or output with a specified application. For instance, if you have an audio file that you would like to open with the VLC media player, then you can specify it as follows:
open -a VLC /path/to/audiofile
Managing streaming output can similarly be done, and allow you to capture logs, errors, or any other output to a program specified by the “open” command, if the “-f” flag is included. For instance, running the command “echo” will send whatever text follows it in the command to the standard output (reports back to the Terminal window), such as the following:
Tophers-Retina-MBP:~ tkessler$ echo "howdy there" howdy there
However, if you pipe this output stream to the “open” command, then you can open it in the specified application. If this applications supports standard input, then it should handle the stream and display it accordingly. For example, running the following command should open TextEdit and create a new document containing with the words “howdy there.”
echo "howdy there" | open -fe