一、为什么选择Zsh? Zsh(Z Shell)是Linux/Unix系统下的一个强大的Shell,比Bash更强大、更智能。
1.1 Zsh特点 1 2 3 4 5 6 7 8 9 10 11 ┌─────────────────────────────────────────────────────────────────┐ │ Zsh优势 │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ 🔸 智能补全 → 命令、参数、文件路径都能补全 │ │ 🔸 插件丰富 → 语法高亮、自动建议、git提示等 │ │ 🔸 主题丰富 → 界面美观,提升心情 │ │ 🔸 共享历史 → 多终端共享命令历史 │ │ 🔸 快速跳转 → 快速跳转到常用目录 │ │ │ └─────────────────────────────────────────────────────────────────┘
1.2 Oh My Zsh是什么? Oh My Zsh是一个用于管理Zsh配置的社区框架,内置了大量插件和主题,让终端变得好看又好用。
二、安装Zsh 2.1 Ubuntu/Debian 1 2 3 4 5 6 7 8 9 10 sudo apt updatesudo apt install -y zsh chsh -s /bin/zshecho $SHELL
2.2 macOS 1 2 3 4 5 6 brew install zsh chsh -s /bin/zsh
2.3 验证安装
三、安装Oh My Zsh 3.1 自动安装 1 2 3 4 5 sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh) " sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh) "
3.2 手动安装 1 2 3 4 5 6 7 8 git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zshcp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrcsource ~/.zshrc
四、主题配置 4.1 切换主题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 vim ~/.zshrc ZSH_THEME="robbyrussell" ZSH_THEME=" agnoster" ZSH_THEME="powerlevel10k" ZSH_THEME="starship" ZSH_THEME="pure" source ~/.zshrc
4.2 常用主题推荐
主题
特点
robbyrussell
默认主题,简洁
agnoster
最流行,信息丰富
powerlevel10k
颜值高,速度快
starship
现代跨平台
pure
极简风格
bullet-train
简洁大方
4.3 安装Powerlevel10k 1 2 3 4 5 6 7 8 9 git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME /.oh-my-zsh/custom} /themes/powerlevel10k vim ~/.zshrc ZSH_THEME="powerlevel10k/powerlevel10k" p10k configure
4.4 安装Starship 1 2 3 4 5 6 7 8 9 10 11 12 curl -sS https://starship.rs/install.sh | sh vim ~/.zshrceval "$(starship init zsh) " mkdir -p ~/.config vim ~/.config/starship.toml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 format = """ [character] success_symbol = "[➜](bold green) " error_symbol = "[✗](bold red) " [python] symbol = " " [git_branch] symbol = " " [cmd_duration] min_time = 500
五、插件配置 5.1 启用插件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 vim ~/.zshrc plugins=( git docker pip python vscode zsh-autosuggestions zsh-syntax-highlighting z )source ~/.zshrc
5.2 必备插件 5.2.1 zsh-autosuggestions(自动建议) 1 2 3 4 5 git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom} /plugins/zsh-autosuggestions plugins=(... zsh-autosuggestions)
5.2.2 zsh-syntax-highlighting(语法高亮) 1 2 3 4 5 git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom} /plugins/zsh-syntax-highlighting plugins=(... zsh-syntax-highlighting)
5.2.3 z(快速跳转)
5.3 Git插件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 plugins=(... git) gcl → git clone ga → git add gc → git commit gp → git push gl → git pull gd → git diff gst → git status gb → git branch gco → git checkout gm → git merge gr → git rebase
5.4 Docker插件 1 2 3 4 5 6 7 mkdir -p ~/.zsh/completions curl -L https://raw.githubusercontent.com/docker/cli/master/contrib/completion/zsh/_docker > ~/.zsh/completions/_docker curl -L https://raw.githubusercontent.com/docker/compose/master/contrib/completion/zsh/_docker-compose > ~/.zsh/completions/_docker-compose plugins=(... docker docker-compose)
5.5 更多插件
插件
说明
zsh-history-substring-search
历史命令搜索
zsh-completions
额外补全
fzf
模糊搜索
bat
cat增强版
exa
ls增强版
fd
find增强版
六、实用配置 6.1 基础配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 export CLICOLOR=1 HISTFILE=~/.zsh_history HISTSIZE=10000 SAVEHIST=10000setopt SHARE_HISTORYsetopt HIST_IGNORE_DUPSsetopt CORRECTsetopt CORRECT_ALLsetopt AUTO_CDsetopt AUTO_PUSHDsetopt PUSHD_IGNORE_DUPSautoload -Uz compinit compinit
6.2 别名配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 alias ll='ls -lah' alias la='ls -A' alias l='ls -CF' alias gs='git status' alias ga='git add' alias gc='git commit' alias gp='git push' alias gl='git pull' alias gd='git diff' alias gco='git checkout' alias dps='docker ps' alias dpsa='docker ps -a' alias di='docker images' alias dex='docker exec -it' alias py='python3' alias pip='pip3' alias ip='curl -s https://ipinfo.io' alias c='clear'
6.3 路径显示配置 1 2 3 4 5 6 7 8 9 10 11 12 13 DEFAULT_USER="$USER " prompt_context () { if [[ "$USER " != "$DEFAULT_USER " || -n "$SSH_CLIENT " ]]; then prompt_segment black default "%(!.%root.%n@)%m" fi }
6.4 启动画面 1 2 3 4 5 6 7 DISABLE_AUTO_UPDATE="true" DISABLE_UPDATE_PROMPT="true" DISABLE_AUTO_UPDATE="true"
七、工具推荐 7.1 exa(ls替代) 1 2 3 4 5 6 7 8 9 10 11 12 sudo apt install exa brew install exaalias ls ='exa --icons' alias ll='exa -l --icons --group-directories-first' alias la='exa -la --icons --group-directories-first' alias lt='exa --tree --level=2'
7.2 bat(cat替代) 1 2 3 4 5 6 7 8 9 sudo apt install bat brew install batalias cat ='bat'
7.3 fzf(模糊搜索) 1 2 3 4 5 6 7 git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf ~/.fzf/install Ctrl+r Ctrl+t
7.4 tldr(简化man) 1 2 3 4 5 6 7 8 9 sudo apt install tldr brew install tldr tldr tar
八、实战配置 8.1 完整配置示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 ZSH_THEME="robbyrussell" plugins=( git docker pip python zsh-autosuggestions zsh-syntax-highlighting z ) DISABLE_AUTO_UPDATE="true" alias ll='ls -lah' alias la='ls -A' alias gs='git status' alias ga='git add' alias gc='git commit' alias gp='git push' alias dps='docker ps' export PATH="$HOME /bin:$PATH " HISTFILE=~/.zsh_history HISTSIZE=10000 SAVEHIST=10000setopt SHARE_HISTORYsetopt HIST_IGNORE_DUPSautoload -Uz compinit compinitexport LSCOLORS=ExFxBxDxCxegedabagacadexport LS_COLORS='di=34:ln=35:so=32:pi=33:ex=31:bd=34;46:cd=34;43:su=30;41:sg=30:tw=30;42:ow=30;43' export LANG=zh_CN.UTF-8export EDITOR=vimexport VISUAL=vim
8.2 Powerlevel10k配置 1 2 3 4 5 6 7 8 9 10 11 12 13 p10k configure ZSH_THEME="powerlevel10k/powerlevel10k" vim ~/.p10k.zshtypeset -g POWERLEVEL9K_INSTANT_PROMPT=quiettypeset -g POWERLEVEL9K_DISABLE_HOT_RELOAD=true
九、常见问题 9.1 字体问题 1 2 3 4 5 6 7 8 9 sudo apt install fonts-powerline brew install homebrew/cask-fonts/font-powerline
9.2 加载慢 1 2 3 4 5 6 7 8 9 10 11 12 zmodload zsh/zprofsource ~/.zshrczprof
9.3 补全不工作 1 2 3 4 5 6 7 autoload -Uz compinit compinitrm -f ~/.zcompdump* compinit
十、快捷键汇总 10.1 移动
快捷键
说明
Ctrl+a
行首
Ctrl+e
行尾
Alt+b
后退一个单词
Alt+f
前进一个单词
10.2 编辑
快捷键
说明
Ctrl+u
清除整行
Ctrl+k
清除到行尾
Ctrl+w
删除前一个单词
Ctrl+y
粘贴
10.3 历史
快捷键
说明
Ctrl+r
搜索历史
Ctrl+p
上一条命令
Ctrl+n
下一条命令
Ctrl+g
退出搜索
参考资料
持续更新中…欢迎收藏!
#Zsh #OhMyZsh #终端 #美化 #效率 #工具