如何从终端调用文本编辑器?

发布于 2025-01-08 14:27:51 字数 231 浏览 2 评论 0原文

在 Windows 命令提示符中,我可以输入 notepad helloworld.cpp,然后它将创建一个名为 helloworld.cpp 文件并打开记事本为我。

Mac Terminal 是否有类似的功能,最好使用 Textmate 或 Textedit?

我正在运行 Mac OS X Lion 10.7,带有 Xcode 开发人员工具。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

初熏 2025-01-15 14:27:51
open -e <filename>

选项-e用于使用TextEdit打开文件

open -e <filename>

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

阳光①夏 2025-01-15 14:27:51

有很多方法。尝试:

  1. vi <要保存的文件名或 open.cpp>
  2. pico
  3. Open /Applications/TextEdit.app <文件名>

There are plenty of ways. Try:

  1. vi <filename you want to save or open.cpp>,
  2. pico,
  3. Open /Applications/TextEdit.app <filename>.
友欢 2025-01-15 14:27:51

只需使用 文章。它将打开与该文件类型关联的应用程序。

使用 open -e 打开 /Applications/TextEdit

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

流心雨 2025-01-15 14:27:51

关于之前的一些建议 - 您可以使用 open 命令结合 a 标志来打开具有特定应用程序的文件:

open -a [appname] [filename ]

但如果 [filename] 不存在,则会显示错误 文件不存在 或类似内容,并且不会创建所需的文件文件,按照您的要求。

内容写入您的 ~/.bashrc 文件(如果该文件不存在,您可以通过在终端内写入 touch ~/.bashrc 来创建它):

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

将以下 像这样使用它:

open2 [appname] [filename]

请注意,appname 是已安装的应用程序文件夹 (/Applications) 中的一个应用程序。

命令 touch 创建您所需的文件(不用担心,如果该文件存在,它不会删除/重置当前文件,只会将修改时间重新定义为当前时间)。

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).

明媚如初 2025-01-15 14:27:51

问题是:

open -e

open -a TextEdit

是您无法控制 TextEdit.app 模式:纯文本或 RichText。

例如,如果您尝试打开 HTML 文件,TextEdit 将以富文本模式打开它,而不是按预期以纯文本模式打开它。然后切换到纯文本模式将不会显示 HTML 标签。
我找不到终端命令来激活“打开”选项:

Ignore rich text commands

或“首选项”设置:

Display HTML files as HTML code instead of formatted text

据我所知,即使 osascript 也无法解决此问题。

这是不幸的,因为 TextEdit.app 是唯一存在的文本编辑器。并非所有 Mac 用户都安装了 BBedit、TextMate 或任何其他第三方编辑器,甚至更少的用户定义了“默认编辑器”。

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".

↙厌世 2025-01-15 14:27:51

如果您使用文本伙伴,您可以将其设置为与终端一起使用

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

取自

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

将 mate 添加到路径中后,

mate helloworld.cpp

如果您希望 text mate 将文件夹中的所有文件显示为项目抽屉,则可以在终端中键入以下内容

mate .

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 .
·深蓝 2025-01-15 14:27:51

您还可以使用:

nano <您要打开的文件名>

You can also use:

nano <filename you want to open>

夏末 2025-01-15 14:27:51

转到首选项 (⌘+,) &安装外壳支持。
Like here

然后您可以使用以下命令从终端打开任何文件:

open file.txt

mate file.txt

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

Then you could open any files from terminal with:

open file.txt

or

mate file.txt
陈甜 2025-01-15 14:27:51

对于 macOS Monterey 版本 12.4,命令 open 会对您有所帮助。在此命令之后,您应该放置一个空格,然后使用 / 键入文件路径,例如:
打开/Users/your_username/desktop/filename.txt

For macOS Monterey version 12.4 command open would help you. After this command you should put a space and then type the file path using / for example:
open /Users/your_username/desktop/filename.txt

り繁华旳梦境 2025-01-15 14:27:51

对我来说,这个问题的答案是:

leafpad

The answer to the question, for me, was:

leafpad

就像说晚安 2025-01-15 14:27:51

在 macOS 中,您只需在终端上运行 open 即可打开相关文件。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文