I frequently find that a Terminal command to open a file in TextEdit would be very handy. Does such a command exist on Mac OS X?
5 Answers
open -a TextEdit filename
should do the trick.
The -a
flag specifies any application you want, so it's applicable to any number of situations, including ones where TextEdit isn't the default editor.
Other relevant options
-t
opens in the default editor (i.e. if you use BBEdit, TextMate, etc.)-e
will open the file specifically in TextEdit

- 41,194
-
3
-
If you want a program whose name has spaces, you'll need to escape them, e.g., open -a Adobe\ Photoshop\ CS6 image.png – Choylton B. Higginbottom Jun 03 '15 at 00:13
-
How to open it with SublimeText3 that stays in /path/SublimeText3? – emeraldhieu Jan 14 '16 at 09:51
-
7In
open
command's manual, you will see-e Causes the file to be opened with /Applications/TextEdit
. Soopen -e filename
do the same thing, and is simpler. – DawnSong Mar 31 '16 at 11:47 -
Are the available applications defined as those living inside of
/Applications/
? – Daniel Jul 17 '18 at 15:33 -
1It's all the applications registered with Launch Services, so they don't have to be in /Applications. – robmathers Jul 17 '18 at 17:06
-
Direct, easy answer - add an alias to your ~/.bash_profile like:
alias textedit='open -a TextEdit'
Invoke it like:
textedit theFiletoEdit.txt
The difference between this and the other answer, is you can easily remember it when you want it. Typing text
and Tab to autocomplete it will make it instantly available
For those with a bit more bash background, who want the reasoning without having to fuss with figuring it out:
The problem is that a trivial symlink (or its ilk, like a shell script redirect) to
/Applications/TextEdit.app/Contents/MacOS/TextEdit
causes a new instance ofTextEdit
to open the file you pass to it. This works, but does not give your expected Mac OS X default behavior. (if you try it, you'll see)To get the default UI behavior, using the Mac built-in
open
command, with that '-a TextEdit' flag (that others mentioned) induces the Mac UI to have (any) currently running instance of TextEdit handle the call (with no inapropos 'sudo' needed for general case usage).

- 100,768

- 671
-
Opening a file in an already running textedit instance does not work in mojave. I tried all the flags. – Karl Pokus Sep 02 '19 at 18:44
The open command can be used to open files (in their default apps, unless using the -a flag), URLs (in your default web browser), and directories (in Finder).
An example would be
open /Users/Example/Desktop/example.rtf

- 2,047

- 8,363
-
1This is probably the easiest way to open text files for edit in a graphical editor from the terminal. – David Thomas Nov 08 '14 at 23:54
For those finding this post:
The solution was to use the command sudo open -t /path/file.txt
And the man pages for the open command also show how to do some other things like show the file in the finder.
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/open.1.html

- 2,809
-
13
-
1Unless the file isn't accessible to the user otherwise. But in general, agreed. – Bobson Mar 02 '12 at 12:12
-
2Actually, I just tried using this command with sudo to edit
/etc/hosts
on Lion and it just doesn't work (as expected). It doesn't allow me to edit the file. Only clone it. Might as well go without thesudo
, in this case. The file will be marked as locked anyway. To really be able to do so, have to first open the TextEdit in root mode, or just use a different editor: http://apple.stackexchange.com/questions/20199/how-do-i-open-a-file-as-root-in-textedit-on-lion – cregox May 02 '13 at 11:22
This is not a programming question. But I did a quick google and found this site:
http://ss64.com/osx/pico.html pico: Simple text editor
http://ss64.com/vi.html vi: Text Editor
And this site explains how to use TextEdit from terminal.
http://www.tech-recipes.com/rx/2754/os_x_edit_file_using_textedit_as_root_superuser/
-
1
-
-
It absolutely does, thanks for adding that, you are a much faster googler than I. – Sep 22 '11 at 09:24
-
sudo
, gota open TextEdit in root mode first – cregox May 02 '13 at 11:26sublime
command. – Tuesday Nov 19 '16 at 21:44