目录

Zsh 配置使用指南

Where there is a shell, there is a way.

Z shell(Zsh)是一款可用作交互式(英语:interactive)登录的shell及脚本编写的命令解释器。Zsh对Bourne shell做出了大量改进,同时加入了Bash、ksh及tcsh的某些功能。

自2019年起,macOS的预设Shell已从Bash改为Zsh。

zsh

Zsh is a shell designed for interactive use, although it is also a powerful scripting language.

全局配置 /etc/zshrc 当前用户配置文件 ~/.zshrc

1
zsh --version

Xcode Command Line Tools

从 App Store 上下载的 Xcode,默认是不会安装 Command Line Tools 的,Command Line Tools 是在 Xcode 中的一款命令行开发工具,gcc/g++编译器、make、git 等,是很多其他软件的基础。 xcode-select --install 安装完成后,在终端中执行 gcc -v 命令查看是否安装成功。

Homebrew 会自动安装 Command Line Tools。

1
2
3
sudo xcode-select -switch /Library/Developer/CommandLineTools
sudo xcode-select --switch /Applications/Xcode.app
# -r, --reset                 reset to the default command line tools path

Homebrew

The Missing Package Manager for macOS

Homebrew macOS 包管理工具,官方称之为缺失的软件包的管理器

与 Homebrew 搭配最常用的是 Homebrew-Cask ,它有点类似于一个开源的 App Store。

Homebrew只能用于安装命令行软件,不能用于安装带图形界面的软件。Homebrew-Cask是Homebrew的 一个扩展,可以用于安装带图形界面软件

1
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

此脚本将 Homebrew 安装到其首选前缀(/usr/local 对于 macOS Intel、/opt/homebrew 对于 Apple Silicon 和 /home/linuxbrew/.linuxbrew 对于 Linux),因此您使用 brew install 不需要 sudo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
brew --help
brew search TEXT|/REGEX/
brew info [FORMULA|CASK...]
brew install FORMULA|CASK...
brew update
brew upgrade [FORMULA|CASK...]
brew uninstall FORMULA|CASK...
brew list
# 安装 chrome 浏览器
brew install google-chrome

# 保存到 ~/.zshrc
alias bs='brew search'
alias bi='brew install -s'
alias bif='brew info'
alias bui='brew uninstall'
alias bug='brew upgrade'
alias bud='brew update'

brew services

管理后台服务,可以简化 lauchctl 的操作。

macOS 使用 launchctl 命令加载开机自动运行的服务。

1
2
3
4
5
6
7
8
brew services --help
brew services list  # 查看使用 brew 安装的服务列表
brew services info (formula | --all) # 查看服务状态 
brew services run (formula | --all)  # 启动服务(仅启动不注册)
brew services start (formula | --all)  # 启动服务,并注册
brew services stop (formula | --all)   # 停止服务,并取消注册
brew services restart (formula | --all)  # 重启服务,并注册
brew services cleanup  # 清除已卸载应用的无用的配置

安装 Nerd Fonts

Nerd Fonts是一个使用大量字形(glyphs)(图标 icons)修补开发人员目标字体的项目。特别是从流行的 “标志性字体” 中添加大量额外的字形,例如 Font Awesome、Devions、Octicons 等。 如果不安装图形字体,终端中的图标就无法显示正确的字形。

1
2
3
4
brew tap homebrew/cask-fonts
bs font
# 安装微软 Cascadia Code 的 nerd font 版本
bi font-caskaydia-cove-nerd-font

iTerm 2

iTerm2 is a terminal emulator for macOs that does amazing things

1
2
# 以下 brew install alias bi
bi iterm2
  • 打开iTerm2 右上角菜单:iTerm2Make iTerm2 Default Term
  • 配置全局热键显示/隐藏 iTerm2 iTerm2 - Preferences... - Keys - 勾选 Hotkey 下的 Show/hide iTerm2 with a system-wide hotkey ,将热键设置 option + Space
  • 修改配色方案 iTerm2 - Preferences... - Profiles - Colors - Color Presets... - Solarized Dark
  • 应用字体到 iTerm2,iTerm2 > Preferences > Profiles > Text > Font 选择 Caskaydia Cove Nerd Font Complete Mono
1
2
# Disable the “Last login” Message on new Terminal Session
touch ~/.hushlogin

Oh My Zsh

Oh My Zsh Unleash your terminal like never before.

1
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
1
2
3
4
5
6
7
8
9
alias # List all aliases
zsh_stats # Get a list of the top 20 commands and how many times they have been run.
# 3点表示回退2层 4点回退3层
...

# 输入完按tab自动补全 cd /usr/local/bin 
cd /u/l/b 
# 输入 d 回车即可列出你在这个会话里访问的目录列表,然后输入序号,回车即可跳转
d
Alias Command
md mkdir -p
rd rmdir
.. cd ..
cd ../..
…. cd ../../..
/ cd /
d dirs -v (lists last visited directories)
cd +n Switch to directory number n
- cd to last visited directory
1 cd -1
2 cd -2
3 cd -3 (1~9)
port-listen lsof -i -P
x extract

插件

  • ~/.oh-my-zsh/plugins 官方插件目录
  • ~/.oh-my-zsh/custom/plugins 第三方插件目录 $ZSH_CUSTOM/plugins
plugins=(vi-mode git z extract pyenv)

vi-mode

vi 模式 快速移动命令行光标

