Skip to content

Instantly share code, notes, and snippets.

@lebenasa
Last active October 4, 2024 06:11
Show Gist options
  • Save lebenasa/120eca4333af129c2e60d314e4cb9d77 to your computer and use it in GitHub Desktop.
Save lebenasa/120eca4333af129c2e60d314e4cb9d77 to your computer and use it in GitHub Desktop.
Use Neovim inside Windows Subsystem Linux in Windows
#!python3
"""
Start neovim server inside WSL then run neovim client (nvim-qt) in Windows.
Requirements:
- Windows Subsystem Linux 2
- Neovim inside WSL (install plugins, customizations, etc. in WSL-side)
- nvim-qt inside Windows (bundled with `choco install neovim`)
- Python3.6 or later inside Windows to run this script
- Correct PATH variables on Windows
- Must be able to find "wsl", "nvim" and "nvim-qt"
"""
import subprocess
import socket
import random
from concurrent import futures
def findFreePort() -> int:
host: str = "localhost"
port: int = 7777
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
is_available: bool = s.connect_ex((host, port)) != 0
while is_available is False:
port = random.randint(10000, 60000)
is_available = s.connect_ex((host, port)) != 0
return port
def startNeovimInWSL(port: int = 7777) -> subprocess.Popen:
"""
Start neovim server in WSL.
"""
host: str = "localhost:{}".format(port)
return subprocess.Popen(["wsl", "nvim", "--headless", "--listen", host])
def startNeovimClient(port: int = 7777) -> subprocess.Popen:
"""
Start neovim client in Windows, then kill all nvim process in WSL when
client finished.
"""
host: str = "localhost:{}".format(port)
return subprocess.Popen(["nvim-qt.exe", "--server", host])
def main():
print("Finding free port...")
port: int = findFreePort()
print("Starting neovim server in port {}".format(port))
serverProc = startNeovimInWSL(port)
print("Starting neovim client...")
clientProc = startNeovimClient(port)
with futures.ThreadPoolExecutor() as executor:
serverFut: futures.Future = executor.submit(serverProc.wait)
clientFut: futures.Future = executor.submit(clientProc.wait)
serverFut.add_done_callback(lambda done: clientProc.terminate())
clientFut.add_done_callback(lambda done: serverProc.terminate())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment