WezTerm 配置笔记
WezTerm 配置笔记
我用过 iTerm2、Alacritty、Windows Terminal,最后留下的是 WezTerm。原因很简单:我在三个系统上开发,WezTerm 让我只维护一份配置文件。
WezTerm 跑在 Windows、macOS、Linux 上,用 GPU 渲染,配置文件是 Lua 写的。
配置拆解
按系统选 Shell
检测 target_triple 来决定启动什么:
if wezterm.target_triple == "x86_64-pc-windows-msvc" then
default_prog = { "powershell.exe", "-NoLogo" }
elseif wezterm.target_triple == "x86_64-unknown-linux-gnu" then
default_prog = { "bash", "-l" }
else
default_prog = { "zsh", "-l" }
end
换系统不用改配置。
启动菜单
按平台往菜单里塞可用的 Shell:
local launch_menu = {}
table.insert(launch_menu, {
label = "PowerShell 7",
args = { "pwsh.exe", "-NoLogo" },
})
table.insert(launch_menu, {
label = "Bash",
args = { "bash", "-l" },
})
Ctrl + Shift + T 呼出。
读 SSH config
WezTerm 能直接读 ~/.ssh/config,把里面的 Host 加到启动菜单:
local ssh_config_file = wezterm.home_dir .. "/.ssh/config"
if file_exists(ssh_config_file) then
for line in f:lines() do
if line:find("Host ") == 1 then
local host = line:gsub("Host ", ""):gsub("%s+", "")
table.insert(launch_menu, {
label = "SSH " .. host,
args = { "ssh", host },
})
end
end
end
不用记 IP,菜单里直接点。管十几台服务器的时候这个功能很实用。
字体和外观
config.window_background_opacity = 0.95
config.font = wezterm.font_with_fallback({
"FiraCode Nerd Font",
"JetBrainsMono Nerd Font",
"Microsoft YaHei UI",
"Segoe UI Emoji",
})
config.color_scheme = "Material Darker (base16)"
字体回退设好之后,英文、中文、Emoji 混排不会乱。
标签页标题
只显示当前进程名:
wezterm.on("format-tab-title", function(tab, tabs, panes, cfg, hover, max_width)
local process = basename(pane.foreground_process_name)
return {
{ Text = " " .. process .. " " },
}
end)
标签页显示 zsh、ssh、vim 这种,一眼能看出在跑什么。
快捷键
{ key = "t", mods = "CTRL|SHIFT", action = act.ShowLauncher },
{ key = "p", mods = "CTRL|SHIFT", action = act.ActivateCommandPalette },
{ key = "+", mods = "CTRL", action = act.IncreaseFontSize },
鼠标
config.mouse_bindings = {
{
event = { Down = { streak = 1, button = "Right" } },
mods = "SHIFT",
action = act.PasteFrom("Clipboard"),
},
{
event = { Up = { streak = 1, button = "Right" } },
mods = "NONE",
action = act.CompleteSelection("Clipboard"),
},
}
右键复制,Shift + 右键粘贴。
性能
config.scrollback_lines = 10000000
config.freetype_load_target = "Light"
config.freetype_render_target = "HorizontalLcd"
跑 CI 输出几万行也不卡。
it2dl / it2ul:终端里传文件
WezTerm 支持 iTerm2 Image Protocol。这个协议名字里有 "Image",但其实能传文件。
it2dl 从服务器往本地拉文件,it2ul 往服务器推文件。不用开额外端口,不依赖 scp 权限。
适合临时拉个日志、下个小文件。大文件别用这个。
装 it2dl
服务器上:
wget https://iterm2.com/utilities/it2dl
chmod +x it2dl
mv it2dl /usr/local/bin
之后在 SSH 里跑 it2dl 文件名,文件直接落到本地 Downloads。
WezTerm 的处理方式:文件名冲突自动加数字后缀,用 toast 通知下载状态,不弹窗打断你。
总结
WezTerm 的配置比 iTerm2 复杂,但能做的事情多:
- 一份配置跨三个系统
- GPU 渲染
- Lua 写配置,想怎么改怎么改
- 内置类似 tmux 的多路复用
- 支持图片显示和文件传输