63

I work a lot at the command line in a Terminal and would like to start a text editor on a certain file. I'm from Linux Land and normally use kwrite or gedit from a bash shell. Trying to find the Mac equivalent, as a guess, tried

user> textedit somefile.txt

and

user> texteditor somefile.txt

and

user> TextEditor somefile.txt

and other variations. I can't seem to find the proper name of the text editor app. (No, vi isn't to my liking.) Oddly, the guy at an Apple store didn't know this.

  • Both Nathan and mipadi provide answers, but I think they're only partial answers, so I added one. I'm not sure if it would have been better to combine their responses somehow, and if so, how to have done that. – Viktor Haag Jun 27 '11 at 19:07
  • @Viktor it generally isn't good to re-post things that people have already said, and exact duplicates are removed. Since you had something to add, good ways to go would be an answer saying "In addition to Nathan and mipadi's techniques...", or comments on those answers with your addendums. What you've done here is okay, but not exactly a 'best practice'. Thanks for asking; now you know for the future :-). – Nathan Greenstein Jun 27 '11 at 21:27
101

Here are some possible answers, all using the 'open' command-line utility.

The -a option means "open the file argument with the named application":

open -a TextEdit file.txt

The -e option means "open the file argument with the TextEdit application":

open -e file.txt

The -t option means "open the file with the default application for editing text files, as determined via LaunchServices". By default, this will be /Applications/TextEdit.app; however, it's possible for this setting to get overridden:

open -t file.txt

Finally, any file that's of the "text" type will get opened by the application bound to the text type if you just say open file.txt. You can use the "file" command to reveal what the operating system thinks the file type is: file file.txt. So, for example, if you renamed "file.txt" to just "textfile" then open textfile would still open it in the default text-file editing application, as long as file textfile still thought that "textfile" was actually a text file.

A short 'help' file on open can be found by running

open --help

Or you can read the whole manual with

man open
  • 2
    I put this into my .bash_profile: "alias o='open -a Sublime\ Text\ 2'" and then I can simply type at the command prompt: "o text.txt". It's very easy on the fingers ;) – cmroanirgo Jul 15 '14 at 2:57
  • open is the only 'right' way to do this. – CousinCocaine Oct 9 '14 at 9:40
11

The default text editor is TextEdit. You can open a text file in there by using open -a:

$ open -a TextEdit hi.txt

If you want to use another app, just put it's name in, like this:

$ open -a Coda index.html
8

There are two ways:

If your default text editor is TextEdit, you can simply use

open -t file.txt

to open it in TextEditor.

If your default text editor is not TextEdit, and you want to open it in TextEdit specifically, you can use

/Applications/TextEdit.app/Contents/MacOS/TextEdit file.txt

Either of those can be made an alias in your shell config file, of course.

  • Notice that I'm not sure that the convention of passing the file name argument directly to the application's name on the command line may not work for all applications. Only applications that are built to accept filenames on the command line as valid arguments for opening will work this way. I think that the more general open command uses a more thorough method to get an application to "open" the file argument passed to the open command (i.e. it depends on the same functionality by which the Finder's "double click to open" action works). – Viktor Haag Jun 27 '11 at 19:10
  • @Viktor Haag: Both of those will work with TextEdit. – mipadi Jun 27 '11 at 19:18
6

If you search for a comfortable way with more "linux feel" add something like the following to your ~/.profile:

alias textmate='open -a TextMate'

or

alias textedit='open -a TextEdit'

depending on your editors.

  • 2
    Although .profile works, .bash_profile is more common on OS X. – CousinCocaine Oct 9 '14 at 9:41
3

If you already started to write in terminal and you want to continue on your favorite editor you can press ctrl+X, ctrl+E and continue working in emacs or your default bash editor.

If you want to change the default editor, change the environment variable EDITOR to your favorite one:

EDITOR=vim;export EDITOR
0

See if you have the vim editor, I didn't even realize my terminal could use it.

Try:

user> vimtutor 

to figure out its controls.

You can edit files pretty well with it. The syntax looks something like:

user> vim foo.txt
0

TextWrangler (now defunct and replaced with BBEdit) when installed with the Command Line Utilities, allowed you to do:

edit README.txt

to launch TextWrangler, which to me is nicer than open -t README.txt, but maybe I'm just suffering from inertia.

FYI - edit is a binary executable file (from the TextWrangler kit), not just an alias or symlink.

-3

Use gedit command:

gedit your-file.ext
  • 2
    gedit is not installed by default on MacOS, though t can be installed via homebrew brew install gedit – Scot Feb 27 '18 at 5:47
  • It works on Linux :D – Alex Chaliy Feb 27 '18 at 14:00
  • The OP is looking for a MacOS solution, not Linux. And mentioned looking for something to replace gedit on Linux. Also, this is a MacOS site, so a Linux solution would be offftopic. – Scot Feb 27 '18 at 15:58

You must log in to answer this question.

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