Skip to content

Instantly share code, notes, and snippets.

@masato3
Last active July 10, 2026 10:56
Show Gist options
  • Select an option

  • Save masato3/8a36906d46791b25c82ba2db02ba27fc to your computer and use it in GitHub Desktop.

Select an option

Save masato3/8a36906d46791b25c82ba2db02ba27fc to your computer and use it in GitHub Desktop.
パイプが存在していないなら、WezTerm経由でfnm初期化してパイプ付与したNeovim起動。パイプが存在していたらNeovimに投げる。そのあとにWezTermのタブを閉じる
' パイプが存在していないなら、WezTerm経由でfnm初期化してパイプ付与したNeovim起動。パイプが存在していたらNeovimに投げる。そのあとにWezTermのタブを閉じる
' WezTerm経由のYaziから、WezTerm経由のNeovim開く用
Option Explicit
Dim shell, fso, args, filepath
Set shell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set args = WScript.Arguments
' --- 環境設定 ---
Dim pipe, wezterm, fnmInit, nvim, close_delay
pipe = "\\.\pipe\nvim-remote"
wezterm = "C:\tools\WezTerm\wezterm-gui.exe"
fnmInit = "C:\tools\cmder_mini\bin\fnm-init_command-prompt.cmd"
nvim = shell.ExpandEnvironmentStrings("%userprofile%\scoop\apps\neovim\current\bin\nvim.exe")
close_delay = 50
' ----------------
' 引数(ファイルパス)の取得
If args.Count > 0 Then
filepath = args(0)
Else
filepath = ""
End If
' パイプが存在するかどうかで分岐
If fso.FileExists(pipe) Then
' ---------------------------------------------------------
' 2回目以降の起動: 既存のNeovimにリモートでファイルを送る
' ---------------------------------------------------------
If filepath <> "" Then
Dim remoteCmd
remoteCmd = """" & nvim & """ --server " & pipe & " --remote """ & filepath & """"
' ここは裏で処理するため 0 (非表示) で実行
shell.Run remoteCmd, 0, True
End If
' フォーカス対策(WezTermを前面へ)
WScript.Sleep 50
ActivateWezTerm
Else
' ---------------------------------------------------------
' 1回目: WezTermごと新規起動
' ---------------------------------------------------------
Dim startCmd
If filepath <> "" Then
startCmd = """" & wezterm & """ start -- cmd.exe /c """"" & fnmInit & """ && """ & nvim & """ --listen """ & pipe & """ """ & filepath & """"
Else
startCmd = """" & wezterm & """ start -- cmd.exe /c """"" & fnmInit & """ && """ & nvim & """ --listen """ & pipe & """"
End If
shell.Run startCmd, 1, False
End If
' メモリ解放
Set shell = Nothing
Set fso = Nothing
Set args = Nothing
' --- WezTermを確実にアクティブにするサブプロシージャ ---
Sub ActivateWezTerm()
Dim wmi, processes, process, activated
Set wmi = GetObject("winmgmts:\\.\root\cimv2")
Set processes = wmi.ExecQuery("Select * from Win32_Process Where Name = 'wezterm-gui.exe'")
activated = False
For Each process In processes
If shell.AppActivate(process.ProcessId) Then
activated = True
Exit For
End If
Next
If Not activated Then
shell.AppActivate "wezterm"
End If
Set wmi = Nothing
Set processes = Nothing
End Sub
' ==============================================================================
' WezTermのタブを閉じる
' ==============================================================================
Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
' Leaderキー Ctrl-Space送信
wshShell.SendKeys "^ "
WScript.Sleep close_delay
' w送信してタブを閉じる
wshShell.SendKeys "w"
WScript.Sleep close_delay
' タブを閉じる確認ダイアログに対してy送信
wshShell.SendKeys "y"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment