簡體   English   中英

在 macOS 中從終端打開 Sublime Text

[英]Open Sublime Text from Terminal in macOS

在終端中使用.subl

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

有人知道如何在 macOS 中從命令行打開 Sublime Text 3 嗎?

我終於讓它在我的 OSX 盒子上工作了。 我使用這些步驟來讓它工作:

  1. 從您的 ST 安裝中測試subl

    首先,導航到終端中您希望 ST 打開的小文件夾並輸入以下命令:

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

    注意:您可能需要將上面命令中的Sublime\\ Text.app替換為Sublime\\ Text\\ 3.appSublime\\ Text\\ 2.app具體取決於應用程序在您的Applications目錄中的存儲位置。 . 在上述命令的末尾打開您所在的當前工作目錄(再次確保您所在的目錄僅包含幾個文件!)。

    如果你沒有讓 Sublime Text 打開你當前的工作目錄,那么接下來的步驟將不起作用。 如果沒有任何反應或者您從終端收到錯誤,那將是因為它找不到 Sublime Text 應用程序。 這意味着您必須檢查您輸入的內容(拼寫等)或未安裝 Sublime Text!

  2. 檢查“.bash_profile”:

    現在是時候在你的PATH文件夾中創建你的符號鏈接了,但是,在我們做之前,讓我們使用nano ~/.bash_profile檢查你的配置文件。 這些是與在 Sublime Text 的命令行上使用subl相關的以下幾行:

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

    第一行設置您希望終端在您的機器上查找二進制文件的位置,我將把我的符號鏈接存儲在/usr/local/bin目錄中 - 我想您可以將它存儲在任何您已經通知終端的地方在哪里尋找二進制文件。

    第二行是可選的,只是將 Sublime Text 設置為默認編輯器。 已添加標志-w ,您可以通過訪問 Sublime Text 文檔來了解有關標志的更多信息: ST3 sublST2 subl

    如果您在關閉此文件后對其進行了任何編輯,則需要運行以下命令:

     source ~/.bash_profile

    編譯您新應用的編輯。 如果在獲取文件后發現任何錯誤,請在進行最后一步之前修復它們。

  3. 創建到 Sublime Text 的符號鏈接:

    現在在您選擇的路徑(我使用/usr/local/bin )中,您現在輸入以下命令:

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

    /Applications/Sublime\\ Text.app/Contents/SharedSupport/bin/subl與您在上述第 1 /Applications/Sublime\\ Text.app/Contents/SharedSupport/bin/subl中輸入並驗證的位置完全相同。 /usr/local/bin/subl是您希望符號鏈接所在的位置 - 需要是上面第 2 /usr/local/bin/subl中的PATH位置之一。

    現在,當您導航到要在 Sublime Text 中打開的文件夾或文件時,現在只需輸入subl后跟文件名或. 打開當前工作目錄。

對於 MAC 10.8+:

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

工作。

假設:

  • 您已經安裝了 Homebrew。
  • /usr/local/bin 在您的 $PATH 中。
  • 您在優勝美地或 El Capitain。

MacOS Sierra 10.12.5 也能正常工作,David Rawson 和 MacOS Sierra 10.12.6 也得到了 Alexander K 的確認。

在終端中運行以下腳本以創建特定的符號鏈接。

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

然后:

subl .

點擊Return ,它應該立即打開 Sublime Text。

這對我有用(我使用的是 OS X Mavericks)

首先,創建一個符號鏈接:

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

現在你可以打開崇高

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

我在 Mac OSX Mavericks 上使用 oh-my-zsh 並且符號鏈接對我不起作用,所以我在我的 .zshrc 文件中添加了一個別名:

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

打開一個新終端,你應該很高興,然后輸入subl

我將使用的一個非常簡單。

open -a "sublime text" [file]

這立即打開了崇高的文本。 您可以指定要打開的文件作為可選參數,例如在當前目錄中打開“myfile.txt”。

有一種簡單的方法可以做到這一點。 它只需要幾個步驟,您不需要過多地使用命令行。 如果您不熟悉命令行,這就是這樣做的方法。

