在macOS中从终端打开Sublime Text

本文翻译自:Open Sublime Text from Terminal in macOS

In Terminal when I use .subl 在终端中,当我使用.subl

It returns -bash: .subl: command not found 它返回-bash: .subl: command not found

Anyone know how to open Sublime Text 3 from the command line in macOS? 有人知道如何从macOS的命令行中打开Sublime Text 3吗?


#1楼

参考:https://stackoom.com/question/15yFZ/在macOS中从终端打开Sublime-Text


#2楼

if you have subl set up to be called from the command line, the proper command to open the current directory is: 如果已设置要从命令行调用的subl,则打开当前目录的正确命令是:

subl .

" OS X Command Line " is a link on how to make sure everything is set up. OS X命令行 ”是有关如何确保所有设置均已设置的链接。


#3楼

I finally got this to work on my OSX box. 我终于可以在OSX盒子上使用它了。 I used these steps to get it to work: 我使用以下步骤使其正常工作:

  1. Test subl from your ST installation: 从ST安装中测试subl

    First, navigate to a small folder in Terminal that you want ST to open and enter the following command: 首先,在终端中导航到要ST打开的小文件夹,然后输入以下命令:

     /Applications/Sublime\\ Text.app/Contents/SharedSupport/bin/subl . 

    NOTE: You may need to replace Sublime\\ Text.app in the command above to Sublime\\ Text\\ 3.app or Sublime\\ Text\\ 2.app depending upon where the application is stored in your Applications directory. 注意:您可能需要在上面的命令Sublime\\ Text.app替换为Sublime\\ Text\\ 3.appSublime\\ Text\\ 2.app具体取决于应用程序在Applications目录中的存储位置。 The . . at the end of the above command opens the current working directory you are located in (again make sure you're in a directory that only contains a few files!). 在以上命令的末尾,将打开您所在的当前工作目录(再次确保您位于仅包含几个文件的目录中!)。

    If you DO NOT get Sublime Text opening your current working directory then the next set of steps will NOT work. 如果您没有在打开当前工作目录时看到Sublime Text,则接下来的步骤将不起作用。 If nothing happens or you get an error from Terminal it will be because it couldn't find the Sublime Text application. 如果什么也没发生,或者您从Terminal收到错误消息,那是因为它找不到Sublime Text应用程序。 This would mean that you would have to check what you've typed (spelling, etc.) OR that Sublime Text isn't installed! 这意味着您将必须检查您键入的内容(拼写等),或者未安装Sublime Text!

  2. Check ".bash_profile": 检查“ .bash_profile”:

    Now it's time to create your symbolic link in your PATH folder, BUT, before we do, let's check your profile file by using nano ~/.bash_profile . 现在是时候在PATH文件夹BUT中创建符号链接了,在此之前,让我们使用nano ~/.bash_profile检查您的配置文件。 These are the following lines that pertain to having subl work on the command line for Sublime Text: 这些是与在Sublime Text的命令行上使subl工作有关的以下行:

     export PATH=/bin:/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:$PATH export EDITOR='subl -w' 

    The first line sets the location where you want Terminal to look for binaries on your machine, I'm going to store my symbolic link in the /usr/local/bin directory - I guess you could store it anywhere provided you've notified Terminal where to look for binaries. 第一行设置您希望终端在计算机上查找二进制文件的位置,我将把符号链接存储在/usr/local/bin目录中-我猜您可以将其存储在通知终端的任何位置在哪里寻找二进制文件。

    The second line is OPTIONAL and just sets Sublime Text as the default editor. 第二行是OPTIONAL,仅将Sublime Text设置为默认编辑器。 The flag -w has been added and you can find out more about flags by going to the Sublime Text docs: ST3 subl or ST2 subl 标志-w已添加,您可以通过转到Sublime Text文档: ST3 sublST2 subl来找到有关标志的更多信息。

    If you do make any edits to this file once you have closed it, you need to run the command: 如果您在关闭文件后确实对其进行了任何编辑,则需要运行以下命令:

     source ~/.bash_profile 

    to compile your newly applied edits. 来编译您新应用的修改。 If you see any errors after sourcing your file get them fixed before moving to the final step. 如果在采购文件后看到任何错误,请先进行修复,然后再进行最后一步。

  3. Create a symbolic link to Sublime Text: 创建一个符号链接到Sublime Text:

    Now in your chosen path (I used /usr/local/bin ) you now enter the following command: 现在在您选择的路径(我使用/usr/local/bin )中,输入以下命令:

     ln -s /Applications/Sublime\\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl 

    The /Applications/Sublime\\ Text.app/Contents/SharedSupport/bin/subl being EXACTLY the same location as what you entered and verified as working in STEP 1 above. /Applications/Sublime\\ Text.app/Contents/SharedSupport/bin/subl与上述步骤1中输入并验证为可以使用的位置完全相同。 The /usr/local/bin/subl being the location of where you want the symbolic link to be located - needs to be one of your PATH locations from STEP 2 above. /usr/local/bin/subl是您希望符号链接位于的位置-必须是上述STEP 2中的PATH位置之一。

    Now when you navigate to a folder or file that you want to open in Sublime Text you now just enter subl followed by the name of the file or . 现在,当您导航到要在Sublime Text中打开的文件夹或文件时,现在只需输入subl然后输入文件名或即可. to open the current working directory. 打开当前工作目录。


#4楼

This worked for me (I'm using OS X Mavericks) 这对我有用(我正在使用OS X Mavericks)

First, create a symbolic link: 首先,创建一个符号链接:

sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/bin/subl

Now you can open sublime with 现在您可以打开

subl "/a/path/to/the/directory/you/want/to/open"

#5楼

For MAC 10.8+: 对于MAC 10.8+:

sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl

worked. 工作了。


#6楼

I'm using oh-my-zsh on Mac OSX Mavericks and the symbol link didn't work for me, so I added an alias in my .zshrc file instead: 我在Mac OSX Mavericks上使用oh-my-zsh,符号链接对我不起作用,因此我在.zshrc文件中添加了一个别名:

alias subl="/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl"

Open a new terminal and you should be good to go, and type subl . 打开一个新的终端,您应该subl顺利,然后输入subl

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个错误提示是因为你在执行一个名为 `$file _basename` 的文件,但是系统找不到这个文件。可能是你在执行命令时写错了文件名。 或者,如果你是使用 Sublime Text 编辑器来运行 Python 文件,那么可能是你的配置出了问题。你可以尝试重新配置 Sublime Text 来确保它能够正确地运行 Python 文件。 ### 回答2: 根据你的问题描述,你可能遇到了在Mac上使用Python打开文件时遇到的一个问题。你提到了路径为'/Applications/Sublime Text.app/Contents/MacOS/$file _basename'的文件无法打开。这个问题可能是由于文件路径错误或文件不存在导致的。 首先,你需要确保你输入的文件路径是正确的。在Mac上,文件路径是以斜杠(/)分隔的。应用程序文件通常位于/Applications文件夹,而Sublime Text.app是你提到的文件名。因此,正确的文件路径应该是'/Applications/Sublime Text.app'。确保在路径没有任何拼写错误或额外的空格。 另外,你还需要检查文件是否真正存在。你可以在终端使用ls命令来列出特定路径下的文件和文件夹。在终端,输入以下命令: ls /Applications/Sublime\ Text.app/Contents/MacOS/ 如果文件存在,它将在终端显示。否则,它会显示一个错误消息,表示该文件不存在。 如果路径和文件都是正确的,但是仍然无法打开文件,那可能是由于文件权限的问题。你可以尝试更改文件的权限,以便能够读取或执行它。在终端,使用chmod命令来更改文件的权限,例如: chmod +x /Applications/Sublime\ Text.app/Contents/MacOS/$file _basename 上述命令将给文件添加可执行权限。 如果你不确定如何正确设置文件路径和权限,并且仍然无法解决问题,建议你向相关的技术支持寻求帮助。 ### 回答3: 这个错误信息出现是因为在Mac上使用Sublime Text编辑器运行Python脚本时,脚本的路径包含了一个特殊的字符串"$file _basename",而Sublime Text无法识别这个字符串作为有效的文件路径。 要解决这个问题,可以尝试以下几种方法: 1. 检查文件路径:确保文件路径是正确的,没有拼写错误或者无效字符。可以试着手动打开文件路径,看是否能够成功打开。 2. 重新安装Sublime Text:有时候Sublime Text的安装文件可能出现问题,导致无法正常运行。可以尝试重新下载并安装Sublime Text最新版本,然后再次运行Python脚本。 3. 更改脚本保存位置:将Python脚本保存到其他目录或者桌面上试试,看是否能够正常执行。有时特定的文件夹或者路径可能会导致Sublime Text无法正确识别。 4. 修改文件名:将文件名的特殊字符或者无效字符删除或者替换成合法的字符,然后再次尝试运行Python脚本。 5. 使用终端运行脚本:可以尝试在终端运行Python脚本,看是否能够正常执行。在终端,使用命令"python 脚本路径"即可运行Python脚本。 如果以上方法都无法解决问题,可能需要进一步检查你的系统设置、Sublime Text配置或者Python环境是否有问题。你可以考虑查阅相关文档或者寻求更专业的技术支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值