iTerm2终端搭配zsh shell插件配置
此前有过几篇介绍oh-my-zsh和powerlevel10k配色方案的博客,都是一些花拳绣腿最多可以让人起到赏心悦目的作用,今天就来介绍一些实打实可以提升工作效率的配置。
Plugins
最新的macOS操作系统中已经将默认的登录shell变更为zsh,今日主要介绍两个zsh的插件以及一些配置技巧实现终端智能提示命令建议补全,主角就是zsh-autosuggestions
和zsh-syntax-highlighting
,想必这两个软件会出现在绝大多数Geek的Plugins的配置列表中,就不需要在做过多介绍,下面主要介绍下两种安装配置方式:
方式一
安装:
brew install zsh-autosuggestions zsh-syntax-highlighting
查看详细信息:
brew info zsh-autosuggestions
zsh-autosuggestions: stable 0.7.0 (bottled)
Fish-like fast/unobtrusive autosuggestions for zsh
https://github.com/zsh-users/zsh-autosuggestions
Not installed
From: https://mirrors.aliyun.com/homebrew/homebrew-core.git/Formula/zsh-autosuggestions.rb
License: MIT
==> Caveats
To activate the autosuggestions, add the following at the end of your .zshrc:
source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh
You will also need to force reload of your .zshrc:
source ~/.zshrc
brew info zsh-syntax-highlighting
zsh-syntax-highlighting: stable 0.7.1 (bottled), HEAD
Fish shell like syntax highlighting for zsh
https://github.com/zsh-users/zsh-syntax-highlighting
Not installed
From: https://mirrors.aliyun.com/homebrew/homebrew-core.git/Formula/zsh-syntax-highlighting.rb
License: BSD-3-Clause
==> Options
--HEAD
Install HEAD version
==> Caveats
To activate the syntax highlighting, add the following at the end of your .zshrc:
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
If you receive "highlighters directory not found" error message,
you may need to add the following to your .zshenv:
export ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR=/usr/local/share/zsh-syntax-highlighting/highlighters
==> Analytics
install: 11,970 (30 days), 29,408 (90 days), 121,898 (365 days)
install-on-request: 11,971 (30 days), 29,408 (90 days), 121,845 (365 days)
build-error: 0 (30 days)
~/.zshrc中加载插件
source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
配置文件添加完成以上内容后只需要source ~/.zshrc
配置文件即可生效。
效果如图所示可以实现正确命令行颜色提示以及补全建议信息等。
方式二
安装:
cd ~/.oh-my-zsh/plugins
git clone https://github.com/zsh-users/zsh-autosuggestions.git
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
其实这里的github地址在上面这种方式使用brew info命令查看时已经给出了github URI,注意将插件直接放置到oh-my-zsh的plugins目录下即可。
~/.zshrc中加载插件
plugins=(
git
autojump
zsh-autosuggestions
zsh-syntax-highlighting
)
注意两种安装方式对应了两种不同的生效方式。
配置启动iTerm2自动生效
以上配置完成后,你会发现每次打开iTerm2后必须执行source ~/.zshrc
才能生效,并且不能将该命令直接写入到~/.zshrc
文件中,否则会造成无限循环。
于是研究了两种自动加载该文件的方法:
方法一
依托iTerm2软件功能实现
iTerm2
-->Preferences
-->Profiles
-->General
-->Login Shell
添加如下配置:source ~/.zshrc; clear
该配置项可以保证每次启动iTerm2软件后自动调用命令,并且会将执行命令历史clear掉,但是细心的同学可以发现其实还是能够看到命令的执行过程,反正这种方式我是很难接受,也就不上截图了,甚至觉得是浪费流量,相信大多数人会选择第二种方案,或者有大牛知道更好的方案也可以分享下。
方法二
依托于zsh shell机制
技术原理就不做过多的介绍了,直接上干货:
vim ~/.zprofile
source ~/.zshrc
感兴趣的同学可以研究下zsh登录过程中加载文件的顺序,这里不做过多介绍,能实现目的即可。
从此以后我们就可以实现打开iTerm2就能直接享受补全建议和语法高亮功能啦!
博客地址:https://www.cnblogs.com/Cherry-Linux
座右铭:人总是这么痛苦吗?还是只有小时候这样?总是如此。