CLI 生态系统经历了一场静默革命。用 Rust 和 Go 编写的工具取代了有数十年历史的 Unix 二进制文件,几乎不牺牲速度地添加了颜色、语法高亮、模糊搜索和 Git 感知。这些是我日常使用的工具。
目录
Shell: Zsh + Starship
Starship 无疑是以最小努力最大改善体验的 prompt。它适用于任何 shell,速度极快(用 Rust 编写),并显示相关上下文:Git 分支、Node/Python/Rust 版本、上一个命令的状态。
# 极简但信息丰富
format = """
$directory\
$git_branch\
$git_status\
$nodejs\
$rust\
$python\
$cmd_duration\
$line_break\
$character"""
[git_branch]
symbol = " "
style = "bold purple"
[git_status]
conflicted = "⚔️ "
ahead = "⇡${count}"
behind = "⇣${count}"
modified = "✎${count}"
untracked = "?${count}"
[cmd_duration]
min_time = 2_000
format = "took [$duration](bold yellow)"~/.config/starship.toml
经典工具的替代品
ls → eza(前 exa)
eza --tree --level=2 --icons --git # 带图标和 Git 状态的树
eza -la --sort=modified # 长列表,按日期排序
find → fd
# find: 冗长且不 ergonomics
find . -name "*.ts" -not -path "*/node_modules/*"
# fd: 更直观,默认尊重 .gitignore
fd -e ts # 项目中所有的 .ts
fd -e ts --exec bat {} # 用 bat 打开每个结果
grep → ripgrep (rg)
# 经典 grep
grep -r "useEffect" src/ --include="*.tsx"
# rg: 快 5-10 倍,默认尊重 .gitignore
rg "useEffect" --type ts
rg "TODO|FIXME|HACK" --type ts --stats
rg "deprecated" -l # 只显示文件名
cat → bat
bat 是带语法高亮、行号、分页和集成 Git diff 的 cat:
bat src/components/Header.astro # 带颜色和行号
bat --diff archivo.ts # 内联显示 Git 变更
cd → zoxide
学习你频繁访问的目录,让你用很少的字母跳转:
z astro # 跳转到 ~/proyectos/mi-blog-astro 如果这是你访问最多的
z blog src # 多重匹配
zi # 使用 fzf 的交互模式
多路复用器:配置现代化的 tmux
# 更舒适的前缀键
set -g prefix C-a
unbind C-b
# 用直观的按键分割面板
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
# Alt+方向键导航(无需前缀)
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D
# 启用鼠标
set -g mouse on
# 256 色
set -g default-terminal "tmux-256color"~/.tmux.conf
模糊查找器:fzf —— 一切的力量倍增器
fzf 将任何列表转换为交互式搜索器。只需在任何命令后加 | fzf。
# 搜索命令历史
CTRL+R 集成 fzf
# 带 preview 的 branch checkout
git branch | fzf --preview 'git log --oneline {}' | xargs git checkout
# 终止进程
ps aux | fzf --multi | awk '{print $2}' | xargs kill
# 搜索并打开文件
fd -e ts | fzf --preview 'bat --color=always {}' | xargs nvim
现代 Git:lazygit
一个让仓库中发生的事情一目了然的 Git TUI(终端 UI):
lazygit # 打开界面
突出特点:
- 按文件和按行查看 diffs
- 选择性 stage(单行,不仅限于文件)
- 可视化解决冲突
- 拖放交互式 rebase
我的优化版基础 .zshrc
# 懒加载实现快速启动
export PATH="$HOME/.cargo/bin:$HOME/.local/bin:$PATH"
# 现代别名
alias ls='eza --icons'
alias ll='eza -la --icons --git'
alias tree='eza --tree --icons'
alias cat='bat'
alias find='fd'
alias grep='rg'
alias lg='lazygit'
# fzf 集成
source <(fzf --zsh)
# zoxide
eval "$(zoxide init zsh)"
# starship
eval "$(starship init zsh)"~/.zshrc
终端生产力方面的时间最佳投资不是学习新工具——而是掌握你已经拥有的工具。但当一个现代工具以更好的 DX 做同样的事情快 5 倍时,这个改变在第一周就能回本。