Skip to content

zsh 配置

安装zsh

macOS 系统自带了zsh, 一般不是最新版,如果需要最新版可通过Homebrew来安装(确认安装了Homebrew)

bash
brew install zsh

更改默认shell

bash
brew install zsh

安装oh-my-zsh

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

配置主题

编辑 ~/.zshrc文件, 默认捆绑的主题, 扩展主题

sh
ZSH_THEME="robbyrussell"

oh-my-zsh 插件推荐

输入命令时可提示自动补全

bash
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

~/.zshrc

sh
plugins=( 
    # other plugins...
    zsh-autosuggestions
)

日常用的命令会高亮显示,命令错误显示红色

bash
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

~/.zshrc

sh
plugins=( 
    # other plugins...
    zsh-syntax-highlighting
)

自动扩展缩写

bash
brew install olets/tap/zsh-abbr

~/.zshrc

sh
source /usr/local/share/zsh-abbr/zsh-abbr.zsh

使用

bash
# Add and use an abbreviation
abbr gc="git checkout"
gc[Space] # space expands this to `git checkout `
abbr gcm="git checkout main"
gcm[Enter] # enter expands this to `git checkout main` and then accepts
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

# Abbreviations are immediately available to all current and future sessions
source ~/.zshrc
gc[Space] # expands to `git checkout`

# Add a session-specific abbreviation
abbr -S x="git checkout"
x[Space] # expands to `git checkout `
source ~/.zshrc
x[Space] # but only in the session it was created in

# Erase an abbreviation
abbr -e gc
gc[Space] # no expansion

# Add a global abbreviation
abbr -g g=git
echo global && g[Space] # expands to `echo global && git `

# Rename an abbreviation
abbr -r gcm cm
gcm[Space] # does not expand
cm[Space] # expands to `git checkout main `

# Make the switch from aliases
abbr import-aliases
abbr import-git-aliases

tips

解决zsh复制大量文本时候卡顿

~/.zshrc解开这个注释

sh
# Uncomment the following line if pasting URLs and other text is messed up.
DISABLE_MAGIC_FUNCTIONS="true"