ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

如何写跨平台 tmux 脚本?psmux 让一份 bash 脚本在 Windows、Linux、macOS 通吃

如何写跨平台 tmux 脚本?psmux 让一份 bash 脚本在 Windows、Linux、macOS 通吃 如何写跨平台 tmux 脚本psmux 让一份 bash 脚本在 Windows、Linux、macOS 通吃【免费下载链接】psmuxTmux on Windows Powershell - tmux for PowerShell, Windows Terminal, cmd.exe. Includes psmux, pmux, and tmux commands. This is native High-Performance Tmux designed for Windows in Rust 项目地址: https://gitcode.com/gh_mirrors/ps/psmux想在 Windows、Linux、macOS 三台电脑上跑同一份 tmux 脚本psmux 是一个用 Rust 编写的原生 Windows 终端复用器完全兼容 tmux 的命令、配置与格式变量并自带tmux命令别名。装上它之后你写给 Linux 的那份 bash 会话脚本在 Windows 上原样就能跑。本文给出一套跨平台 tmux 脚本的实用写法、高频命令和平台差异清单帮你告别if OS Windows式的补丁代码。为什么 tmux 脚本能在 Windows 上直接运行psmux 安装后会提供三个可执行文件psmux.exe、pmux.exe和tmux.exe它们是同一个程序。命令名、参数、目标语法session:window.pane、%pane_id、140 格式变量、配置语法全部沿用 tmux 规范版本号也是 tmux 风格所以脚本里的tmux -V版本检查在三个平台上都能通过$ tmux -V tmux 3.3.8也就是说脚本里始终调用tmuxLinux/macOS 上它找到真正的 tmuxWindows 上它找到 psmux。完整兼容矩阵见 Compatibility 文档。一键安装 psmux最快 30 秒Windows 上用 winget 一条命令即可winget install psmux也支持scoop install psmux、choco install psmux或cargo install psmux。安装完执行tmux -V验证只要打印出tmux 3.3.x跨平台脚本的环境就齐了。一份 bash 脚本跑通三台机器下面是典型的项目会话引导脚本检测会话是否存在不存在就创建编辑器窗口 日志窗口的布局。在 Windows 上用 Git Bash 运行、在 Linux/macOS 上用任意 shell 运行内容一字不改#!/usr/bin/env bash # dev.sh幂等的会话引导三平台通用 S${1:-dev} if ! tmux has-session -t $S 2/dev/null; then tmux new-session -d -s $S -n editor tmux split-window -h -t $S:editor tmux new-window -d -t $S -n logs fi tmux list-windows -t $S -F #{window_index}:#{window_name}关键点只有一个has-session -t幂等模式——会话存在时退出码为 0不存在时为 1且失败路径会向 stderr 输出cant find session所以记得加2/dev/null。运行两次第二次自动变成空操作这正是自动化脚本最需要的行为。脚本里最好用的 5 个跨平台命令写跨平台 tmux 脚本时真正干活的基本就是下面这几个它们在 psmux 与 tmux 中的行为完全一致命令在脚本中的用途has-session -t 名字判断会话是否存在幂等创建的基础send-keys -t 目标 按键向指定面板打字如send-keys -t dev:logs git status Entercapture-pane -p -t 目标抓取面板屏幕内容用于断言和日志display-message -p #{变量}把会话名、面板 ID 等值读进脚本变量wait-for 通道名跨面板、跨进程的简单信号同步目标语法也完全通用dev:editor.0会话:窗口.面板、%3面板稳定 ID、4窗口稳定 ID脚本可以安全地按 tmux 的写法定位目标。更多命令细节见 Scripting 文档 和 命令参数参考。必须留意的 4 个平台差异绝大多数 tmux 脚本无需改动但以下 4 处是 Windows 与 Unix 的真实分界建议写脚本前对照一遍run-shell/if-shell的解释器不同Linux/macOS 上 tmux 用sh -c执行Windows 上 psmux 用pwsh -NoProfile -Command执行。想让一份配置两端通吃可以用这个便宜的分支Get-Command在 sh 里不存在、必然失败if-shell Get-Command pwsh source-file ~/.tmux.windows.conf source-file ~/.tmux.unix.conf路径形态不同#{pane_current_path}在 Windows 上返回C:\...而不是 POSIX 路径。Git Bash 里需要时用cygpath -u转一下反过来给pipe-pane写日志文件时要用cygpath -w转成 Windows 路径因为管道在 Windows 侧执行。面板默认 shell 不同Unix 下面板跑$SHELLWindows 下默认是 PowerShell 7。echo hello world在 pwsh 里是Write-Output会逐词换行输出——这是 shell 行为而非 psmux 行为。给面板发送的命令尽量保持 shell 中立或用单引号包住需要面板自己展开的变量。错误前缀是psmux:找不到目标时退出码同样是 1psmux: cant find pane: %999按冒号后面的措辞匹配即可兼容两端。完整对照表见官方教程 Cross Platform tmux Scripts。进阶把 TUI 变成无头测试对象复用器还有个隐藏福利全屏程序neovim、lazygit、AI CLI可以在无显示器环境下被脚本驱动并断言。核心套路只有两行——固定面板尺寸保证换行一致然后轮询capture-pane等待标记而不是固定 sleeptmux new-session -d -s ci -x 100 -y 30 -n tui tmux send-keys -t ci:tui nvim --clean Enter until tmux capture-pane -p -t ci:tui | grep -q NVIM; do sleep 0.5; done这段 CI 检查脚本在 WindowsGit Bash psmux与 Ubuntu runner 上可以共用同一个 job 体是跨平台 tmux 自动化里性价比最高的技巧。延伸阅读官方跨平台脚本完整教程docs/tutorials/cross-platform-tmux-scripts.md脚本与自动化全命令参考hooks、pipe-pane、目标语法docs/scripting.mdtmux 兼容矩阵docs/compatibility.md多窗口/多面板开发环境布局docs/tutorials/dev-environment-layouts.md面向 IDE 与工具的-C控制模式协议docs/control-mode.md一句话总结命令永远写tmux解释器相关逻辑用if-shell分支路径按需cygpath转换——做到这三点你的一份 bash 脚本就真正在 Windows、Linux、macOS 通吃了。【免费下载链接】psmuxTmux on Windows Powershell - tmux for PowerShell, Windows Terminal, cmd.exe. Includes psmux, pmux, and tmux commands. This is native High-Performance Tmux designed for Windows in Rust 项目地址: https://gitcode.com/gh_mirrors/ps/psmux创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表