Skip to content

Instantly share code, notes, and snippets.

@ian-h-chamberlain
Last active April 21, 2025 01:06
Show Gist options
  • Save ian-h-chamberlain/12a5d815754011d48051148424a74326 to your computer and use it in GitHub Desktop.
Save ian-h-chamberlain/12a5d815754011d48051148424a74326 to your computer and use it in GitHub Desktop.
Helper function to abbreviate wezterm key definitions
local function keys(tbl)
local result = {}
for k, act in pairs(tbl) do
local mods, last
for key in k:gmatch('([^-]+)') do
if last then
if mods then
mods = mods .. '|' .. last
else
mods = last
end
end
last = ({
['C'] = "CTRL", -- TODO os-specific
['S'] = 'SHIFT',
})[key] or key
end
table.insert(result, {
key = last,
mods = mods,
action = act,
})
end
return result
end
config.keys = keys {
['C-m'] = wezterm.action.DisableDefaultAssignment,
['C-S-p'] = wezterm.action.SomethingElse,
}
-- Other possible options depending how magic I wanna make it / syntax preferences
-- Some may work better using full names like Ctrl / Shift instead of vim notation
config.keys = keys {
-- underscores to avoid ["quoted"] form
C_m = wezterm.action.DisableDefaultAssignment,
}
config.keys = {
-- helper function to create a single binding instead of all
k { "C-m", wezterm.action.DisableDefaultAssignment },
}
-- __index on some top-level modifier objects
config.keys = {
C.m wezterm.action.DisableDefaultAssignment,
C.S.p wezterm.action.SomethingElse,
}
moon = require "moon"
wezterm = require 'wezterm'
local mt
mt = {
__index: (idx) =>
if idx\sub(1, 1) == "_" then return rawget(self, idx)
mod = switch idx
when "cmd" then "CMD"
when "ctrl" then "CTRL"
when "alt" then "ALT"
when "shift" then "SHIFT"
_mods = moon.copy @_mods or {}
local _key
if mod
table.insert _mods, mod
else
_key = idx
setmetatable {:_mods, :_key }, mt
__call: (action = nil) =>
{
key: @_key
mods: table.concat @_mods, "|"
:action
}
}
key = setmetatable({}, mt)
{ :cmd, :alt, :shift, :ctrl } = key
act = wezterm.action
split_pane = (win, dir) ->
new_pane = win\active_tab!\get_pane_direction dir
if not new_pane
wezterm.log_warn "No pane found to move to"
return
pane = win\active_tab!\active_pane!
wezterm.run_child_process {
"/opt/homebrew/bin/wezterm",
"cli",
"split-pane",
"--pane-id",
tostring(new_pane),
"--move-pane-id",
tostring(pane\pane_id!),
}
nil
apply_to_config = (config) ->
config.debug_key_events = true
for k, v in pairs {
cmd.ctrl.RightArrow wezterm.action_callback (win, pane) ->
split_pane win, "Right"
cmd.ctrl.LeftArrow wezterm.action_callback (win, pane) ->
split_pane win, "Left"
cmd.ctrl.UpArrow wezterm.action_callback (win, pane) ->
split_pane win, "Up"
cmd.ctrl.DownArrow wezterm.action_callback (win, pane) ->
split_pane win, "Down"
}
config.keys[k] = v
print("Done from moon:", wezterm.to_string(config.keys))
{ :apply_to_config }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment