Terminal v2
- Terminal v2 - Shell (zsh + oh-my-zsh) + Prompt (Starship) + Terminal Emulator (Ghostty) + Multiplexer (tmux)
- 终端工具升级:从 Alacritty/p10k 到 Ghostty/Starship
| 组件 | 选型 |
|---|---|
| Shell | zsh + oh-my-zsh |
| Prompt | Starship |
| Terminal Emulator | Ghostty |
| Multiplexer | tmux |
1. Shell 层:zsh + oh-my-zsh
1.1 安装 zsh 和 oh-my-zsh
1.1.1 安装 zsh
macOS:
- macOS Catalina (10.15) 及更高版本已内置 zsh 并设为默认 shell,无需安装
- 检查当前 shell:
echo $SHELL - 如需切换到 zsh:
chsh -s $(which zsh)
Ubuntu/Linux:
# Ubuntu/Debian
sudo apt update
sudo apt install zsh
# 设置 zsh 为默认 shell
chsh -s $(which zsh)注销并重新登录后生效。
1.1.2 安装 oh-my-zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"未科学上网可使用
sh -c "$(curl -fsSL https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh)"1.2 插件配置
zsh-autosuggestions, zsh-syntax-highlighting, zsh-history-substring-search插件安装
git clone https://github.com/zsh-users/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-history-substring-search ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-history-substring-search~/.zshrc中配置插件:
plugins=(
z
# git
zsh-autosuggestions
zsh-syntax-highlighting
zsh-history-substring-search
)
# zsh-history-substring-search 绑定到方向键
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
bindkey "$terminfo[kcuu1]" history-substring-search-up
bindkey "$terminfo[kcud1]" history-substring-search-down
HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=11.3 配置文件:.zshrc 与 .zshenv
理解 zsh 配置文件的加载顺序对于正确配置环境至关重要。
核心区别:
| 文件 | 加载时机 | 典型用途 | 是否适合放 PATH |
|---|---|---|---|
~/.zshenv | 所有 zsh 都会加载(包括非交互式、脚本、SSH) | 环境变量(如 PATH)、export | 是 |
~/.zshrc | 仅交互式 shell 加载(打开终端时) | 别名、提示符、插件、快捷键绑定 | 否 |
一句话总结:
.zshenv:任何 zsh 进程都能继承的环境变量.zshrc:终端交互时才需要的配置
Zsh 配置文件加载顺序:
/etc/zshenv → 系统级环境变量
~/.zshenv → 用户级环境变量(最早、最通用)
↓
[登录 shell]
/etc/zprofile → 系统级登录配置
~/.zprofile → 用户级登录配置
↓
[交互式 shell]
/etc/zshrc → 系统级交互配置
~/.zshrc → 用户级交互配置(alias、插件等)
↓
[退出时]
~/.zlogout → 退出时执行macOS 特殊注意
macOS 的 /etc/zprofile 中会调用 path_helper,这会重新排序 PATH(系统路径被移到前面)。
如果在 .zshenv 中设置 PATH,可能会被打乱顺序。解决方案:
- 方案 A:把 PATH 放在
~/.zprofile而非~/.zshenv - 方案 B:在
~/.zshrc中重新设置 PATH(仅影响交互式 shell)
配置示例:
~/.zshenv 放这些:
export EDITOR=vim
export LANG=en_US.UTF-8
# PATH 设置(注意 macOS 的特殊情况)
export PATH="$HOME/.local/bin:$PATH"~/.zshrc 放这些:
# 别名
alias ll='ls -alh'
alias grep='grep --color=auto'
# oh-my-zsh 配置
plugins=(z zsh-autosuggestions zsh-syntax-highlighting)
source $ZSH/oh-my-zsh.sh
# 提示符、快捷键绑定等交互配置验证加载顺序:
在各配置文件中添加调试输出例如:
echo "loading ~/.zshenv ($$)" # $$ 显示进程 ID然后运行:
zsh -i -l # -i 交互式,-l 登录 shell2. Prompt 层:Starship美化提示符
3. Terminal Emulator 层:Ghostty
关于终端模拟器
主流 GPU 加速终端模拟器对比:
| 特性 | Alacritty | Kitty | Ghostty |
|---|---|---|---|
| 定位 | 极简 + 性能优先 | 功能 + 性能兼顾 | 性能 + 功能 + 原生体验平衡 |
| GPU 加速 | 支持 | 支持 | 支持 |
| Tabs / Splits | 不支持 需借助 tmux | 支持 内建 | 支持 内建 |
| 图片显示 | 不支持 | 支持 | 支持 |
| 配置复杂度 | 低 | 高 | 中 |
选择 Ghostty 的原因:兼顾速度与功能,原生支持 tabs/splits 和图形协议,同时提供接近系统原生应用的 GUI 体验。作为新一代终端模拟器,在保持 Alacritty 级别性能的同时,提供了 Kitty 级别的功能丰富度。
4. 终端复用:tmux
终端复用器(Terminal Multiplexer)允许你在一个终端窗口中管理多个会话、窗口和面板,是提升终端效率的重要工具。
4.1 安装
macOS:
brew install tmuxUbuntu:
Ubuntu 可以直接安装发行版提供的 tmux:
sudo apt update
sudo apt install tmux如需从官方 release 安装最新版(推荐),同时保留系统版本作为回退,请参考:
4.2 插件管理器 tpm
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm4.3 配置文件
配置文件~/.tumx.conf:
set -g pane-scrollbars modal
set -g pane-scrollbars-position right
set -g pane-scrollbars-style fg=colour248,bg=colour236
set -g pane-border-status bottom
set -g pane-border-format " #{pane_index} #{pane_current_command}"
set -g pane-border-indicators arrows
set -g mouse on
set -g focus-follows-mouse off
# set -g default-terminal "xterm-256color"
set -g default-terminal "tmux-256color"
set -g display-panes-time 3000 # PREFIX-Q 显示编号的驻留时长,单位 ms
set -g renumber-windows on # 关掉某个窗口后,编号重排
set -g copy-mode-line-numbers absolute
set -g cursor-style blinking-bar
# https://draculatheme.com/tmux
set -g @plugin 'dracula/tmux'
set -g @dracula-plugins "custom:mymacos.sh"
set -g @dracula-show-powerline true
set -g @dracula-refresh-rate 120
set -g @dracula-show-left-icon session
set -g @dracula-border-contrast true
set -g @dracula-left-icon-padding 0
set -g @dracula-show-empty-plugins false
#set -g @dracula-show-left-sep
#set -g @dracula-show-right-sep
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
run '~/.tmux/plugins/tpm/tpm'上面的配置文件对tmux做了基本的配置,并将使用tpm安装了主题插件dracula/tmux。
使用下面的命令安装插件。
tmux new -t mac
tmux source ~/.tmux.conf按tmux前缀键control+b,再按Shift + I即可自动安装配置文件中配置的插件。
custom:mymacos.sh
custom:mymacos.sh是dracula/tmux主题的一个自定义插件,可以用来在tmux状态栏中显示自定义信息。
将下面的脚本内容保存到~/.tmux/plugins/dracula/custom目录下,并命名为mymacos.sh,并赋予可执行权限chmod u+x mymacos.sh。
mymacos.sh
#!/bin/bash
get_memory_info() {
local mem_output="$1"
local page_size active_pages wired_pages
page_size=$(echo "$mem_output" | grep "page size of" | sed 's/.*page size of \([0-9]*\).*/\1/')
active_pages=$(echo "$mem_output" | grep "Pages active:" | awk '{print $3}')
wired_pages=$(echo "$mem_output" | grep "Pages wired down:" | awk '{print $4}')
if [[ -n "$page_size" && -n "$active_pages" && -n "$wired_pages" ]]; then
awk "BEGIN {
used = ($active_pages + $wired_pages) * $page_size / 1024 / 1024 / 1024
printf(\"%.1f\", used)
}"
else
echo "N/A"
fi
}
get_memory_pressure_icon() {
local mem_output="$1"
local free_percentage swapouts icon
free_percentage=$(echo "$mem_output" | grep "System-wide memory free percentage:" | awk '{print $5}' | tr -d '%')
if [[ -n "$free_percentage" ]]; then
if (( free_percentage >= 50 )); then
icon=""
elif (( free_percentage >= 20 )); then
icon=""
else
icon=""
fi
else
swapouts=$(echo "$mem_output" | grep "Swapouts:" | awk '{print $2}')
if [[ -n "$swapouts" ]]; then
if (( swapouts < 1000 )); then
icon=""
elif (( swapouts < 10000 )); then
icon=""
else
icon=""
fi
else
icon=""
fi
fi
echo "$icon"
}
get_cpu_usage() {
local cpu_line user idle
cpu_line=$(top -l 1 | grep "CPU usage")
user=$(echo "$cpu_line" | awk -F'[:,]' '{print $2}' | tr -d ' user')
idle=$(echo "$cpu_line" | awk -F'[:,]' '{print $4}' | tr -d ' idle')
echo "$user $idle"
}
get_load_avg() {
sysctl -n vm.loadavg | sed 's/[{}]//g' | awk '{printf "%.2f %.2f %.2f\n", $1, $2, $3}'
}
main() {
local mem_output mem_used mem_icon cpu usage load
mem_output=$(memory_pressure)
mem_used=$(get_memory_info "$mem_output")
mem_pressure_icon=$(get_memory_pressure_icon "$mem_output")
cpu_usage=$(get_cpu_usage)
load=$(get_load_avg)
echo "${mem_pressure_icon} ${mem_used}G $cpu_usage $load"
}
main注意:ssh连接旧版终端
如果在tmux下SSH到较旧版本的Linux(如CentOS7)时报错:
'tmux-256color': unknown terminal type.可以通过以下方式临时解决,覆盖~/.tmux.conf:
export TERM=xterm-256color这将把终端类型设置为 xterm-256color,以避免兼容性问题。
4.4 tmux 快捷键
| 快捷键 | 功能 |
|---|---|
Ctrl + b, space | 循环切换window内预设布局, 多按几次 |
Ctrl + b, { | 将当前pane与左侧pane交换位置 |
Ctrl + b, } | 将当前pane与右侧pane交换位置 |
Ctrl + b c | 创建一个window |
Ctl + b, , | 重命名window |
Ctl + b :join-pane -t :1 | 将当前 pane 移动到目标window 1| |
4.5 远端终端 terminfo:tmux-256color
当在 tmux 里 SSH 到远端机器时,若远端缺少 tmux-256color 的 terminfo,会报:
'tmux-256color': unknown terminal type原因是 tmux 会使用 TERM=tmux-256color,但远端 terminfo 数据库里没有这个条目。正确做法不是长期降级到 xterm-256color,而是把 tmux-256color 安装到远端。
4.5.1 先恢复当前会话(临时)
export TERM=xterm-256color
clear这只是临时恢复可用性,当前会话结束后仍建议做正式安装。
4.5.2 从本机推送 tmux-256color 到远端(正式)
infocmp -x tmux-256color | ssh remote-host 'sudo tic -x -'infocmp -x tmux-256color:导出本机的 terminfo 描述tic -x -:在远端编译并写入 terminfo 数据库
4.5.3 在远端验证
infocmp tmux-256color >/dev/null && echo OK输出 OK 表示远端已识别 tmux-256color。
5. 编辑器主题:vim 高亮
在终端中使用 vim 编辑器时,配置合适的主题可以提供更好的代码阅读体验。
使用 hardhackerlabs 的 vim 主题:
git clone https://github.com/hardhackerlabs/theme-vim.git
cp -r theme-vim/colors ~/.vim/
cp -r theme-vim/autoload ~/.vim/配置 vi ~/.vimrc:
syntax enable
syntax on
set t_Co=256
" let g:hardhacker_darker = 1
colorscheme hardhacker
" 解决插入模式下delete/backspce键失效问题
set backspace=26. 终端工具:bat
bat 是 cat 命令的现代替代品,提供语法高亮、Git 集成和自动分页功能。
主要特性:
- 语法高亮:支持多种编程语言和配置文件格式
- Git 集成:显示文件修改状态(添加/删除/修改的行)
- 自动分页:内容过长时自动调用 pager
- 行号显示:默认显示行号,便于阅读
6.1 安装 bat
macOS:
brew install batUbuntu/Debian:
# Ubuntu 19.10+ / Debian 11+
sudo apt install bat
# 注意:在某些版本中,二进制文件名为 batcat 而非 bat
# 可以创建别名解决:
mkdir -p ~/.local/bin
ln -s /usr/bin/batcat ~/.local/bin/bat6.2 基本用法
# 查看文件(带语法高亮)
bat file.py
# 显示多个文件
bat file1.js file2.js
# 显示行号范围
bat --line-range 10:20 file.py
# 纯文本输出(无装饰)
bat --plain file.txt