步驟1:找到bin文件來放置subl可執行文件

  1. 打開終端
  2. 輸入cd .. ---------------------這應該返回一個目錄
  3. 輸入ls ------------------------ 查看目錄中的文件列表
  4. 輸入cd .. ---------------------直到你得到一個包含usr的文件夾
  5. 輸入open usr ---------------這應該會打開查找器,您應該會看到一些文件夾
  6. 打開bin文件夾 -------這是您將復制 sublime 可執行文件的地方。

第二步:查找可執行文件

  1. 打開取景器
  2. 在文件下打開一個新的查找窗口(CMD + N)
  3. 導航到應用程序文件夾
  4. 找到 Sublime Text 並右鍵單擊,以便獲得下拉菜單
  5. 單擊顯示包內容
  6. 打開 Content/SharedSupport/bin
  7. 復制subl文件
  8. 粘貼到我們之前找到的usr文件夾下的bin文件夾中
  9. subl的終端類型 --------------這應該打開 Sublime Text

確保它被復制並且它不是快捷方式。 如果確實有問題,請以圖標形式查看 usr/bin 文件夾,然后將subl粘貼到該文件夾​​中的空白區域。 圖標圖像中不應有快捷箭頭。

試試這個。

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

Sublime Text 3文檔中的 Symlink 命令將不起作用,因為 Mac OS X El Capitan 或更高版本的 Home 位置沒有~/bin/目錄。

因此,我們需要將符號鏈接放在/usr/local/bin因為在大多數情況下,此路徑將位於我們的$PATH變量

因此,以下命令應該可以解決問題:

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

一旦你正確創建了符號鏈接,你就可以像這樣運行 Sublime Text 3: subl . (. 表示當前目錄)

您可以在終端中創建一個新別名:

nano ~/.bash_profile

復制這一行並將其粘貼到編輯器中:

alias subl='open -a "Sublime Text"'

點擊control + x ,然后y ,然后輸入以保存並關閉它。

關閉所有終端窗口並再次打開它。

就是這樣,您現在可以使用subl filenamesubl .

zsh 的更新

自 macOS 10.15 Catalina 起,默認 shell 已更改為 zsh。

nano ~/.zshrc

其余步驟保持不變。

請注意不要寫入/usr/bin ,而是寫入/usr/local/bin 第一個是用於將自己的二進制文件寫入系統的應用程序,最新的是用於制作我們自己的系統范圍的二進制文件的特定用途(這是我們在符號鏈接時的情況)。

另外/usr/local/bin/usr/bin之后讀取,因此也是覆蓋任何默認應用程序的好地方。

考慮到這一點,正確的符號鏈接是:

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

在運行 Sublime Text 2 的 OS X Mavericks 中,以下對我有用。

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

在查找器中找到文件並將其拖放到終端窗口中很方便,這樣您就可以確定路徑是正確的,我不是一個龐大的終端用戶,所以這對我來說更舒服。 然后您可以轉到路徑的開頭並開始添加其他部分,例如速記 UNIX 命令。 希望這可以幫助

有用 !!!! 對於我在 MacOS Sierra 10.12.2 上

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

並在終端subl找到它

關閉崇高。 運行此命令。 它會卸載它。 你不會失去你的偏好。 然后再次運行它。 它會自動綁定subl

brew install Caskroom/cask/sublime-text

只需遵循Sublime 文檔

Bash

echo 'export PATH="/Applications/Sublime Text.app/Contents/SharedSupport/bin:$PATH"' >> ~/.bash_profile

ZSH

echo 'export PATH="/Applications/Sublime Text.app/Contents/SharedSupport/bin:$PATH"' >> ~/.zprofile

Linux

sudo ln -s /opt/sublime_text/sublime_text /usr/local/bin/subl

確保在運行命令后關閉終端。

用法

要查看可用標志,請運行subl --help

除非確實需要,否則不應污染 /usr/bin 目錄。 對於那些不受分發包管理器管理的二進制文件,我總是使用 /usr/local/bin。 為什么? 因為如果包管理器得到更新,它總是會替換 /usr/bin 中的文件。

所以我要做的是

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

將此添加到您的 .bashrc 或 .zshrc 是一個簡單的解決方案。

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

以下對我有用

open -a Sublime\ Text file_name.txt
open -a Sublime\ Text Folder_Path

