Debian 安装 ohMyzsh 并且安装 补全插件
我来为你介绍如何配置 zsh 的命令历史记忆和智能补全功能。这主要通过 zsh-autosuggestions 和 zsh-syntax-highlighting 这两个核心插件实现。
安装插件管理器 (Oh My Zsh)
首先确保你安装了 Oh My Zsh(如果还没有):
# 安装 Oh My Zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
安装自动补全插件
1. 安装 zsh-autosuggestions(历史命令自动建议)
# 克隆插件到 Oh My Zsh 插件目录
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
2. 安装 zsh-syntax-highlighting(语法高亮)
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
配置 ~/.zshrc
编辑你的 zsh 配置文件:
nano ~/.zshrc
修改插件列表
找到 plugins= 这一行,修改为:
plugins=(
git
zsh-autosuggestions
zsh-syntax-highlighting
history-substring-search
colored-man-pages
command-not-found
)
添加历史记录优化配置
在文件末尾添加以下配置:
# ==================== 历史记录配置 ====================
# 历史记录文件大小
HISTSIZE=100000
SAVEHIST=100000
HISTFILE=~/.zsh_history
# 历史记录选项
setopt SHARE_HISTORY # 所有会话共享历史
setopt HIST_IGNORE_DUPS # 忽略重复命令
setopt HIST_IGNORE_ALL_DUPS # 删除旧命令,保留最新的
setopt HIST_SAVE_NO_DUPS # 保存时不写入重复命令
setopt HIST_REDUCE_BLANKS # 删除多余空格
setopt HIST_VERIFY # 执行前验证历史扩展
setopt INC_APPEND_HISTORY # 立即追加到历史文件
setopt EXTENDED_HISTORY # 记录时间戳
# ==================== 自动补全配置 ====================
# zsh-autosuggestions 配置
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#8a8a8a" # 建议文字颜色(灰色)
ZSH_AUTOSUGGEST_STRATEGY=(history completion) # 优先历史,其次补全
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=20 # 最大缓冲大小
ZSH_AUTOSUGGEST_USE_ASYNC=true # 异步加载,提升性能
# 绑定快捷键接受建议
bindkey '^ ' autosuggest-accept # Ctrl+空格 接受建议
bindkey '^f' autosuggest-accept # Ctrl+f 接受建议
bindkey '^e' autosuggest-execute # Ctrl+e 接受并执行
# 标准补全配置
autoload -Uz compinit && compinit
zstyle ':completion:*' menu select # 菜单选择模式
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' # 大小写不敏感
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS} # 彩色补全
生效配置
source ~/.zshrc
使用效果
| 功能 | 操作 |
|---|---|
| 灰色建议 | 输入时自动显示历史命令 |
| 接受建议 | 按 → 或 End 或 Ctrl+空格 |
| 执行建议 | Ctrl+e |
| 历史搜索 | 输入部分命令 + ↑ 键 |
进阶:fzf 历史模糊搜索(强烈推荐)
# 安装 fzf
sudo apt install fzf
# 在 ~/.zshrc 中添加
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
# 绑定 Ctrl+R 触发 fzf 历史搜索
bindkey '^R' fzf-history-widget

浙公网安备 33010602011771号