终端与 Shell
默认 Shell
macOS Catalina (10.15) 起默认 Shell 改为 zsh,之前为 bash。
echo $SHELL # 当前 Shell,如 /bin/zsh
chsh -s /bin/zsh # 切换为 zsh
chsh -s /bin/bash # 切换为 bash
zsh 配置文件加载顺序
| 文件 | 加载时机 |
|---|
~/.zshenv | 每次启动(最先,推荐放环境变量) |
~/.zprofile | 登录 Shell |
~/.zshrc | 交互式 Shell(最常用) |
~/.zlogin | 登录 Shell(zprofile 之后) |
~/.zlogout | 退出登录时 |
常用 zsh 配置(~/.zshrc)
# 历史记录
HISTSIZE=10000
SAVEHIST=10000
HISTFILE=~/.zsh_history
setopt HIST_IGNORE_DUPS
setopt SHARE_HISTORY
# 别名
alias ll='ls -lhG'
alias la='ls -lahG'
alias ..='cd ..'
alias ...='cd ../..'
alias grep='grep --color=auto'
# 环境变量
export EDITOR=vim
export LANG=en_US.UTF-8
# PATH(Apple Silicon Homebrew)
export PATH="/opt/homebrew/bin:$PATH"
Oh My Zsh
# 安装
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# ~/.zshrc 配置
ZSH_THEME="robbyrussell"
plugins=(
git
zsh-autosuggestions
zsh-syntax-highlighting
z
)
# 安装第三方插件
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-syntax-highlighting \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
常用命令速查
导航
pwd # 当前目录
cd ~ # 回家目录
cd - # 回上一个目录
ls -lhG # 彩色列表
ls -lahG # 含隐藏文件
tree -L 2 # 目录树(brew install tree)
文件操作
cp -r src/ dst/ # 递归复制
mv old new # 移动/重命名
rm -rf dir/ # 强制删除目录
ln -s target link # 软链接
touch file.txt # 创建空文件
mkdir -p a/b/c # 递归创建目录
查看内容
cat file.txt
less file.txt # 分页查看,q 退出
head -20 file.txt
tail -f app.log # 实时追踪日志
grep -rn "keyword" . # 递归搜索
查找
find . -name "*.log" -mtime -7 # 7天内修改的 log
find . -type f -size +10M # 大于 10MB 的文件
mdfind -name "keyword" # Spotlight 命令行搜索
tmux 多路复用
brew install tmux
tmux # 新建会话
tmux new -s main # 命名会话
tmux ls # 列出会话
tmux attach -t main # 重新接入会话
前缀键 ⌃B + | 功能 |
|---|
c | 新建窗口 |
n / p | 下/上一个窗口 |
% | 垂直分割 |
" | 水平分割 |
o | 切换面板 |
d | 分离会话 |
[ | 进入滚动模式(q 退出) |
环境变量管理
env # 查看所有
echo $PATH
printenv HOME
export MY_VAR=value # 临时(当前 Shell)
# 永久写入 ~/.zshrc
echo 'export MY_VAR=value' >> ~/.zshrc
source ~/.zshrc
相关链接