How to call a text editor from the terminal?

At the Windows command prompt, I can enter notepad helloworld.cpp , which then creates a .cpp file called helloworld and open Notepad for me.

Is there a similar feature for a Mac terminal, preferably with Textmate or Textedit?

I am running Mac OS X Lion 10.7 using the Xcode Developer Tool.

+59
terminal compilation macos
Feb 23 '12 at 21:35
source share
7 answers

There are many ways. Try:

  • vi <filename you want to save or open.cpp> ,
  • pico ,
  • Open /Applications/TextEdit.app <filename> .
+49
Feb 23 2018-12-23T00:
source share
 open -e <filename> 

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

+79
Feb 11 '14 at 15:03
source share

Just use the open <filename> command as described in this article . It will open the application associated with the file type.

Use open -e to open / Applications / TextEdit

+13
Feb 09 '13 at 21:18
source share

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

open -a [appname] [filename]

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

Write the following into your ~/.bashrc (if this file does not exist, you can create it by writing touch ~/.bashrc inside the terminal):

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

And use it as follows:

open2 [appname] [filename]

Please note that appname is the application in your installed application folder ( /Applications ).

The touch command creates the necessary file (do not worry if the file exists, it will not delete / reset the current file, it will only override the modification time to the current time).

+6
Sep 21 '13 at 22:12
source share

If you use text pairing, you can configure it to work with the 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 have entered your path, you can enter the following into the terminal

 mate helloworld.cpp 

if you want text pairing to display all the files in the folder as a project box

 mate . 
+3
Dec 11 '12 at 22:14
source share

A problem with:

 open -e

or

 open -a TextEdit

The fact is that you do not control the TextEdit.app modes: plain text or RichText.

eg. if you try to open the HTML file, TextEdit will open it in Rich Text mode, and not in plain text mode, as expected. Then switching to plain text mode does not display HTML tags.
I could not find the terminal command to activate the Open option:

 Ignore rich text commands

or Preferences parameter:

  Display HTML files as HTML code instead of formatted text

As far as I can see, even osascript will not solve the case.

This is sad 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 fewer users have defined a "default editor."

+3
May 30 '16 at 17:58
source share

Go to settings (โŒ˜ +,) & install shell support. Like here

Then you can open any files from the terminal with:

 open file.txt 

or

 mate file.txt 
0
Feb 08 '18 at 18:32
source share



All Articles