ESC to enter Normal mode.

  • History
    • ctrl-p : Previous command in history
    • ctrl-n : Next command in history
  • Movement
    • $ : To the end of the line
    • ^ : To the first non-blank character of the line
    • 0 : To the first character of the line
    • w : [count] words forward
    • W : [count] WORDS forward
    • e : Forward to the end of word [count] inclusive
    • E : Forward to the end of WORD [count] inclusive
    • b : [count] words backward
    • B : [count] WORDS backward
  • Insertion
    • i : Insert text before the cursor
    • I : Insert text before the first character in the line
    • a : Append text after the cursor
    • A : Append text at the end of the line
  • Delete and Insert
    • ctrl-h : While in Insert mode: delete character before the cursor
    • ctrl-w : While in Insert mode: delete word before the cursor
    • dd : Delete line
    • D : Delete characters under the cursor until the end of the line

git

插件主要是一些 git 命令的 aliases

Alias Command
ga git add
gb git branch
gba git branch -a
gbd git branch -d
gcam git commit -a -m
gcb git checkout -b
gcl git clone –recurse-submodules
gcmsg git commit -m
gco git checkout
gcp git cherry-pick
gd git diff
glol git log –graph –pretty=’%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset'
glols git log –graph –pretty=’%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset’ –stat
glola git log –graph –pretty=’%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset’ –all

z

快速的切换文件路径。跟踪你最常访问的目录,并允许你用很少的按键来访问它们。

访问过 ~/.oh-my-zsh/plugins

1
z plug # 甚至 `z p`就足够了

extract

这个插件定义了一个函数,叫做extract提取你传递给它的存档文件,它支持各种各样的存档文件类型。

alias 是 x 用来解压文件。当然 tar -tab 键,zsh 会列出参数的含义

支持 7z、apk、gz、ipa、jar、rar、tar、tar.gz、tgz、zip 等常见归档及压缩格式。

zsh 插件

以下这三个个插件可以使用 Homebrew 安装,这里以 Oh My Zsh 插件的形式安装。

1
2
3
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

如果觉得键盘按键 → (苹果键盘在最右下角很便利)不方便,可自定义补全快捷键: bindkey ',' autosuggest-accept

1
vim ~/.zshrc
plugins=( 
    # other plugins...
    zsh-autosuggestions 
    zsh-syntax-highlighting
)

fpath+=${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions/src
# 追加 FPATH 在这条 source 命令前
source "$ZSH/oh-my-zsh.sh"
1
2
# 使配置生效
source ~/.zshrc

Powerlevel10k

Powerlevel10k is a theme for Zsh. It emphasizes speed, flexibility and out-of-the-box experience.

1
2
3
4
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
# Set ZSH_THEME="powerlevel10k/powerlevel10k" in ~/.zshrc.
# 交互式配置主题
p10k configure

bat

Bat 命令是 cat 命令的克隆,具有许多吸引人且有用的功能。 其中一些功能是语法高亮、自动分页、git 集成等,也增强了程序员的体验。 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
brew install bat
bat --list-languages
# 创建新文件
bat > filename
# 将不同文件的内容附加到单个文件上
bat file1 file2 > singlefile
bat --line-range 2:4 /etc/hosts
# 主题相关
bat --list-themes
bat --theme=ansi filename
export BAT_THEME="ansi"

shell 中的色彩

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
for code ({000..255}) print -P -- "$code: %F{$code}Winter is coming"
# 字颜色
for code ({30..37}) echo -e "$code \033[0;$code;5m FBI WARNING \033[0m"
# 背景颜色
for code ({40..47}) echo -e "$code \033[$code;5m FBI WARNING \033[0m"
# 字加亮颜色
for code ({90..97}) echo -e "$code \033[$code;5m FBI WARNING \033[0m"
# 背景加亮颜色
for code ({100..107}) echo -e "$code \033[$code;5m FBI WARNING \033[0m"
# ANSI控制码的说明 
\33[0m 关闭所有属性 
\33[1m 设置高亮度 
\33[4m 下划线 
\33[5m 闪烁 
\33[7m 反显 
\33[8m 消隐 
\33[30m -- \33[37m 设置前景色 
\33[40m -- \33[47m 设置背景色 
\33[nA 光标上移n行 
\33[nB 光标下移n行 
\33[nC 光标右移n行 
\33[nD 光标左移n行 
\33[y;xH设置光标位置 
\33[2J 清屏 
\33[K 清除从光标到行尾的内容 
\33[s 保存光标位置 
\33[u 恢复光标位置 
\33[?25l 隐藏光标 
\33[?25h 显示光标

SpaceVim

SpaceVim 是一个社区驱动的模块化 Vim IDE,以模块的方式组织管理插件以及相关配置, 为不同的语言开发量身定制了相关的开发模块,该模块提供代码自动补全, 语法检查、格式化、调试、REPL 等特性。

1
2
curl -sLf https://spacevim.org/cn/install.sh | bash
# vim ~/.SpaceVim.d/init.toml

Quick Look plugin

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 代码 自动安装 highlight
brew cask install qlcolorcode
brew cask install qlmarkdown
brew cask install qlimagesize
brew cask install betterzip
# 重启 QuickLook
qlmanage -r
# 需要注意的是如果 macOS 系统版本是 10.15 Catalina 及以上,安装完插件后需要额外执行一下
xattr -r ~/Library/QuickLook
xattr -d -r com.apple.quarantine ~/Library/QuickLook
qlmanage -r
# 以解决运行不了 QuickLook 插件的问题