以下是在 macOS iTerm2 上安装 zsh-autosuggestions
和 zsh-completions
插件的完整步骤:
前提条件
- 已安装 iTerm2(官网下载)
- 已安装 Zsh(macOS Catalina 及以后版本默认使用 Zsh)
- 已安装 Oh My Zsh(如未安装,先运行以下命令):
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
安装步骤
1. 安装插件
通过 Git 将插件克隆到 Oh My Zsh 的插件目录:
# 进入 Oh My Zsh 插件目录
cd ~/.oh-my-zsh/custom/plugins
# 安装 zsh-autosuggestions(输入建议)
git clone https://github.com/zsh-users/zsh-autosuggestions.git
# 安装 zsh-completions(自动补全)
git clone https://github.com/zsh-users/zsh-completions.git
#克隆插件仓库(字体高亮,可以不安装)
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
2. 启用插件
检查隐藏文件,查看隐藏的文件zshrc是否存在:
ls -a ~
如果存在 .zshrc,它会显示在列表中。如果文件不存在,Zsh 在首次启动时会自动生成 ~/.zsh_history,但不会自动创建 ~/.zshrc。你可以手动创建它:
touch ~/.zshrc
编辑 Zsh 配置文件 ~/.zshrc,打开文件:
3.1 使用:open ~/.zshrc
3.2 或使用:vim ~/.zshrc
进入后按 i 进入编辑模式,退出时按 Esc 输入 :wq 保存并退出。
找到 plugins=(...) 部分,添加插件名称(注意不要留空格):
plugins=(
git
zsh-autosuggestions
zsh-completions
)
3. 更新自动补全功能
在 ~/.zshrc
文件末尾添加以下内容以初始化 zsh-completions
:
fpath+=~/.oh-my-zsh/custom/plugins/zsh-completions/src
autoload -U compinit && compinit
完整案例.png
4. 应用配置
保存文件后退出编辑器(Ctrl + X
→ 按 Y
→ 回车),然后运行:
source ~/.zshrc
验证安装
-
zsh-autosuggestions:输入命令时会出现灰色建议,按
→
或End
键接受。 -
zsh-completions:输入命令后按
Tab
会显示更丰富的补全选项。
常见问题
-
插件不生效:
- 确认
~/.zshrc
中没有语法错误。 - 重启 iTerm2 或运行
exec zsh
。
- 确认
-
补全功能异常:
- 删除 Zsh 的补全缓存后重试:
rm ~/.zcompdump* compinit
- 删除 Zsh 的补全缓存后重试:
-
提示权限问题:
- 确保插件目录可读:(2个命令分开执行)
chmod -R 755 ~/.oh-my-zsh/custom/plugins chmod -R 755 ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting
- 确保插件目录可读:(2个命令分开执行)
完成以上步骤后,你的 iTerm2 + Zsh 环境将具备自动建议和增强补全功能!
item的使用
Command ; //命令补全:同时按住⌘ 键和 ; 分号键
Command Shift ; //查看所有的历史命令记录
iTerm2多窗口操作:
Command N //单独出现一个窗口
Command D //左右拆分窗口
Command shift D //上下拆分窗口