跳转到内容

从 zsh 和 oh-my-zsh 迁移

大多数人使用 oh-my-zsh 及其热门插件所追求的功能,grfsh 都已内置,无需插件或框架。

zsh 配置 grfsh
zsh-autosuggestions 内置
zsh-syntax-highlighting、fast-syntax-highlighting 内置
powerlevel10k、spaceship、pure configure-prompt,配合 rainbow、classic、lean 或 pure 预设
p10k 的瞬态提示符 在主题中设置 transient = true
z、zoxide、autojump 内置 z
git 插件的别名 abbr,见下文
zsh-abbr 内置 abbr
starship 在主题中设置 engine = starship
command-not-found 内置“你是不是想输入”提示

运行 configure-prompt,选择最接近你当前主题的风格。和 p10k 一样,向导会实时预览每个选项。

zsh grfsh
~/.zshrc ~/.grfshrc
~/.zprofile、~/.zlogin ~/.grfsh_profile
~/.zsh_history ~/.grfsh_history
$ZSH_CUSTOM/plugins/name ~/.config/grfsh/plugins/name/init.grfsh

grfsh 不会读取 ~/.zshenv 或 ~/.zprofile,请把其中的 export PATH=... 和其他 export 语句移到 ~/.grfsh_profile,登录 shell 会读取这个文件。在 macOS 上,登录 shell 会自动加入 /etc/paths 和 /etc/paths.d 中的目录,也就是 zsh 通过 path_helper 得到的那些目录,因此 Homebrew 不需要额外配置。例如:

终端窗口
export PATH="$HOME/.local/bin:$PATH"

brew shellenv 输出的是 grfsh 无法运行的 zsh 语法,不要在 grfsh 中对它使用 eval。

~/.zshrc 中的别名和 export 可以原样复制。对于 oh-my-zsh 的 git 别名,使用缩写更合适,因为历史记录里会保存完整的命令:

终端窗口
abbr g git
abbr gst 'git status'
abbr gco 'git checkout'
abbr gcm 'git checkout main'
abbr gp 'git push'
abbr gl 'git pull'
abbr gd 'git diff'
abbr glog 'git log --oneline --graph --decorate'
zsh grfsh
precmd、preexec、chpwd 函数 同名函数
add-zsh-hook precmd fn precmd_functions+=(fn)
bindkey '^R' history-incremental-search-backward bindkey '^R' reverse-search-history,bindkey -l 列出所有动作
bindkey -v set -o vi
setopt AUTO_CD 始终开启
setopt globstar 风格的 ** shopt -s globstar
  • grfsh 的语法遵循 bash,因此 zsh 独有的特性并不存在,例如 glob 限定符(*(.))、${(s/:/)var} 这类参数标志,以及从 1 开始的数组下标。和 bash 一样,数组从 0 开始。
  • 没有 setopt 和 autoload。运行 grfsh --check ~/.zshrc 可以看到每一行需要处理的地方。
  • 不会加载 zsh 主题。grfsh 根据主题文件自行绘制提示符,详见提示符主题。