Skip to Content
OSTerminaloh my zsh

使用 oh my zsh 配置 zsh

oh my zsh 这里只做备份,不确保时效性。我个人现在主方案是 zimfw + Starship + zoxide + fzf: oh my zsh/zimfw 负责 plugin,Starship 负责 prompt,zoxide 替代 autojump,fzf 提供模糊搜索。

安装 zsh 和 oh my zsh

Debian / Ubuntu

sudo apt install -y zsh git curl chroma fzf chsh -s "$(command -v zsh)"

zoxide 可以用官方安装脚本安装到 ~/.local/bin

curl -sSfL https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | sh

如果已经用 nix profile 管理用户态工具,也可以直接用 nix 安装。

nix profile add nixpkgs#zoxide

如果 ~/.local/bin 没有在 PATH 里,先加到 ~/.zshrc

echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc

macOS

brew install zsh git curl chroma fzf zoxide chsh -s "$(command -v zsh)"

安装 oh my zsh

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Starship 的安装和 starship.toml 配置看 Starship 配置

获取 zsh 插件

git clone https://github.com/zsh-users/zsh-autosuggestions \ ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions --depth=1 git clone https://github.com/zsh-users/zsh-syntax-highlighting.git \ ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting --depth=1

配置 zsh

编辑 ~/.zshrc,把 oh my zsh 自带主题关掉,并启用常用插件。这里只保留 Starship 一个 prompt,避免和 robbyrussell、p10k 或其他 prompt 初始化冲突。

ZSH_THEME="" plugins=(git docker colorize zsh-autosuggestions zsh-syntax-highlighting)

继续把 zoxide、fzf、Starship 和自用 alias 放到 ~/.zshrc 末尾。systemctljournalctl 相关 alias 只适合 Linux,macOS 上可以删掉。

cat <<'EOF' >> ~/.zshrc # ============================================================ # fzf # ============================================================ # fzf key bindings and completion if command -v fzf >/dev/null 2>&1 && fzf --zsh >/dev/null 2>&1; then source <(fzf --zsh) fi # ============================================================ # zoxide # ============================================================ # zoxide directory jumper eval "$(zoxide init zsh)" alias j="z" alias ji="zi" # ============================================================ # Python venv # ============================================================ activate_venv() { local venv_path="${1:-.venv}" local candidate local activate="" local candidates=( "$venv_path" "$venv_path/bin/activate" "$venv_path/.venv/bin/activate" ) for candidate in "${candidates[@]}"; do if [[ -f "$candidate" ]]; then activate="$candidate" break fi done if [[ -z "$activate" ]]; then print -u2 "No virtual environment activation script found for '$venv_path'." return 1 fi source "$activate" } deactivate_venv() { if (( $+functions[deactivate] )); then deactivate return fi print "No active Python virtual environment." } alias venv="activate_venv" alias va="activate_venv" alias vde="deactivate_venv" # ============================================================ # Aliases # ============================================================ # Git alias g="git" alias gct="git commit" alias gs="git stash" alias gr1="git reset --soft HEAD~1" alias gp="git pull" alias ggp="git push" alias gaa="git add ." alias gch="git checkout" # JavaScript runtime alias nr="npm run" alias br="bun run" alias bx="bunx" # Python alias uvpy="uv run python" alias uvpip="uv pip" # Linux systemd alias sdr="systemctl daemon-reload" alias sr="systemctl restart" alias jl="journalctl -o cat -fu" # Tools alias ard="aria2c --summary-interval=10 -x 3 --allow-overwrite=true -Z" # Starship prompt eval "$(starship init zsh)" EOF source ~/.zshrc

如果之前用过 autojump,可以把历史数据导入 zoxide。

zoxide import --from=autojump ~/.local/share/autojump/autojump.txt

备份方案:Powerlevel10k

如果要临时回退到 p10k,只保留一个 prompt。启用 p10k 时,把 ~/.zshrc 里的 eval "$(starship init zsh)" 注释掉。

git clone --depth=1 https://github.com/romkatv/powerlevel10k.git \ ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k

编辑 ~/.zshrc,把主题改成 p10k。

ZSH_THEME="powerlevel10k/powerlevel10k" p10k configure
Last updated on