iTerm2 + Oh My Zsh 配置

创建时间: 2021年12月27日
更新时间: 2021年12月28日

参考:这篇 iTerm2 + Oh My Zsh 教程手把手让你成为这条街最靓的仔

iTerm2 简介

iTerm2是终端的替代品,也是iTerm的继承者。它适用于装有macOS 10.14或更新版本的Mac

安装 iTerm2

官网下载

下载成功后,拖入Application

安装 oh-my-zsh

Oh My Zsh是一个开源、社区驱动的框架,用于管理Zsh配置

官网提供了两种安装方式:

扩展:什么是curl和wget命令

via curl(如果报443失败,可稍后再试)

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

via wget

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

手动安装方式:

clone

# 下载 oh-my-zsh 源码
git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh

# 并且把 .zshrc 配置文件拷贝到根目录下
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

# 让 .zshrc 配置文件生效
source ~/.zshrc

zip
在GitHub下载oh-my-zsh,解压后移动到oh-my-zsh到根目录

cd ~/Downloads
mv ohmyzsh-master ~/.oh-my-zsh
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
source ~/.zshrc

更换主题

oh-my-zsh 提供了150多个主题,这是主题样式截图

# 查看所有配色方案
ls ~/.oh-my-zsh/themes
3den.zsh-theme                 kardan.zsh-theme
Soliah.zsh-theme               kennethreitz.zsh-theme
adben.zsh-theme                kiwi.zsh-theme
af-magic.zsh-theme             kolo.zsh-theme
afowler.zsh-theme              kphoen.zsh-theme
agnoster.zsh-theme             lambda.zsh-theme
alanpeabody.zsh-theme          linuxonly.zsh-theme
amuse.zsh-theme                lukerandall.zsh-theme
apple.zsh-theme                macovsky-ruby.zsh-theme
arrow.zsh-theme                macovsky.zsh-theme
aussiegeek.zsh-theme           maran.zsh-theme
avit.zsh-theme                 mgutz.zsh-theme
awesomepanda.zsh-theme         mh.zsh-theme
bira.zsh-theme                 michelebologna.zsh-theme
blinks.zsh-theme               mikeh.zsh-theme
bureau.zsh-theme               miloshadzic.zsh-theme
candy-kingdom.zsh-theme        minimal.zsh-theme
candy.zsh-theme                mira.zsh-theme
clean.zsh-theme                mlh.zsh-theme
cloud.zsh-theme                mortalscumbag.zsh-theme
crcandy.zsh-theme              mrtazz.zsh-theme
crunch.zsh-theme               murilasso.zsh-theme
cypher.zsh-theme               muse.zsh-theme
dallas.zsh-theme               nanotech.zsh-theme
darkblood.zsh-theme            nebirhos.zsh-theme
daveverwer.zsh-theme           nicoulaj.zsh-theme
# If you come from bash you might have to change your $PATH.
dieter.zsh-theme               norm.zsh-theme
# If you come from bash you might have to change your $PATH.
dogenpunk.zsh-theme            obraun.zsh-theme
dpoggi.zsh-theme               peepcode.zsh-theme
dst.zsh-theme                  philips.zsh-theme
dstufft.zsh-theme              pmcgee.zsh-theme
duellj.zsh-theme               pygmalion-virtualenv.zsh-theme
eastwood.zsh-theme             pygmalion.zsh-theme
edvardm.zsh-theme              random.zsh-theme
emotty.zsh-theme               re5et.zsh-theme
essembeh.zsh-theme             refined.zsh-theme
evan.zsh-theme                 rgm.zsh-theme
fino-time.zsh-theme            risto.zsh-theme
fino.zsh-theme                 rixius.zsh-theme
fishy.zsh-theme                rkj-repos.zsh-theme
flazz.zsh-theme                rkj.zsh-theme
fletcherm.zsh-theme            robbyrussell.zsh-theme
fox.zsh-theme                  sammy.zsh-theme
frisk.zsh-theme                simonoff.zsh-theme
frontcube.zsh-theme            simple.zsh-theme
funky.zsh-theme                skaro.zsh-theme
fwalch.zsh-theme               smt.zsh-theme
gallifrey.zsh-theme            sonicradish.zsh-theme
# If you come from bash you might have to change your $PATH.
gallois.zsh-theme              sorin.zsh-theme
garyblessington.zsh-theme      sporty_256.zsh-theme
gentoo.zsh-theme               steeef.zsh-theme
geoffgarside.zsh-theme         strug.zsh-theme
gianu.zsh-theme                sunaku.zsh-theme
gnzh.zsh-theme                 sunrise.zsh-theme
gozilla.zsh-theme              superjarin.zsh-theme
half-life.zsh-theme            suvash.zsh-theme
humza.zsh-theme                takashiyoshida.zsh-theme
imajes.zsh-theme               terminalparty.zsh-theme
intheloop.zsh-theme            theunraveler.zsh-theme
itchy.zsh-theme                tjkirch.zsh-theme
jaischeema.zsh-theme           tjkirch_mod.zsh-theme
jbergantine.zsh-theme          tonotdo.zsh-theme
jispwoso.zsh-theme             trapd00r.zsh-theme
jnrowe.zsh-theme               wedisagree.zsh-theme
jonathan.zsh-theme             wezm+.zsh-theme
josh.zsh-theme                 wezm.zsh-theme
jreese.zsh-theme               wuffers.zsh-theme
jtriley.zsh-theme              xiong-chiamiov-plus.zsh-theme
juanghurtado.zsh-theme         xiong-chiamiov.zsh-theme
junkfood.zsh-theme             ys.zsh-theme
kafeitu.zsh-theme              zhann.zsh-theme

