Skip to content

Instantly share code, notes, and snippets.

@arulrajnet
Last active March 8, 2025 14:05
Show Gist options
  • Save arulrajnet/d848ff1181c482ca0eb5d958b4111516 to your computer and use it in GitHub Desktop.
Save arulrajnet/d848ff1181c482ca0eb5d958b4111516 to your computer and use it in GitHub Desktop.
WezTerm config for Open VSCode from the Remote SSH Shell.
-- The only required line is this one.
local wezterm = require 'wezterm'
local mux = wezterm.mux
local act = wezterm.action
-- Some empty tables for later use
local config = wezterm.config_builder()
local keys = {}
local mouse_bindings = {}
local launch_menu = {}
--- Custom Config
config.color_scheme = 'Hardcore'
config.debug_key_events = false
config.default_cursor_style = 'BlinkingBar'
config.disable_default_key_bindings = false
config.font = wezterm.font('JetBrainsMono Nerd Font')
config.font_size = 14
config.hide_tab_bar_if_only_one_tab = true
config.launch_menu = launch_menu
config.line_height = 1.0
config.macos_window_background_blur = 40
config.mouse_bindings = mouse_bindings
config.native_macos_fullscreen_mode = true
config.scrollback_lines = 5000
config.use_fancy_tab_bar = false
config.window_background_opacity = 0.75
config.window_decorations = "RESIZE"
-- set the status bar to show the active workspace
wezterm.on('update-right-status', function(window, pane)
window:set_right_status(window:active_workspace())
end)
wezterm.on('gui-startup', function(cmd)
-- allow `wezterm start -- something` to affect what we spawn
-- in our initial window
local args = {}
if cmd then
args = cmd.args
end
-- Set a workspace for coding on a current project
-- Top pane is for the editor, bottom pane is for the build tool
local project_dir = wezterm.home_dir
local tab, build_pane, window = mux.spawn_window {
workspace = 'coding',
cwd = project_dir,
args = args,
}
-- We want to startup in the coding workspace
mux.set_active_workspace 'coding'
end)
-- Open VSCode Remote SSH
wezterm.on('open_vscode', function(window, pane)
wezterm.log_info("open_vscode event fired")
-- Get the command output from remote shell
local res, err = pane:get_foreground_process_name()
if not err and string.match(res, "ssh") then
-- Pane title
local pane_title = pane:get_title()
-- Generate a unique marker to identify our output
local marker = "WEZTERM_REMOTE_INFO_" .. os.time()
-- Clear any pending output first
pane:send_text("\n")
-- Send command to get hostname and current directory
-- Define the SSH_HOSTNAME in /etc/environment
pane:send_text("echo '" .. marker .. "_BEGIN'; echo -n 'HOST:'; hostname; echo 'SSH_HOST:'${SSH_HOSTNAME}; echo -n 'PWD:'; pwd; echo '" .. marker .. "_END'\n")
-- Give the command some time to execute
wezterm.sleep_ms(600)
-- Get the output from the scrollback
local scrollback = pane:get_lines_as_text(50)
-- Find the LAST occurrence of the markers
local last_start_pos = nil
local last_end_pos = nil
local current_pos = 1
-- Find all occurrences of the BEGIN marker and keep the last one
while true do
local start_pos = scrollback:find(marker .. "_BEGIN\n", current_pos)
if not start_pos then
break
end
last_start_pos = start_pos
current_pos = start_pos + 1
end
-- Reset for finding END marker
current_pos = 1
-- Find all occurrences of the END marker and keep the last one
while true do
local end_pos = scrollback:find(marker .. "_END", current_pos)
if not end_pos then
break
end
last_end_pos = end_pos
current_pos = end_pos + 1
end
wezterm.log_info("Scrollback:", scrollback)
-- Log the positions
wezterm.log_info("Last Start pos:", last_start_pos, "Last End pos:", last_end_pos)
-- Extract information only if we found both markers and they're in correct order
local path = pane_title -- Default to pane title
local host = nil
if last_start_pos and last_end_pos and last_start_pos < last_end_pos then
local info_text = scrollback:sub(last_start_pos + #(marker .. "_BEGIN\n"), last_end_pos - 1)
-- wezterm.log_info("Extracted info: " .. info_text)
-- Parse the path
path = info_text:match("PWD:([^\n]+)")
host = info_text:match("SSH_HOST:([^\n]+)")
host = host or info_text:match("HOST:([^\n]+)")
end
if path then
-- Extract the path and remote machine
local remote_host = host
local cwd = path
if cwd and remote_host then
-- Construct the VSCode Remote SSH command
local command = { "/usr/local/bin/code", "--remote", "ssh-remote+" .. remote_host, cwd }
wezterm.log_info("Running:", table.concat(command, " "))
-- Run the command locally
wezterm.run_child_process(command)
end
end
end
end)
-- Keys
config.keys = {
{ key = 'o', mods = 'CTRL', action = wezterm.action { EmitEvent = 'open_vscode' } },
}
wezterm.log_info("wezterm config loaded")
return config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment