58

Often when I work in the Terminal (the command line) I need to edit some text files. I prefer to edit text files using TextWrangler.

How can I open a text file with TextWrangler from the Terminal?

I tried with /Applications/TextWrangler.app/ my_text_file.txt but it doesn't work since TextWrangler.app/ is a directory.

5 Answers 5

59

As a one-time thing,

open -a /Applications/TextWrangler.app myfile.txt
open -b com.barebones.textwrangler myfile.txt # same thing by ID

You can also create an alias for opening TextWrangler, that you would put on the .bash_profile file, which is an hidden file by default that is usually in your home directory.

This is the command that you could insert:

# Type 'tw' on the terminal to open TextWrangler
alias tw='open -a /Applications/TextWrangler.app'

To make TextWrangler the default:

  1. "Get Info" on a text file in the Finder.
  2. Change the "Open with:" program to TextWrangler, in the fifth information pane.
  3. Click the "Change All..." button at the bottom of the pane.
4
  • After making it the default, then you use open file.txt.
    – user588
    Commented Mar 4, 2011 at 20:42
  • 8
    You can leave out the path to the application completely, as open consults the LaunchServices DB. So open -a TextWrangler file.txt is enough.
    – Asmus
    Commented Mar 5, 2011 at 10:27
  • 1
    when you say "as a one-time thing", do you mean you only have to do it one time (and after that the prefs are saved), or do you mean it only applies one time (and after that you have to do it again to achieve the same result)?
    – chharvey
    Commented Mar 26, 2014 at 4:55
  • If you don't want to use TextWrangler (or any other code editor) as default editor for text files, you can also save a "shortcut" (actually, a shell alias) to make it easier to use TextWrangler on the terminal, typing for example something like tw myfile. Just add to your ~/.bash_profile something like alias tw='open -a TextWrangler'
    – gerlos
    Commented Sep 7, 2014 at 10:44
40

If you have the TextWrangler command line tools installed, you can just type

edit my_text_file.txt

in the terminal and it should open.

If this doesn't work you have to install the TextWrangler command line tools.

If you installed the MacApp store version, download the installer from this page: http://www.barebones.com/support/textwrangler/cmd-line-tools.html

If you downloaded from the BareBones Software site, there should be an "install command line tools" command in either the Application (TextWrangler) menu or the help menu. I think it's in the Text Wrangler menu.

4
  • I just tried and installed TextWrangler, first time I open it asks for "installing command line tools" and voia-lah.
    – cregox
    Commented May 2, 2013 at 11:39
  • @Peter, i wish i could give you a bounty for this! been trying to set textwrangler as my default git editor
    – Brad
    Commented Jan 7, 2014 at 19:38
  • For Mac App Store version, the direct link to the command Line Tools Installer is barebones.com/support/textwrangler/cmd-line-tools.html
    – chrish
    Commented Aug 21, 2015 at 15:54
  • 1
    This used to work, then I upgraded OS X... what happened?
    – Jason S
    Commented Jan 21, 2017 at 16:47
16

IMO, using open is the correct approach. However it requires a bit of typing, so you may want to consider creating a shell alias for it. This approach is also good if you don't want to change your default editors.

$ alias tw='open -a /Applications/TextWrangler.app'
$ tw myfile.txt

To have the alias available in every shell you open, just add the line to .bashrc in your home directory. If you don't have a .bashrc yet, just create it yourself and add the line. Then open a new terminal tab to try it out.

1
  • And, open -a TextWrangler . can conveniently open the current folder of text files. … alias twf='open -a TextWrangler.app . or just tw . with the above alias. Commented Jan 9, 2017 at 3:31
2

If you have the TextWrangler command line tools installed you can use 'edit' to open files in TextWrangler from the command line. You should have been prompted at the initial install to install the tools, or you can go to the preferences and the "Tools" section and click the installer button. It also gives you twdiff, a TextWrangler-based diff tool. You can 'man edit' if the tools are installed to see all the switches available.

1

Another way to edit the text file:

/Applications/TextWrangler.app/Contents/MacOS/TextWrangler my_text_file.txt

Not all applications contain a binary in the MacOS directory the same name as their .app though, so replicating this for another program might be different.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .