-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot_wezterm.lua.tmpl
More file actions
521 lines (456 loc) · 17.2 KB
/
dot_wezterm.lua.tmpl
File metadata and controls
521 lines (456 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
-- Pull in the wezterm API
local wezterm = require("wezterm")
local chezmoi_dir = wezterm.home_dir .. "/.local/share/chezmoi"
local act = wezterm.action
local mux = wezterm.mux
local notes_dir = wezterm.home_dir .. "/notes"
local runbook_dir = chezmoi_dir .. "/runbook"
-- OS helpers
local function is_linux()
return wezterm.target_triple:find("linux") ~= nil
end
local function is_darwin()
return wezterm.target_triple:find("darwin") ~= nil
end
-- zoxide path per platform
local zoxide_path = ""
if is_darwin() then
zoxide_path = "/opt/homebrew/bin/zoxide"
elseif is_linux() then
zoxide_path = "/usr/bin/zoxide"
end
-- Resolve an executable via the user's login shell PATH (works for Homebrew, Nix, AppImage, etc.)
local function find_executable(name)
local shell = os.getenv("SHELL") or "sh"
-- 'command -v' outputs nothing on failure (unlike 'which' which prints "not found")
local handle = io.popen(shell .. " -l -c 'command -v " .. name .. "' 2>/dev/null")
if handle then
local result = handle:read("*l")
handle:close()
-- Only accept absolute paths to avoid "not found" noise
if result and result:sub(1, 1) == "/" then
return result
end
end
-- Fallback: probe known install locations directly
local candidates = {
"/opt/homebrew/bin/" .. name, -- macOS Homebrew (Apple Silicon)
"/usr/local/bin/" .. name, -- macOS Homebrew (Intel) / Linux
"/usr/bin/" .. name, -- Linux system package
wezterm.home_dir .. "/.nix-profile/bin/" .. name, -- Nix home-manager
wezterm.home_dir .. "/.local/bin/" .. name, -- AppImage / manual install
}
for _, path in ipairs(candidates) do
local f = io.open(path, "r")
if f then f:close() ; return path end
end
return name -- last resort: hope it's in PATH at spawn time
end
local nvim_path = find_executable("nvim")
-- ------------------------------
-- Functions
-- ------------------------------
local function nvim_cmd(vim_command)
-- -c takes a single Ex command; %q quotes it safely for the shell
local wrapped_command = "autocmd VimEnter * " .. vim_command
return string.format("%s +%q", nvim_path, wrapped_command)
end
-- ------------------------------
-- Hooks
-- ------------------------------
-- remember last workspace when using smart picker
wezterm.on("smart_workspace_switcher.workspace_switcher.before_select", function(window, path, label)
-- Called right before switching (if your plugin exposes such an event; if not, use `.selected` as a fallback)
last_ws = mux.get_active_workspace()
end)
-- If `before_select` doesn’t exist, use this conservative fallback:
wezterm.on("smart_workspace_switcher.workspace_switcher.selected", function(window, path, label)
-- If the plugin already switched at this point, do nothing. If it hasn't, this still records pre-switch in most cases.
last_ws = last_ws or mux.get_active_workspace()
end)
-- ------------------------------
-- Plugins
-- ------------------------------
-- Resurrect: save/restore tabs, panes, workspaces
local resurrect = wezterm.plugin.require("https://github.com/MLFlexer/resurrect.wezterm")
-- Auto-restore the last session on GUI startup (plugin-provided handler)
wezterm.on("gui-startup", resurrect.state_manager.resurrect_on_gui_startup)
-- Periodic auto-save so you don't lose work if the app/window is closed
-- Save automatically every 15 minutes (900 seconds)
resurrect.state_manager.periodic_save({
interval_seconds = 900, -- 15 minutes
save_workspaces = true,
save_windows = true,
save_tabs = true,
})
-- Restore when a new workspace is created by the switcher
wezterm.on("smart_workspace_switcher.workspace_switcher.created", function(window, path, label)
local workspace_state = resurrect.workspace_state
local state = resurrect.state_manager.load_state(label, "workspace")
if state then
workspace_state.restore_workspace(state, {
window = window,
relative = true,
restore_text = true,
on_pane_restore = resurrect.tab_state.default_on_pane_restore,
})
end
end)
-- Save whenever a workspace is selected
wezterm.on("smart_workspace_switcher.workspace_switcher.selected", function(window, path, label)
local workspace_state = resurrect.workspace_state
resurrect.state_manager.save_state(workspace_state.get_workspace_state())
end)
-- Workspace switcher (zoxide-backed)
local workspace_switcher = wezterm.plugin.require("https://github.com/MLFlexer/smart_workspace_switcher.wezterm")
workspace_switcher.zoxide_path = zoxide_path
-- ------------------------------
-- ------------------------------
-- Helpers
-- ------------------------------
-- ------------------------------
-- --------------------------------------------------
-- Workspace jump helper (Resurrect-aware, auto-clean)
-- --------------------------------------------------
local function jump_workspace(opts)
return wezterm.action_callback(function(win, pane)
last_ws = mux.get_active_workspace()
-- SwitchToWorkspace with spawn: if the workspace exists it switches to it,
-- if not it creates it in the current window (not a new OS window).
win:perform_action(
act.SwitchToWorkspace {
name = opts.name,
spawn = {
cwd = opts.cwd,
args = { os.getenv("SHELL") or "bash", "-c", opts.cmd },
},
},
pane
)
end)
end
-- --------------------------------
-- Toggle between current/last workspace
-- --------------------------------
local last_ws = nil
-- Switch to a named workspace, remembering where we came from.
local function switch_ws_remember(name, spawn)
return wezterm.action_callback(function(win, pane)
local current = mux.get_active_workspace()
if current ~= name then
last_ws = current
win:perform_action(
act.SwitchToWorkspace({
name = name,
spawn = spawn,
}),
pane
)
end
end)
end
-- Toggle back to the previously recorded workspace (ping-pong).
local function switch_to_last_ws()
return wezterm.action_callback(function(win, pane)
if not last_ws then
-- no previous workspace yet (e.g., first run)
return
end
local target = last_ws
last_ws = mux.get_active_workspace() -- swap to enable ping-pong
win:perform_action(act.SwitchToWorkspace({ name = target }), pane)
end)
end
-- Wrap an action so we remember the current workspace first.
local function remember_then(action)
return wezterm.action_callback(function(win, pane)
last_ws = mux.get_active_workspace()
win:perform_action(action, pane)
end)
end
-- ------------------------------
-- Helper: detect Neovim or tmux panes
-- ------------------------------
local function pane_process_name(pane)
local name = pane:get_foreground_process_name()
if not name or name == "" then
return nil
end
return name:match("([^/]+)$") or name
end
local function is_vim_or_tmux(pane)
-- reliable when smart-splits.nvim sets the user var
local user_vars = pane:get_user_vars() or {}
local is_vim = user_vars.IS_NVIM == "true"
local process_name = pane_process_name(pane)
local is_tmux = process_name and process_name:find("tmux") ~= nil
return is_vim
or is_tmux
or process_name == "nvim"
or process_name == "vim"
end
local direction_keys = { h = "Left", j = "Down", k = "Up", l = "Right" }
local function split_nav(key)
return {
key = key,
mods = "CTRL",
action = wezterm.action_callback(function(win, pane)
if is_vim_or_tmux(pane) then
-- pass through to Neovim/tmux
win:perform_action({
SendKey = { key = key, mods = "CTRL" },
}, pane)
else
-- Native Wezterm pane navigation
win:perform_action({ ActivatePaneDirection = direction_keys[key] }, pane)
end
end),
}
end
-- ------------------------------
-- Config
-- ------------------------------
local config = {}
if wezterm.config_builder then
config = wezterm.config_builder()
end
config.window_close_confirmation = "AlwaysPrompt"
config.max_fps = 120
config.window_decorations = "RESIZE"
config.default_cursor_style = "BlinkingBar"
config.font = wezterm.font_with_fallback({ "JetBrains Mono", "Fira Code" })
config.warn_about_missing_glyphs = false
config.font_size = 15
config.color_scheme = "MaterialDarker"
-- Default shell/program on Windows
if os.getenv("OS") == "Windows_NT" then
config.default_prog = { "pwsh.exe" }
end
-- Leader: Ctrl-g (portable to Zellij, low conflicts)
config.leader = { key = "g", mods = "CTRL", timeout_milliseconds = 2000 }
-- ------------------------------
-- Keys
-- ------------------------------
config.keys = {
--------------------------------------------------
--- Navigation & Splits
--------------------------------------------------
split_nav("h"),
split_nav("j"),
split_nav("k"),
split_nav("l"),
{ mods = "LEADER", key = "v", action = act.SplitHorizontal({ domain = "CurrentPaneDomain" }) },
{ mods = "LEADER", key = "h", action = act.SplitVertical({ domain = "CurrentPaneDomain" }) },
-- Tabs
{ mods = "LEADER", key = "t", action = act.SpawnTab("CurrentPaneDomain") },
-- Close (wrapped to save first; see save_then below)
-- (we also wrap the default CMD-w / CTRL|SHIFT-w under macOS/Linux style)
-- NOTE: Ctrl-w (without leader) still closes current pane as you had.
-- The wrapper is applied later with table.insert to keep ordering clean.
-- Tab cycling
{ mods = "CTRL", key = "LeftArrow", action = act.ActivateTabRelative(-1) },
{ mods = "CTRL", key = "RightArrow", action = act.ActivateTabRelative(1) },
-- Pane resize
{ mods = "LEADER", key = "LeftArrow", action = act.AdjustPaneSize({ "Left", 5 }) },
{ mods = "LEADER", key = "RightArrow", action = act.AdjustPaneSize({ "Right", 5 }) },
{ mods = "LEADER", key = "DownArrow", action = act.AdjustPaneSize({ "Down", 5 }) },
{ mods = "LEADER", key = "UpArrow", action = act.AdjustPaneSize({ "Up", 5 }) },
--{ mods = "LEADER", key = "Space", action = act.RotatePanes("Clockwise") },
{ mods = "LEADER", key = "0", action = act.PaneSelect { mode = "SwapWithActive" } },
-- Tabs bind Super+0 through Super+9 to activate the corresponding tab index
{ key = '0', mods = 'SUPER', action = wezterm.action.ActivateTab(0) },
{ key = '1', mods = 'SUPER', action = wezterm.action.ActivateTab(1) },
{ key = '2', mods = 'SUPER', action = wezterm.action.ActivateTab(2) },
{ key = '3', mods = 'SUPER', action = wezterm.action.ActivateTab(3) },
{ key = '4', mods = 'SUPER', action = wezterm.action.ActivateTab(4) },
{ key = '5', mods = 'SUPER', action = wezterm.action.ActivateTab(5) },
{ key = '6', mods = 'SUPER', action = wezterm.action.ActivateTab(6) },
{ key = '7', mods = 'SUPER', action = wezterm.action.ActivateTab(7) },
{ key = '8', mods = 'SUPER', action = wezterm.action.ActivateTab(8) },
{ key = '9', mods = 'SUPER', action = wezterm.action.ActivateTab(9) },
-- Windows
{ mods = "LEADER", key = "z", action = wezterm.action.TogglePaneZoomState},
--------------------------------------------------
--- Session / Workspace Management
--------------------------------------------------
{ mods = "LEADER", key = "w", action = act.ShowLauncherArgs { flags = "FUZZY|WORKSPACES" } },
{ mods = "CTRL|SHIFT", key = "s", action = workspace_switcher.switch_workspace() },
--{ mods = "LEADER", key = "[", action = act.SwitchWorkspaceRelative(1) },
--{ mods = "LEADER", key = "]", action = act.SwitchWorkspaceRelative(-1) },
{ mods = "LEADER", key = "[", action = remember_then(act.SwitchWorkspaceRelative(1)) },
{ mods = "LEADER", key = "]", action = remember_then(act.SwitchWorkspaceRelative(-1)) },
-- toggle last workspace
{ mods = "LEADER", key = "Space", action = switch_to_last_ws() },
--------------------------------------------------
--- Clipboard
--------------------------------------------------
-- Keep a single explicit paste binding; primary selection paste is optional
{ key = "V", mods = "CTRL", action = act.PasteFrom("Clipboard") },
--------------------------------------------------
--- Save / Restore (Resurrect)
--------------------------------------------------
-- Save full workspace state (manual on demand)
{
mods = "LEADER",
key = "S",
action = wezterm.action_callback(function(win, pane)
resurrect.state_manager.save_state(
resurrect.workspace_state.get_workspace_state()
)
end),
},
-- Restore from saved states with fuzzy picker
{
mods = "LEADER",
key = "R",
action = wezterm.action_callback(function(win, pane)
resurrect.fuzzy_loader.fuzzy_load(win, pane, function(id, _label)
local kind = string.match(id, "^([^/]+)") -- "workspace" | "window" | "tab"
local stem = string.match(id, "([^/]+)$") -- filename
stem = string.match(stem, "(.-)%.json$") -- strip extension
local state = resurrect.state_manager.load_state(stem, kind)
local opts = {
relative = true,
restore_text = true,
on_pane_restore = resurrect.tab_state.default_on_pane_restore,
}
if kind == "workspace" then
-- restore into the current mux window to avoid new-window spam
opts.window = win:mux_window()
resurrect.workspace_state.restore_workspace(state, opts)
elseif kind == "window" then
resurrect.window_state.restore_window(pane:window(), state, opts)
elseif kind == "tab" then
resurrect.tab_state.restore_tab(pane:tab(), state, opts)
end
end)
end),
},
-- ALT+d => Fuzzy delete a saved state (workspace/window/tab)
{
key = "D",
mods = "LEADER",
action = wezterm.action_callback(function(win, pane)
resurrect.fuzzy_loader.fuzzy_load(
win, pane,
function(id)
-- id is the selected state identifier. Delete it.
resurrect.state_manager.delete_state(id)
end,
{
title = "Delete State",
description = "Select State to Delete and press Enter = accept, Esc = cancel, / = filter",
fuzzy_description = "Search State to Delete: ",
is_fuzzy = true,
}
)
end),
},
--------------------------------------------------
--- Custom: jump to your config workspace
--------------------------------------------------
{
mods = "LEADER",
key = "c",
action = jump_workspace {
name = "config",
cwd = chezmoi_dir .. "/dot_config",
cmd = nvim_path .. " " .. chezmoi_dir .. "/dot_config",
},
},
{
mods = "LEADER",
key = "r",
action = jump_workspace {
name = "notes",
cwd = runbook_dir,
cmd = nvim_path .. " " .. runbook_dir,
},
},
{
mods = "LEADER",
key = "n",
action = jump_workspace {
name = "notes",
cwd = notes_dir,
cmd = nvim_cmd("FzfLua files cwd=" .. notes_dir),
},
},
{
mods = "LEADER",
key = "N",
action = jump_workspace {
name = "notes grep",
cwd = notes_dir,
cmd = nvim_cmd("FzfLua live_grep cwd=" .. notes_dir),
},
},
}
-- Leader + number -> Activate tab (0-9)
for i = 0, 9 do
table.insert(config.keys, {
key = tostring(i),
mods = "LEADER",
action = act.ActivateTab(i),
})
end
-- ------------------------------
-- Save-then-close wrappers
-- ------------------------------
local function save_then(action)
return wezterm.action_callback(function(win, pane)
resurrect.state_manager.save_state(
resurrect.workspace_state.get_workspace_state()
)
win:perform_action(action, pane)
end)
end
-- Wrap common close actions so they snapshot first
table.insert(config.keys, { key = "x", mods = "LEADER",
action = save_then(act.CloseCurrentPane { confirm = true }) })
-- ------------------------------
-- Window / Tabs UI
-- ------------------------------
config.hide_tab_bar_if_only_one_tab = false
config.tab_bar_at_bottom = true
config.use_fancy_tab_bar = false
config.tab_and_split_indices_are_zero_based = true
-- Wayland toggle
{{ if eq .chezmoi.os "darwin" -}}
config.enable_wayland = false
{{ end }}
-- ------------------------------
-- Statusline: left (leader indicator), right (workspace)
-- ------------------------------
wezterm.on("update-right-status", function(window, _)
-- Right: active workspace
window:set_right_status(window:active_workspace())
-- Left: leader status (ocean wave when leader active)
local left_icon = ""
local SOLID_LEFT_ARROW = ""
local ARROW_FORGROUND = { Foreground = { Color = "#c6a0f6" } }
if window:leader_is_active() then
left_icon = " " .. utf8.char(0x1f30a) -- ocean wave
SOLID_LEFT_ARROW = utf8.char(0xe0b2)
end
if window:active_tab():tab_id() ~= 0 then
ARROW_FORGROUND = { Foreground = { Color = "#1e2030 " } }
end
window:set_left_status(wezterm.format({
{ Background = { Color = "#b7bdf8" } },
{ Text = left_icon },
ARROW_FORGROUND,
{ Text = SOLID_LEFT_ARROW },
}))
end)
-- ------------------------------
-- Startup window: maximize only if nothing was restored
-- ------------------------------
wezterm.on("gui-startup", function(cmd)
if #mux.all_windows() == 0 then
local _, _, window = mux.spawn_window(cmd or {})
window:gui_window():maximize()
end
end)
-- and finally, return the configuration to wezterm
return config