您可以使用別名使其事件變得簡單,例如

在您的

~/.bash_profile

alias sublime="open -a Sublime\ Text $@"

然后下次您可以使用以下命令打開文件/文件夾

sublime file_name.txt
sublime Folder_Path

如果您已將 subl 設置為從命令行調用,則打開當前目錄的正確命令是:

subl .

OS X 命令行”是一個關於如何確保一切都設置好的鏈接。

你可以只添加一個別名

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

然后你應該能夠打開一個文件夾或其他任何東西

subl <path>

對於任何想從終端在 Mac 上使用 Sublime 打開文件的人

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

這是為了讓它作為別名工作,而不是符號鏈接!

這將允許您在終端中運行其他命令而不會中斷 subl 會話。 在此處使用許多符號鏈接答案 ( ln -s ),使終端進程在使用 Sublime 文本時持續存在。 如果您想要分離,請在 Bash 配置文件中創建一個別名,如下所示:

  1. 從您的 ST 安裝中測試subl

    首先,導航到終端中您希望 ST 打開的文件夾並輸入以下命令:

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

    注意:您可能需要將上面命令中的Sublime\\ Text.app替換為Sublime\\ Text\\ 3.appSublime\\ Text\\ 2.app具體取決於應用程序在您的Applications目錄中的存儲位置。 . 在上述命令的末尾打開您所在的當前工作目錄(再次確保您所在的目錄僅包含幾個文件!)。

    如果你沒有讓 Sublime Text 打開你當前的工作目錄,那么接下來的步驟將不起作用。 如果沒有任何反應或者您從終端收到錯誤,那將是因為它找不到 Sublime Text 應用程序。 這意味着您必須檢查您輸入的內容(拼寫等)或未安裝 Sublime Text!

  2. 檢查並更新“.bash_profile”:

    現在在您的 Bash 配置文件中添加別名。 通過vim ~/.bash_profile打開它。 這些是與在 Sublime Text 的命令行上使用subl相關的以下幾行:

     ## 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'

    我建議總是在這里用##注釋你的代碼。 第二行是可選的,只是將 Sublime Text 設置為默認編輯器。 已添加標志-w ,您可以通過訪問 Sublime Text 文檔來了解有關標志的更多信息: ST3 sublST2 subl

    如果你所做的任何修改這個文件一旦你已經關閉了它,你需要source文件在當前會話或關閉終端(標簽),並打開一個新的。 您可以source通過運行命令文件source ~/.bash_profile移動到最后一步之前解決所有錯誤。

我想補充一點,如果您是從 Sublime Text 2 升級,請在按照上述相同的說明操作之前,先進入 /usr/bin 並刪除舊的 subl。 值得升級。

我在終端中只用一行就實現了這一點(使用 Sublime 3):

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

我正在使用 mac airbook 打開你的終端並輸入

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

然后輸入簡單的 subl 和文件名

 subl index.py

總結實現它的不同方法:

  1. 從終端打開 sublime text
open /Applications/Sublime\ Text.app/
  1. 使用 sublime text 打開當前路徑中的特定文件(或提供您需要打開的文件的路徑)
open -a /Applications/Sublime\ Text.app/ myFileToOpen.txt
  1. 通過引入一個名為“sublime”的新別名來縮短你的命令並使用它

    一種。 打開 bash_profile:

    nano ~/.bash_profile

    復制此行以創建別名並保存並重新啟動終端

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

    C。 用法:apple.txt 將以崇高的文本打開(如有必要,請提供文件路徑)

    sublime apple.txt

我正在使用 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

默認路徑中創建文件對我不起作用,因為Menu.sublime-menu文件覆蓋了幾乎所有其他菜單選項,只剩下自定義選項。

對我~/Library/Application Support/Sublime Text 3/Packages/User/Main.sublime-menu是在路徑~/Library/Application Support/Sublime Text 3/Packages/User/Main.sublime-menu (注意目錄User而不是Default )中創建以下文件:

