如何让Windows Terminal启动时预填命令不执行且支持多窗格?
Windows Terminal 预填命令不执行解决方案
单窗格场景
你原有方案会自动执行命令是因为启动PowerShell时直接将yarn start作为启动命令传入,修改为调用SendKeys模拟输入即可实现预填不执行:
wt -p "Windows Powershell" -d C:\dir powershell -noExit -c "(New-Object -ComObject WScript.Shell).SendKeys('yarn start')"
多窗格场景适配
你原有多窗格配置失效的核心原因是仅给最后一个窗格指定了预填逻辑,而Windows Terminal多窗格启动时默认焦点落在第一个窗格,会导致命令输入错位。正确配置需要给每一个需要预填命令的窗格单独指定启动逻辑:
wt ` -p "Windows Powershell" -d C:\dir1 powershell -noExit -c "(New-Object -ComObject WScript.Shell).SendKeys('yarn start')" `; split-pane ` -p "Windows Powershell" -d C:\dir2 powershell -noExit -c "(New-Object -ComObject WScript.Shell).SendKeys('yarn start')" `; split-pane ` -p "Windows Powershell" -d C:\dir3 powershell -noExit -c "(New-Object -ComObject WScript.Shell).SendKeys('yarn start')"
稳定性优化
如果出现命令输入不全、错位的情况,可以添加短延迟确保窗格加载完成后再执行输入,修改对应启动参数即可:
powershell -noExit -c "Start-Sleep -Milliseconds 200; (New-Object -ComObject WScript.Shell).SendKeys('yarn start')"
内容的提问来源于stack exchange,提问作者stemby
相关产品推荐
相关产品推荐

