How to edit files in TextEdit from the OS X Terminal

NewTerminalIconXIf 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

If you wish to edit the file in TextEdit, then you can do so by using the “open” command in the following manner:

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):

open myfile.html

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:

  1. Directly by name using the -a flag:
    open -a BBEdit /path/to/file
  2. 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

2 thoughts on “How to edit files in TextEdit from the OS X Terminal

  1. Strod

    Topher mentions BBEdit and its little sibling TextWrangler. If you live frequently in the Terminal, a premise in the article, then most certainly you are the kind of user that would really benefit from a serious text editor like either of those two. TextWrangler is free so there is little excuse not to try it. BBEdit is for more advanced users.

    And if you do install either on them, then you should seriously consider installing the command line tools that come with them (choose “Install Command Line Tools…” from the TextWrangler/BBEdit menu). The most basic of those tools binds the command edit to the corresponding GUI editor (TW or BBE), and that’s just fantastic.

Comments are closed.