Skip to content

Instantly share code, notes, and snippets.

@Corner430
Last active August 27, 2025 03:08
Show Gist options
  • Select an option

  • Save Corner430/4c7cd4112d7f728189c5f0a1d7e617e4 to your computer and use it in GitHub Desktop.

Select an option

Save Corner430/4c7cd4112d7f728189c5f0a1d7e617e4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Oh My Zsh 安装脚本
# 支持 macOS、Debian/Ubuntu、CentOS/RHEL
set -e # 遇到错误时退出
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 打印带颜色的消息
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检测操作系统
detect_os() {
if [[ "$OSTYPE" == "darwin"* ]]; then
OS="macos"
elif [[ -f /etc/debian_version ]]; then
OS="debian"
elif [[ -f /etc/redhat-release ]]; then
OS="centos"
else
print_error "不支持的操作系统: $OSTYPE"
exit 1
fi
print_info "检测到操作系统: $OS"
}
# 安装 ZSH 和依赖
install_zsh() {
print_info "安装 ZSH 和依赖..."
case $OS in
"macos")
if ! command -v brew &>/dev/null; then
print_error "Homebrew 未安装。请先安装 Homebrew:"
print_info "https://brew.sh"
exit 1
fi
brew install zsh zsh-autosuggestions zsh-syntax-highlighting git curl
;;
"debian")
sudo apt update
sudo apt install -y zsh zsh-autosuggestions zsh-syntax-highlighting git curl
;;
"centos")
sudo yum install -y zsh zsh-syntax-highlighting git curl
;;
esac
print_success "ZSH 安装完成"
}
# 安装 Oh My Zsh
install_oh_my_zsh() {
if [ -d "$HOME/.oh-my-zsh" ]; then
print_warning "Oh My Zsh 已安装,跳过安装步骤"
return
fi
print_info "安装 Oh My Zsh..."
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
print_success "Oh My Zsh 安装完成"
}
# 安装插件
install_plugins() {
ZSH_CUSTOM=${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}
print_info "安装插件..."
# 插件列表
PLUGINS=(
"zsh-users/zsh-autosuggestions"
"zsh-users/zsh-syntax-highlighting"
"zdharma-continuum/fast-syntax-highlighting"
"marlonrichert/zsh-autocomplete"
)
for plugin in "${PLUGINS[@]}"; do
plugin_name=$(basename "$plugin")
plugin_path="$ZSH_CUSTOM/plugins/$plugin_name"
if [ ! -d "$plugin_path" ]; then
print_info "安装插件: $plugin_name"
case $plugin_name in
"zsh-autocomplete")
git clone --depth 1 -- "https://github.com/$plugin.git" "$plugin_path" 2>/dev/null || {
print_warning "插件 $plugin_name 安装失败,跳过"
continue
}
;;
*)
git clone "https://github.com/$plugin.git" "$plugin_path" 2>/dev/null || {
print_warning "插件 $plugin_name 安装失败,跳过"
continue
}
;;
esac
else
print_info "插件 $plugin_name 已存在,跳过"
fi
done
print_success "插件安装完成"
}
# 配置 .zshrc
configure_zshrc() {
ZSHRC="$HOME/.zshrc"
# 备份原配置文件
if [ -f "$ZSHRC" ]; then
cp "$ZSHRC" "$ZSHRC.backup.$(date +%Y%m%d_%H%M%S)"
print_info "已备份原配置文件到 $ZSHRC.backup.$(date +%Y%m%d_%H%M%S)"
fi
# 创建新的 .zshrc 配置
cat > "$ZSHRC" << 'EOL'
# Oh My Zsh 配置
export ZSH="$HOME/.oh-my-zsh"
# 主题设置
ZSH_THEME="ys"
# 插件设置
plugins=(
git
vi-mode
z
zsh-autosuggestions
zsh-syntax-highlighting
fast-syntax-highlighting
zsh-autocomplete
)
# 自动补全设置
autoload -U compinit && compinit
# 历史记录设置
HIST_STAMPS="yyyy-mm-dd"
HISTSIZE=10000
SAVEHIST=10000
setopt SHARE_HISTORY
# 别名设置
alias ll='ls -la'
alias la='ls -A'
alias l='ls -CF'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
# 环境变量
export PATH="$HOME/.local/bin:$PATH"
# 加载 Oh My Zsh
source $ZSH/oh-my-zsh.sh
# 自定义函数
mkcd() {
mkdir -p "$1" && cd "$1"
}
# 设置编辑器
export EDITOR='vim'
export VISUAL='vim'
# 语言环境
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
EOL
print_success ".zshrc 配置完成"
}
# 设置默认 shell
set_default_shell() {
ZSH_PATH=$(which zsh)
if [ "$SHELL" != "$ZSH_PATH" ]; then
print_info "设置默认 shell 为 zsh..."
chsh -s "$ZSH_PATH" || {
print_warning "无法自动设置默认 shell,请手动执行:"
print_info "chsh -s $(which zsh)"
}
else
print_info "默认 shell 已是 zsh"
fi
}
# 显示完成信息
show_completion_info() {
print_success "Oh My Zsh 安装和配置完成!"
echo
print_info "下一步操作:"
echo "1. 重新启动终端或执行: exec zsh"
echo "2. 或者重新登录系统"
echo
print_info "已安装的插件:"
echo "- zsh-autosuggestions: 命令自动补全"
echo "- zsh-syntax-highlighting: 语法高亮"
echo "- fast-syntax-highlighting: 快速语法高亮"
echo "- zsh-autocomplete: 增强自动补全"
echo
print_info "配置文件位置:"
echo "- .zshrc: $HOME/.zshrc"
echo "- 插件目录: $HOME/.oh-my-zsh/custom/plugins"
}
# 主函数
main() {
print_info "开始安装 Oh My Zsh..."
detect_os
install_zsh
install_oh_my_zsh
install_plugins
configure_zshrc
set_default_shell
show_completion_info
}
# 运行主函数
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment