WezTerm 跨系统终端 和 Lua 配置

15,361 阅读5分钟

前置

  • 如果需要长期编程,或者打算长期学习维护自己的代码,那么终端可能是不二选择,配合 vim 系列可能帮助长期编码过程中受益,缺点是前期的学习痛苦,还有创作的痛苦不能平衡,但是长期是值得的。
  • 希望编程能带来的是 “心流或者福流的心灵状态”,能从中找到编程的快乐,而不是只感觉到剩余价值。
  • 前端不只有 VSCode, 也可以拥抱终端,靠近后端和系统编程
  • 了解 Lua 基本语法,了解终端
  • 想自由的在三大操作系统中(包括 wsl)自由穿梭
  • 有使用 wsl 的经验

简单介绍

WezTerm 是一个 GPU 加速的跨平台终端仿真器和多路复用器,由 @wez 编写并在 Rust 中实现

特点

  • 垮平台支持
  • 远程支持 ssh/wsl
  • 多标签
  • 可配置能力强
  • 亲 vim 系列...

不同平台安装

  • 从命令行安装,简单集中示例有其他的还需要自己探索
# windows winget
winget install wez.wezterm
# brew 为例 linux/MacOS
winget install wez.wezterm
# FreeBSD
pkg install wezterm
  • 从下载地址安装:

WezTerm releases: GitHub 下载地址,选择自己需要的下载安装。

以 Ubuntu 的 deb 类型文件为例:

先 cd 到下载文件安装目录,然后使用 dpkg 命令安装:

cd your_deb_dir
sudo dpkg -i ./your_deb_file.deb

启动

  • wezeterm 命令启动
  • 集成到终端中使用
  • 从 wezeterm 图标 icon 启动

配置

WezTerm 使用 lua 进行配置,如果对 Lua 不熟悉,还没有入门可以看看这篇 前端也学一下 Lua 呀 或者 Lua 官方_网站

配置文件位置

# 方式1: 官方推荐
$HOME/.wezterm.lua

# 方式2
$HOME/.config/wezterm/wezterm.lua

如果复制很复杂,需要将内容提取到单独的文件中时,采用 方式2 进行配置

高度灵活的 wezeterm.lua 配置文件

返回一个 lua 语言数据类型的表:

return {}
  • wezterm 提供了默认配置,可以直接访问 wezterm 对象
local wezterm = require 'wezterm' 
return { 
    font = wezterm.font 'JetBrains Mono', 
    color_scheme = 'Batman', 
}

配置文件设置启动程序

在 Linux/mMacOS 中安装 zsh, 配置默认打开程序是 zsh

return {
    default_prog = { 'zsh' }, 
}

在 windows 中可以设置 powershell/cmd

return {
    default_prog = { 'porwershell' }, 
}

支持配置 The Launcher Menu

image.png

相当于下拉菜单,设置 Launcher 一般配合快捷键进行配置

return {
    default_prog = { 'porwershell' }, 
    keys = {
        { key = 'z', mods = 'ALT', action = wezterm.action.ShowLauncher },
  },
}

使用 alt + z 来切换 Launcher Menu。 wezterm 提供了环境变量 wezterm.target_triple 来判断在什么环境中:

环境变量值系统
x86_64-pc-windows-msvc Windows
x86_64-apple-darwin macOS
x86_64-unknown-linux-gnu Linux

在 windows 下,可以下如下的 Launcher Menu 来进行

local launch_menu = {}

if wezterm.target_triple == "x86_64-pc-windows-msvc" then
  ssh_cmd = { "powershell.exe" }

  table.insert(
    launch_menu,
    {
      label = "PowerShell",
      args = { "powershell.exe", "-NoLogo" }
    }
  )

  table.insert(
    launch_menu,
    {
      label = "Bash",
      args = { "bash.exe" }
    }
  )

  table.insert(
    launch_menu,
    {
      label = "CMD",
      args = { "cmd.exe" }
    }
  )
end

// 注意返回值
return {
     launch_menu  = launch_menu,
}

如果使用 wsl,且需要指定 wsl 开始的目录

local launch_menu = {}

if wezterm.target_triple == "x86_64-pc-windows-msvc" then
  table.insert(
    launch_menu,
    {
      label = "Mg::Rust::Learn", -- 学习 rust 有一个单独的目录,可以从这个吗lauch menu 单独进入
      args = {  "wsl.exe", "--cd", "/home/<user>/<dir1?" }
    }
  )

  table.insert(
    launch_menu,
    {
      label = "Mg::Project::GitHub-Coding", -- 从github下载单独的有一个文件夹,每次访问项目从这个 launch menu 进入
      args = { "wsl.exe", "--cd", "/home/<user>/<dir2>"} -- 此处写绝对路径才有效
    }
  )
end

// 注意返回值
return {
     launch_menu  = launch_menu,
}

指定字体

使用 wezterm.font_with_fallback 表示带有回退方式的字体:

local wezterm = require 'wezterm' 
return { font = wezterm.font_with_fallback { 'Fira Code', 'DengXian', }, }

绑定快捷键

其实之间有绑定 launch menu 实践的同时已经有过快捷键绑定了, 下面以 wezterm 上下分屏为例:

{ 
    key = '"', 
    mods = 'CTRL|SHIFT|ALT', 
    action = wezterm.action.SplitVertical { domain = 'CurrentPaneDomain' }, 
},

同时 CTRL|SHIFT|ALT + " 得到分屏效果(其实 vim/nvim/tmux 效果差不多):

image.png

键值 Tab

如果熟悉 vim/neovim 的 <leader> + 功能键 的组合模式特别熟悉,wezterm 也实现了类似的功能。让使用起来更加的方便,对这个终端有强烈的熟悉感。

配色方案

  • WezTerm 提供 700 多种配色方案

配色配置文件

return { color_scheme = 'Batman', }

默认配色:wezterm.color.get_default_colors()

自定义配色

return { 
    colors = {
         -- your-color-scheme
    }
}

背景

背景透明度

return { window_background_opacity = 1.0, }

背景图片

return { window_background_image = '/path/to/wallpaper.jpg', window_background_image_hsb = {
    -- your_window_background_image_hsb code
}}

背景渐变色

return { window_background_gradient = {
    -- your gradient code
}}

tab 配置

return { 
    colors = {
         tab_bar = { 
             inactive_tab_edge = '#575757', 
         },
    }
}

回滚

WezTerm 提供了一个可搜索的回滚缓冲区,具有可配置的最大大小限制,允许您查看不适合物理窗口大小的信息。当内容打印到显示器上时,显示器可能会向上滚动以适应新添加的行。滚动的行将移动到回滚缓冲区中,并且可以通过向上或向下滚动窗口来查看。

模式

与 vim 设计类似也有模式的概念:

  • 快速选择模式 (配置快捷键:CTRL-SHIFT-SPACE)
  • 复制模式(配置快捷键CTRL-SHIFT-XCTRl-SHIFT-C)复制模式与 vim-easymotion 理念相同

如果熟悉 vim, 这些模式能够很好的帮助进行更多的终端操作。

识别超链接字符串

wezterm 同时支持:隐式和显式超链接。

image.png

在鼠标移动到超链接地址之后鼠标会变成可点击状态,配置 ctrl 键即可打开浏览器跳转

ssh 登录支持

  • 以 windows wm 虚拟机为例,指定如下的 lua 配置
return {
    ssh_domains = {
          name = 'vm:name', -- 指定一个名字
          remote_address = '192.168.161.128',  -- 指定虚拟机地址
          username = 'mg', -- 指定linux用户
        },
  },
}
  • 在 windows 相关终端(prowershell)中执行连接命令:

:::tip::: 按照配置文件中 name 字段进行连接

wezterm connect vm:name

连接完毕之后,就会打开 wezterm 终端,然后输入正确的密码即可与 vm:name 进行连接.

image.png

值得注意:此时通过 添加 + 按钮添加 tab, 打开的也是虚拟机中的终端(如果你也配置了 wsl,可能长得一摸一样)。

类似于 tmux 终端复用工具

wezTerm cli

  • wezterm
  • wezterm-gui

image.png

  • --config 指定配置
  • --config-file 指定配置文件

Lua API模块

模块提供

  • wezterm 模块
  • wezterm.color 模块
  • wezterm.gui 模块
  • wezterm.mux 模块
  • wezterm.procinfo 模块
  • wezterm.time 模块

结构体 struct

  • Config

枚举

  • KeyAssignment
  • CopyModeAssignment

对象

  • Color
  • ExecDomain
  • LocalProcessInfo
  • MuxWindow
  • MuxTab
  • PaneInfomation
  • TabInfomation
  • SshDomain
  • SpawnCommand
  • Time
  • TlsDomainClient
  • TlsDomainServer
  • Pane
  • Window
  • WslDomain
  • Gui
  • Multiplexer
  • Window

小结

  • 本文主讲解,wezterm 适合的对象
  • 基本用法,基本 lua 配置
  • 链接 wsl 以及 虚拟机中的 ssh
  • tab 切换配置
  • 样式配置,绑定快捷键
  • 数字 wezterm 提供的顶层 lua api
  • 更多的,需要根据自己的需求探索

参考