Skip to content

Instantly share code, notes, and snippets.

@songlairui
Last active November 25, 2019 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save songlairui/ed6476404de3ce76953b3e9144876933 to your computer and use it in GitHub Desktop.
Save songlairui/ed6476404de3ce76953b3e9144876933 to your computer and use it in GitHub Desktop.
Tmux open with multi-pane within different working dir. 使用tmux快速切换到多个工作目录。
/**
* 使用 nodejs 打开
*/
const { spawnSync } = require("child_process");
// 关闭 tmux-session
spawnSync("tmux", ["kill-session", "-t", "developing"]);
// 打开上方 `open-tmux.sh`
spawnSync("osascript", [
"-e",
'tell application "Terminal" to do script "/Users/songlairui/space/songlairui-repo/tmux/open-tmux.sh"'
]);
// 展开上方 open-tmux.sh 内容
const innerCommand = `tmux new -s developing \\\\; \
send-keys '/Users/songlairui/space/songlairui-repo/developing' C-m \\\\; \
send-keys 'clear' C-m \\\\; \
split-window -v \\\\; \
send-keys '/Users/songlairui/space/songlairui-repo/developing-web' C-m \\\\; \
send-keys 'clear' C-m \\\\;`;
spawnSync("osascript", [
"-e",
`tell application "Terminal" to do script "${innerCommand}"`
]);
#!/bin/sh
tmux new -s developing \; \
send-keys '/Users/songlairui/space/songlairui-repo/developing' C-m \; \
send-keys 'clear' C-m \; \
split-window -v \; \
send-keys '/Users/songlairui/space/songlairui-repo/developing-web' C-m \; \
send-keys 'clear' C-m \;
# osascript -e 'tell application "Terminal" to do script "/Users/songlairui/space/songlairui-repo/tmux/open.sh"'
@songlairui
Copy link
Author

open-tmux.sh 中的反斜杠为两种反斜杠:

  • 行末反斜杠:表示多行内容为一行
  • 分号前反斜杠: 表示标准输入中输入的内容。(即tmux产生一个接受标准输入的状态。加上反斜杠,才能给到tmux,否则相当于在tmux之外执行)

open-tmux.js 中,确保spaw 标准输入接收到的字符串为 \\, 声明字符串就需要为 \\\\, 试验得出。

@songlairui
Copy link
Author

songlairui commented Nov 25, 2019

TODO

  • 默认执行命令, 如 yarn serve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment