用於更改顏色主題的 OS X 終端命令

用於更改顏色主題的 OS X 終端命令

是否有可用於更改 Mac OS X 終端機的配色方案的命令?我喜歡能夠根據我運行的腳本改變顏色的想法。到目前為止,我只是用 PS1 改變 bash 提示的顏色,這還可以,但沒有我想要的那麼明顯。

答案1

取決於什麼確切地如果您想要實現,這裡有一些使用您的終端樣式在 AppleScript 中實現的想法。這些比 更強大tput,因為它會透過彩色提示重置。等等(至少對我來說)。

這會將運行 Python 的所有選項卡(目前沒有可用於測試的 SSH 伺服器)設定為 Homebrew,將其他選項卡設為 Ocean:

tell application "Terminal"
    repeat with w from 1 to count windows
        repeat with t from 1 to count tabs of window w
            if processes of tab t of window w contains "Python" then
                set current settings of tab t of window w to (first settings set whose name is "Homebrew")
            else
                set current settings of tab t of window w to (first settings set whose name is "Ocean")
            end if
        end repeat
    end repeat
end tell

儲存為腳本並在osascript Name.scpt您想要重新著色 shell 時隨時執行(當然您可以將其包裝為 shell 腳本或其他內容)。

如果您想要以不同方式顯示所有長時間運行的進程,請使用下列條件:

if busy of tab t of window w is true then


或者,您可以設定單一選項卡的樣式,手動選擇:

on run argv
    tell application "Terminal" to set current settings of tab (item 1 of argv as number) of front window to first settings set whose name is (item 2 of argv)
end run

像這樣運行它:

osascript StyleTerm.scpt 3 Homebrew

-> 最前面的終端機視窗的第三個選項卡採用 Homebrew 風格!

如果要修改背景窗口,請將“前窗口”替換為帶有括號的表達式,例如“選項卡”後面的表達式。如果您總是想修改選定的“目前選項卡”,請使用selected tab代替tab (item 1 of argv as number)


.bash_profile如果第一個解決方案對您來說過於體力勞動,請將以下內容添加到您的解決方案中:

PROMPT_COMMAND='osascript "/path/to/Name.scpt"'

現在它會在每個提示之前執行(唯一的問題:不是在啟動某些東西之後執行,即ssh。但是這個主題無論如何都不是關於花哨的 bash 技巧的。這只是一個指針。)

答案2

您的腳本可以使用該tput命令以可移植的方式設定顏色。嘗試以下腳本,您將看到終端清晰為深青色背景,並帶有一些亮青色文字。

#!/bin/bash
tput setab 6
tput clear
tput setaf 14
echo Hello World

您可以在以下位置查看有關此內容的更多信息man 5 terminfo在“顏色處理”部分中。

您可以透過回顯終端直接識別的轉義序列來執行相同的操作。它會更快,但使用其他終端程式可能無法運作。它們中的許多都識別 xterm 序列,下面是使用它們的上面的腳本的樣子。

#!/bin/bash
printf "\033[48;5;6m"  # or "\033[46m"
printf "\033[H\033[2J" # your system's clear command does something similar
printf "\033[38;5;14m" # or "\033[96m"
echo Hello World

有關 xterm 控制序列的更多信息這裡

答案3

您可以使用 applescript 為每個新終端提供一個隨機主題。

編輯.bash_profile並添加此命令

osascript -e "tell application \"Terminal\" to set current settings of front window to some settings set"

如果您獲得相同的隨機主題終端,您可以隨時點擊⌘I並手動設定它。

如果您獲得許多不同外觀的終端主題,這會更有用。如果你環顧四周,有很多這樣的網站。

答案4

這個 AppleScript 在 OS X 14.3 上適用於我(而其他答案則不然)。

tell application "Terminal"
    repeat with i in windows
        try
            set current settings of tabs in i to settings set "Solarized Dark"
        end try
    end repeat
end tell

終端應用程式似乎已經改變了它在 OS X 14.3 中公開其標籤和視窗的方式,因此在迭代某些視窗的標籤時,您會可靠地收到錯誤。但這些似乎不是真正的窗戶,所以你可以忽略它們(我對塊所做的try ... end try)。

我傾向於在 shell 腳本中內聯這樣的小腳本,如下所示:

osascript -e "tell application \"Terminal\"
    repeat with i in windows
        try
            set current settings of tabs in i to settings set \"Solarized Dark\"
        end try
    end repeat
end tell"

我個人使用它來自動在終端機中切換淺色和深色主題(我只需要打開一個新的終端選項卡或運行我的color_mode函數)。您可以查看我執行此操作的完整程式碼在我的 dotfiles 倉庫中

相關內容