終端提示外掛 Starship 假裝大佬必備神器


建立時間: 2023年3月17日 22:36
更新時間: 2023年3月18日 02:50

說明

這次要分享一個比較輕量級的終端提示外掛,可以讓終端顯示更多資訊,例如資料夾、git 資訊、python 版本、時間等等,擺脫永遠只有使用者名稱和$的枯燥,很適合喜歡戴面具打電腦的人,最後我也會分享我的設定給大家。

demo

會介紹此外掛主要是因為安裝方便、配置容易,部分文件內容有繁體中文,如果你想要假裝 OG, 我會推薦另外幾款。

  • powerlevel10k
  • powerline
  • oh-my-zsh

上述幾款的特色是,功能更強,不容易安裝和配置。

安裝

在官方文件中有提供中文的安裝說明,因為我是用 mac,此篇將以 mac 安裝做說明,其他的作業系統請參考官方文件。

先決要求

需要先安裝 Nerd Font 字體,做為終端的字體,這是 Nerd Font 有額外提供很多特殊符號的字體,如果使用其他的字體,有些特殊符號可能會無法顯示。

官方文件有推薦 FiraCode Nerd Font,但如果你想要有速度感的字體??你一定要用 DejaVuSansMono Nert Font。

Initial d Deja vu (中文字幕)

下載完解壓縮,打開要安裝的 .ttf 檔,按安裝即可。

install font

接下來就是修改終端字體,我以 mac 內建 Terminal.app 為例。

打開終端 > 偏好選項 > 描述檔 > 文字 > 字體 > 更改

edit font

1. 安裝 starship 執行檔

使用 Homebrew 安裝。

brew install starship

2. 將初始化腳本 (script) 加入你的 shell 的設定檔

我的 mac 使用的是 zsh,配置如下。

~/.zshrc

eval "$(starship init zsh)"

安裝完畢,重啟終端。

配置

要開始設定 Starship,請先建立檔案: ~/.config/starship.toml,所有配置都寫在這個檔案裡。

下面是我個人配置,示意圖如下,黑色的部分是我後製遮住的。

demo my starship

雪人是我自訂的,就是提示請輸入指令的字元。

Terminal.app 外觀

首先我有修改 Terminal.app 的外觀,如之前提到的描述檔我用的是 Dracula。
下載位置:Dracula Dark theme for Terminal.app

比較簡單的方式是使用手動安裝 Install manually

Dracula Terminal.app Install manually

Dracula Terminal.app Configuration

  1. 將檔案解壓縮拖曳到描述檔欄位
  2. 選擇 Dracula
  3. 設定預設值
  4. 再次檢查字體有沒有使用 Nert Font

starship 配置檔

~/.config/starship.toml

# Move the directory to the second line
format = """
$all
$character
"""

# Set 'dracula' as custom color palette
palette = 'dracula256'

# Define custom colors
[palettes.dracula256]
background = '59'
current_line = '60'
foreground = '231'
comment = '103'
cyan = '159'
green = '120'
orange = '222'
pink = '212'
purple = '183'
red = '210'
yellow = '229'

[aws]
disabled = true
style = "bold orange"

[character]
error_symbol = "[✗ ](bold red)"
success_symbol = "[☃ ](bold white)"

[cmd_duration]
style = "bold yellow"

[directory]
style = "bold green"
truncate_to_repo = false
truncation_length = 2
truncation_symbol = '…/'

[git_branch]
style = "bold pink"

[git_status]
style = "bold red"

[hostname]
style = "bold purple"

[python]
# Only use the `python3` binary to get the version.
python_binary = 'python3'

[time]
disabled = false

[username]
show_always = true
style_user = "bold cyan"

最上面沒有 [] 就是提示字元配置,尤其 format 是設定要提式什麼內容到終端,類似 [username] 就是特定模組配置,例如我的 python_binary = 'python3' 一定要指到 python3,python 是 2.7 舊版,不適用。

另一點是調色盤 palette = 'dracula256',我自訂了 dracula256 調色盤,原本 Dracula Dark theme for Starship 是使用十六進制的色碼 #ffffff,但 mac 內建 Terminal.app 只支援 256 色,所以我自己手動切換成顏色相近的 ANSI 色碼。

dracula dark theme for starship 配置檔

如果你使用 iTerm 就可以使用原本的十六進制色碼,原先配置如下。

starship.toml

[aws]
style = "bold #ffb86c"

[character]
error_symbol = "[λ](bold #ff5555)"
success_symbol = "[λ](bold #50fa7b)"

[cmd_duration]
style = "bold #f1fa8c"

[directory]
style = "bold #50fa7b"

[git_branch]
style = "bold #ff79c6"

[git_status]
style = "bold #ff5555"

[hostname]
style = "bold #bd93f9"

[username]
format = "[$user]($style) on "
style_user = "bold #8be9fd"

參考

觀看次數: 3154
promptshellstarshipterminal終端提示
按讚追蹤 Enjoy 軟體 Facebook 粉絲專頁
每週分享資訊技術

一杯咖啡的力量,勝過千言萬語的感謝。

支持我一杯咖啡,讓我繼續創作優質內容,與您分享更多知識與樂趣!