How does Mountain Lion set the default text editor for the "open -t" terminal command?

OSX seem to offer open -t command line shortcut to open arbitrary files in the system default text editor.

  • Where is this default text editor set?

  • Can I change it from the command line (if possible) or otherwise programmatically?

The default choice of TextEdit is limiting.


Solution 1:

It's the default application for plain text (public.plain-text) files. You can change it from Finder's information panels or with duti.

You can also edit ~/Library/Preferences/com.apple.launchservices.plist, but it requires restarting to apply the changes.

defaults write com.apple.LaunchServices LSHandlers -array-add '{LSHandlerContentType=public.plain-text;LSHandlerRoleAll=com.macromates.textmate;}'

Solution 2:

You can also use open -a <application> <file> to open a file from the Terminal in the editor of your choice, like so:

open -a TextWrangler.app myfile.txt

To make this a bit shorter, I also have an alias in my .profile file, like so:

alias edit="open -a TextWrangler.app $1"

This allows me to just type:

edit myfile.txt

and the file will open in TextWrangler.

Solution 3:

According to the open(1) man page:

-t Causes the file to be opened with the default text editor, as determined via LaunchServices

  1. To change the associated editor, find a file with the extension you are trying to open in the Finder and select it.
  2. Select File > Get Info from the menu bar.
  3. Change the "Open With" to your text editor of choice.
  4. Press the "Change All…"

Now your files with that extension should open in your selected text editor with "open -t " from the command line.