How do I invoke a text editor from the terminal?

MacosCompilationTerminal

Macos Problem Overview


In the Windows command prompt, I can type notepad helloworld.cpp which will then create a .cpp file with the name helloworld and open up Notepad for me.

Is there a similar function for Mac Terminal, preferably with Textmate or Textedit?

I'm running Mac OS X Lion 10.7, with Xcode developers tool.

Macos Solutions


Solution 1 - Macos

open -e <filename>

The option -e is used to open the file <filename> with TextEdit.

Solution 2 - Macos

There are plenty of ways. Try:

  1. vi <filename you want to save or open.cpp>,
  2. pico,
  3. Open /Applications/TextEdit.app <filename>.

Solution 3 - Macos

Simply use open <filename> command as described in this article. It will open an app associated with the file type.

Use open -e to open /Applications/TextEdit

Solution 4 - Macos

About some of the previous suggestions here - you can use open command combined with a flag to open a file with specific application:

open -a [appname] [filename]

but if [filename] doesn't exist it displays an error the file doesn't exists or something like that, and doesn't create the required file, as you have requested.

Write the following to your ~/.bashrc file (if that file doesn't exists, you can create it by writing touch ~/.bashrc inside the terminal):

open2()
{
  touch $2
  open -a $1 $2
}

And use it like this:

open2 [appname] [filename]

Note that appname is an application in your installed application folder (/Applications).

The command touch creates you the required file (don't worry, if the file exists it won't remove / reset the current file, only redefine the modification time to the current time).

Solution 5 - Macos

The problem with:

open -e
or
open -a TextEdit
is that you have no control on the TextEdit.app modes: Plain Text or RichText.

E.g. if you try to open an HTML file, TextEdit will open it in the Rich Text mode, not in the Plain Text mode, as expected. Then switching to the Plain Text mode will not show the HTML tags.
I could not find a Terminal command to activate the Open option:

Ignore rich text commands
or the Preference setting:
Display HTML files as HTML code instead of formatted text
As far as I can see, even an osascript won't solve the case.

This is unfortunate since TextEdit.app is the only text editor that is present for sure. Not all Mac users have installed BBedit, TextMate, or any other third party editor and even less users have defined a "default editor".

Solution 6 - Macos

If your using text mate you can set it up to work with terminal

ln -s /Applications/TextMate.app/Contents/Resources/mate ~/bin/mate

Taken from

http://manual.macromates.com/en/using_textmate_from_terminal.html

Once you've got mate into your path you can type the following into the terminal

mate helloworld.cpp

if you want text mate to display all files in a folder as a project drawer

mate .

Solution 7 - Macos

You can also use:

nano <filename you want to open>

Solution 8 - Macos

Go to Preferences (⌘+,) & install shell support. Like here

Then you could open any files from terminal with:

open file.txt

or

mate file.txt

Solution 9 - Macos

The answer to the question, for me, was:

leafpad

Solution 10 - Macos

In macOS, you can just run open <file-name> on the terminal to open the relevant file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCeetangView Question on Stackoverflow
Solution 1 - MacosVito GentileView Answer on Stackoverflow
Solution 2 - MacospyCthonView Answer on Stackoverflow
Solution 3 - MacosejboyView Answer on Stackoverflow
Solution 4 - MacosRoy MilohView Answer on Stackoverflow
Solution 5 - MacoslinusView Answer on Stackoverflow
Solution 6 - Macosuser1873471View Answer on Stackoverflow
Solution 7 - MacosJakubView Answer on Stackoverflow
Solution 8 - MacosSeargeView Answer on Stackoverflow
Solution 9 - MacosMarcView Answer on Stackoverflow
Solution 10 - MacosKalhara Randil TennakoonView Answer on Stackoverflow