在 macOS 终端中打开 Sublime Text

567

当我在终端中使用.subl命令时,会返回-bash: .subl: command not found

有人知道如何在macOS的命令行中打开Sublime Text 3吗?


4
你对此有什么进展?我也在使用 ST3 的时候遇到了 subl 的问题。我已经在 /usr/local/bin/subl 创建了符号链接,它指向 /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl。我甚至在我的 ~/.bash_profile 中添加了 export PATH=/usr/local/bin:$PATHexport EDITOR="subl -w",但它仍然无法正常工作,并且我收到相同的错误提示。 - rs77
我遇到了一个奇怪的问题,必须打开subl文件夹才能执行命令。以下是执行的命令:Angelfirenze$ /Applications/Sublime\ Text\ 3.app/Contents/SharedSupport/bin/subl ; exit; logout Saving session... ...copying shared history... ...saving history...truncating history files... ...completed. [Process completed] Sublime Text 3最终在新会话中打开。我已经安装了Homebrew并按照说明进行了操作。 - Angelfirenze
7
如果您想从命令行打开文件file.txt,可以使用命令open -a "Sublime Text" file.txt更适合。这将使用Sublime Text打开指定的文本文件,而不需要手动打开软件后再打开文件。 - mareoraft
命令不是.subl,而是subl - the Tin Man
修改了.bash_profile文件后,请确保关闭终端并重新打开。这是唯一真正帮助我的方法。我想确保没有人会被卡在这个明显但非常重要的步骤上^^ - SHANNAX
33个回答

7

除非你确实需要,否则不应该污染/usr/bin目录。

我总是使用/usr/local/bin来存放那些没有受到发行版软件包管理器控制的二进制文件。为什么呢?因为如果软件包管理器更新了,它会始终替换/usr/bin中的文件。

所以我会这样做:

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


7

这个答案适用于 Mac OS VenturaSublime Text 4 (build 4143),它打破了通常的将<APP_PATH>/bin/subl软链接到/usr/local/bin的方法。

一个干净简单的解决方案是使用alias. 在任何相关文件中,如:~/.zshrc~/.zprofile添加:

alias subl="open -a /Applications/Sublime\ Text.app/Contents/MacOS/sublime_text"

然后,source ~/.zshrc

命令subl应该可以工作。


7
根据Sublime设置文档
echo 'export PATH="/Applications/Sublime Text.app/Contents/SharedSupport/bin:$PATH"' >> ~/.zprofile

运行良好。


2
我认为这是最简单的解决方案。 - meyerson

4

这是让它作为别名而工作,而不是符号链接!

这将允许您在终端中运行附加命令,而不会中断subl会话。使用这里的众多符号链接答案(ln -s),会导致终端进程在使用Sublime文本时持续存在。如果您想要分离,可以在Bash配置文件中创建一个别名,如下所示:

  1. Test subl from your ST installation:

    First, navigate to a folder in Terminal that you want ST to open and enter the following command:

    /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. 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. If nothing happens or you get an error from Terminal it will be because it couldn't find the Sublime Text application. This would mean that you would have to check what you've typed (spelling, etc.) OR that Sublime Text isn't installed!

  2. Check and update ".bash_profile":

    Now add the alias in your Bash Profile. Open it via vim ~/.bash_profile. These are the following lines that pertain to having subl work on the command line for Sublime Text:

    ## For Sublime Text 3 alias
    alias subl='/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl'
    ## For Sublime Text global editor preference **Optional
    export EDITOR='subl -w'
    

    I suggest always commenting your code in here with ##. The second line is OPTIONAL and just sets Sublime Text as the default editor. 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

    If you do make any edits to this file once you have closed it, you need to source this file in your current session or close the terminal (tab) and open a new one. You can source this file by running the command source ~/.bash_profile Resolve any errors before moving to the final step.


3

如果您想在Mac终端中使用Sublime打开文件

open 'path/file.txt' -a '/Applications/Sublime Text.app'

2

我正在使用Mac Airbook,请打开终端并输入以下命令:

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

然后输入简单的subl和文件名。
 subl index.py

请使用 /usr/local/bin 中的一个。尽量不要将自定义软链接添加到系统目录中。 - guice

2

我可以在终端中只用一行命令(使用Sublime 3)来实现这个:

alias subl='/usr/local/bin/sublime'

2

我建议如果你是从Sublime Text 2升级过来的话,在按照上述步骤之前,先进入/usr/bin并删除旧的subl。这个升级是值得的。


1

总结一下不同的方法:

  1. 从终端打开Sublime Text
open /Applications/Sublime\ Text.app/

2. 使用Sublime Text打开当前路径中的特定文件(或提供需要打开的文件路径)。
open -a /Applications/Sublime\ Text.app/ myFileToOpen.txt
  1. 通过创建一个名为“sublime”的新别名来缩短您的命令,并使用它

    a. 打开bash_profile:

    nano ~/.bash_profile

    b. 复制此行以创建别名并保存并重新启动终端

    alias sublime="open -a /Applications/Sublime\ Text.app"

    c. 用法:使用sublime text打开apple.txt(如果需要,请提供文件路径)

    sublime apple.txt


1
我正在使用Oh-My-Zshell,之前提到的别名对我没用,所以我编写了一个简单的bash函数,允许您通过使用sublime从命令行打开当前文件夹中的Sublime编辑器。还可以指定要从编辑器中打开的文件。
# Open Sublime from current folder or specified folder
sublime(){
  /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl ./$1
}

使用方法:在终端中打开当前文件夹:

$ sublime

打开特定文件夹的用法:

$ sublime path/to/the/file/to/open

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接