[
    {
        "caption": "File",
        "mnemonic": "F",
        "id": "file",
        "children":
        [
            {
                "caption": "Open Recent More",
                "children":
                [
                    { "command": "open_recent_file", "args": {"index": 1 } },
                    { "command": "open_recent_file", "args": {"index": 2 } },
                    { "command": "open_recent_file", "args": {"index": 3 } },
                    { "command": "open_recent_file", "args": {"index": 4 } },
                    { "command": "open_recent_file", "args": {"index": 5 } },
                    { "command": "open_recent_file", "args": {"index": 6 } },
                    { "command": "open_recent_file", "args": {"index": 7 } },
                    { "command": "open_recent_file", "args": {"index": 8 } },
                    { "command": "open_recent_file", "args": {"index": 9 } },
                    { "command": "open_recent_file", "args": {"index": 10 } },
                    { "command": "open_recent_file", "args": {"index": 11 } },
                    { "command": "open_recent_file", "args": {"index": 12 } },
                    { "command": "open_recent_file", "args": {"index": 13 } },
                    { "command": "open_recent_file", "args": {"index": 14 } },
                    { "command": "open_recent_file", "args": {"index": 15 } },
                    { "command": "open_recent_file", "args": {"index": 16 } },
                    { "command": "open_recent_file", "args": {"index": 17 } },
                    { "command": "open_recent_file", "args": {"index": 18 } },
                    { "command": "open_recent_file", "args": {"index": 19 } },
                    { "command": "open_recent_file", "args": {"index": 20 } },
                    { "command": "open_recent_file", "args": {"index": 21 } },
                    { "command": "open_recent_file", "args": {"index": 22 } },
                    { "command": "open_recent_file", "args": {"index": 23 } },
                    { "command": "open_recent_file", "args": {"index": 24 } },
                    { "command": "open_recent_file", "args": {"index": 25 } },
                    { "command": "open_recent_file", "args": {"index": 26 } },
                    { "command": "open_recent_file", "args": {"index": 27 } },
                    { "command": "open_recent_file", "args": {"index": 28 } },
                    { "command": "open_recent_file", "args": {"index": 29 } },
                    { "command": "open_recent_file", "args": {"index": 30 } },
                    { "command": "open_recent_file", "args": {"index": 31 } },
                    { "command": "open_recent_file", "args": {"index": 32 } },
                    { "command": "open_recent_file", "args": {"index": 33 } },
                    { "command": "open_recent_file", "args": {"index": 34 } },
                    { "command": "open_recent_file", "args": {"index": 35 } },
                    { "command": "open_recent_file", "args": {"index": 36 } },
                    { "command": "open_recent_file", "args": {"index": 37 } },
                    { "command": "open_recent_file", "args": {"index": 38 } },
                    { "command": "open_recent_file", "args": {"index": 39 } },
                    { "command": "open_recent_file", "args": {"index": 40 } },
                    { "command": "open_recent_file", "args": {"index": 41 } },
                    { "command": "open_recent_file", "args": {"index": 42 } },
                    { "command": "open_recent_file", "args": {"index": 43 } },
                    { "command": "open_recent_file", "args": {"index": 44 } },
                    { "command": "open_recent_file", "args": {"index": 45 } },
                    { "command": "open_recent_file", "args": {"index": 46 } },
                    { "command": "open_recent_file", "args": {"index": 47 } },
                    { "command": "open_recent_file", "args": {"index": 48 } },
                    { "command": "open_recent_file", "args": {"index": 49 } },
                    { "command": "open_recent_file", "args": {"index": 50 } }
                ]
            }
        ]
    }
]

結果:

(出於安全原因需要對圖像的某些部分進行模糊處理)

打開-最近-more.png

根據Sublime 設置文檔

echo 'export PATH="/Applications/Sublime Text.app/Contents/SharedSupport/bin:$PATH"' >> ~/.zprofile

工作正常。

從 macOS Catalina(10.15 版)開始使用 zsh。 要讓 Sublime 從命令行運行,請在您的主目錄中編輯.zshrc (不帶參數的cd將使您到達那里)並添加以下行:

export PATH="$PATH:/Applications/Sublime Text.app/Contents/SharedSupport/bin"

對於 Sublime 4+,使用 Bash 更簡單:

echo 'export PATH="/Applications/Sublime Text.app/Contents/SharedSupport/bin:$PATH"' >> ~/.bash_profile

我在 mac 上,這對我有用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM