#!/bin/bash # macOS 的 zsh 工具安裝與設定腳本(以 Bash 執行)。 # 使用方式: # curl -sL "https://vim.chan15.info/download/get_shell_tools_macos.sh" | bash # # 執行內容: # 1. 確認作業系統為 macOS,且已安裝 Homebrew;不符合時結束執行。 # 2. 透過 Homebrew 安裝以下工具: # - zsh-autosuggestions:依歷史紀錄顯示指令建議。 # - zsh-syntax-highlighting:為指令與參數加上語法色彩。 # - fzf:模糊搜尋,並啟用 Ctrl-R 歷史搜尋、Ctrl-T 檔案搜尋等整合。 # - fzf-tab:使用 fzf 顯示 zsh 的 Tab 補全選單。 # - zoxide:依使用紀錄快速切換目錄。 # - bat:提供語法上色的檔案檢視工具。 # - jq:提供 JSON 查詢與處理工具。 # 3. 若 ~/.zshrc 不存在則建立,並追加上述 zsh 外掛與工具的初始化指令。 # 相同的設定行不重複加入;缺少 compinit 時會在 fzf-tab 前初始化補全。 # zsh-syntax-highlighting 的載入設定會最後追加;bat 與 jq 僅安裝。 # 4. 提示重新載入 ~/.zshrc 或重開終端機,讓設定生效。 # 注意:此腳本會安裝套件並修改 ~/.zshrc,不會自動安裝 Homebrew。 yellow='\033[1;33m' no_color='\033[0m' # No Color if [[ "$(uname -s)" != "Darwin" ]]; then echo "This script only supports macOS." exit 1 fi if ! command -v brew >/dev/null 2>&1; then echo "Homebrew is required. Install it from https://brew.sh, then run this script again." exit 1 fi echo "Installing zsh-autosuggestions zsh-syntax-highlighting fzf fzf-tab zoxide bat jq..." brew install zsh-autosuggestions zsh-syntax-highlighting fzf fzf-tab zoxide bat jq zshrc="$HOME/.zshrc" [[ -f "$zshrc" ]] || touch "$zshrc" added=0 append_if_missing() { local line="$1" if ! grep -Fqx "$line" "$zshrc" 2>/dev/null; then echo -e "\n$line" >> "$zshrc" added=1 fi } has_compinit() { awk ' /^[[:space:]]*#/ { next } { sub(/[[:space:]]+#.*/, "") if ($0 ~ /(^|[[:space:];&])compinit([[:space:];&]|$)/) found=1 } END { exit !found } ' "$zshrc" } # gray-text history suggestions append_if_missing 'source "$(brew --prefix)/share/zsh-autosuggestions/zsh-autosuggestions.zsh"' # fuzzy finder shell integration (Ctrl-R history, Ctrl-T file search, etc.) append_if_missing 'eval "$(fzf --zsh)"' # fzf-tab requires zsh's completion system to be initialized first. if ! has_compinit; then append_if_missing 'autoload -Uz compinit && compinit' fi # tab-completion menu powered by fzf append_if_missing 'source "$(brew --prefix)/share/fzf-tab/fzf-tab.plugin.zsh"' # smart cd append_if_missing 'eval "$(zoxide init zsh)"' # command/argument syntax highlighting must be sourced last append_if_missing 'source "$(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"' if [[ "$added" -eq 1 ]]; then echo -e "${yellow}Updated $zshrc. Please run 'source $zshrc' or restart your terminal to activate it.${no_color}" else echo -e "${yellow}$zshrc already configured, nothing to add.${no_color}" fi echo -e "${yellow}Shell tools installed and configured.${no_color}"