Wednesday, October 08, 2008

Using Mac's TextEdit from the command line

Just a short little note. As I recently mentioned there are a few things which I actually do not like about my Mac. One is that the Finder per default does not display hidden files and the second related issue that TextEdit is not in the default path of the console.
Why is that important you ask? Well I would like to say for example say at the command line:

> edit foo.txt

or

> edit ~/.bashrc

For the former I could open Finder, locate the file and double click. For the former I am pretty much out of luck. Of course I also could just use vi and in fact that's what I am doing most of the time, but then there are the times where I just want to have another simple editor. To come to the point, here is what I did and what I would like to share with you. Just add this to your ~/.bashrc (or ~/.bash_profile as a matter of fact):

function edit()
{
    /Applications/TextEdit.app/Contents/MacOS/TextEdit $@ 2>/dev/null
}

Have fun,
--Hardy

4 comments:

Vivek said...

Alternative way to do this.

add the following line to .bashrc:

alias edit='open -a textmate'

Hardy Ferentschik said...

That's of course if you have Textmate installed.

The benefit of the simple open command I discovered actually myself only a short while after I wrote this blog. Today I wold not like to miss it anymore ;-)

Mark said...

On my Mac running Leopard

open -e .bash_profile

opens .bash_profile using TextEdit.

Alexander Orlov said...

Thx Mark!

Unbelivable but on my Mac it works also! :D However I think putting

alias edit='open -e'

into your ˜/.bash_profile is a better solution than the "function edit()" version.