.zshrc是oh-my-zsh的配置文件,在用户的根目录下~/.zshrc,我使用的是agnoster,

# 编辑.zshrc
vim ~/.zshrc

# 设置主题
ZSH_THEME="agnoster"

# 更新配置文件
source ~/.zshrc

注意:许多主题需要安装Powerline字体才能正确渲染,因为电脑上的字体一般不支持这些图标,导致无法加载出现乱码

安装Powerline字体

cd到要下载的目录进行clone,或者下载zip

# clone如果报443错误,稍后重试
git clone https://github.com/powerline/fonts.git --depth=1

# cd到下载好的fonts文件目录,安装字体
./install.sh

设置iTerm2偏好

新建风格

菜单栏 -> Preferences -> Profiles -> +号添加一个新字体风格 -> Other Actions -> Set as Default

字体设置

菜单栏 -> Preferences -> Profiles -> +号添加一个新字体风格 -> Other Actions -> 分栏Text

Font:选择Meslo字体

窗口设置

菜单栏 -> Preferences -> Profiles -> 分栏Window

Transparency:10 透明度设置
Blur:4 模糊度设置

Enabled:开启背景图片
Mode:Scale to Fill 填充
Blending:30 背景纯色与背景图片的混合度

Rows:20 新窗口显示的行数(高度)
style:新窗口的位置,我习惯设置Full-Width Top of Screen

状态栏设置

菜单栏 -> Preferences -> Profiles -> 分栏Session

Status bar enabled:拖动你喜欢的Item

快捷键设置

菜单栏 -> Preferences -> Keys -> 分栏Hotkey

Show timestampas:设置为Always

  • Show/hide all windows Hotkey Window
    Hotkey: 点击后显示Recording进行设置快捷键

插件

oh-my-zsh官方插件一览
oh-my-zsh插件有很多,根据自己的需要进行配置,引入的插件过多,会影响启动的速度

插件配置

# 进入oh-my-zsh的配置文件~/.zshrc
~/.zshrc

# 找到插件配置
plugins=(git)

git

由上我们可以看出,oh-my-zsh自带git插件,并且默认开启了,这可以简化git命令的使用

设置前的git命令 设置后的git命令
git add --all gaa
git branch -D gbD
git commit -a -m gcam
git checkout -b gcb
git checkout master gcm

查看所有git插件的配置:

vim ~/.oh-my-zsh/plugins/git/git.plugin.zsh

autojump

设置自动跳转对应目录可以通过别名alias进行访问文件夹

# 进入配置文件~/.zshrc
vim ~/.zshrc

# 输入 alias 别名=文件路径
alias FolderName1='~/Code/GitLab/FolderName1'
alias FolderName2='~/Code/GitLab/FolderName2'

这样虽然可以实现文件夹的直接访问,但需要我们自行维护,尤其是当文件路径变更后还要再次去修改,比较麻烦。而autojump插件会自动记录我们所有的访问记录,通过命令j就可执行auto-jump

安装 auto-jump

# Homebrew下载
brew install autojump

配置 auto-jump

# 进入配置文件~/.zshrc
vim ~/.zshrc

# 加入一行代码,不限位置,建议plugins下面添加并注释
# Install autojump
[[ -s $(brew --prefix)/etc/profile.d/autojump.sh ]] && . $(brew --prefix)/etc/profile.d/autojump.sh

# 退出编辑
shift+: -> wq -> 回车

# 更新配置文件
source ~/.zshrc

使用 auto-jump 命令

# FolderName是历史访问过的文件夹名
j FolderName

zsh-autosuggestions

输入命令时会显示自动补全提示

安装 zsh-autosuggestions

# Homebrew下载
brew install zsh-autosuggestions

# 进入配置文件~/.zshrc
vim ~/.zshrc

# 引入zsh-autosuggestions
plugins=(
    git 
    zsh-autosuggestions
)

brew是Homebrew的语法,下载插件路径可通过brew list查看所有Homebrew下载的插件,后面拼接插件名,可输出地址例如:

brew list zsh-autosuggestions

输出地址:

/usr/local/Cellar/zsh-autosuggestions/0.7.0/share/zsh-autosuggestions/zsh-autosuggestions.zsh

但使用brew下载的zsh-autosuggestions在source ~/.zshrc时会提示,并且插件没有效果

[oh-my-zsh] plugin 'zsh-autosuggestions' not found

通过查看oh-my-zsh对插件的描述:

# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)

官方插件搜索路径

~/.oh-my-zsh/plugins

自定义插件搜索路径

~/.oh-my-zsh/custom/plugins

所以我们需要将zsh-autosuggestions存放在自定义插件

cd ~/.oh-my-zsh/custom/plugins
git clone https://github.com/zsh-users/zsh-autosuggestions
source ~/.zshrc

此时自动补全插件生效了

总结

通过Homebrew下载的插件,不在~/.oh-my-zsh/custom/plugins自定义插件目录,而是在/usr/local/Cellar目录,~/.zshrc中应该可以配置增加搜索路径,待研究,如果解决这一问题就可以统一用Homebrew下载管理插件了

如果没有安装Homebrew,或是因为不可描述的网络原因导致443无法安装的,可以阅读CoaoaPods安装

你可能感兴趣的:(iTerm2 + Oh My Zsh 配置)