Managing Wezterm Keybindings, or Merging with Lua

As I expand my Wezterm usage, I find that either (a) a third-party module will have default keybinding configuration I want to adopt, and/or (b) I want to segregate keybindings related to specific contexts into separate modules to simplify my configuration.

Keybindings are stored as a list of tables (what we call associative arrays in PHP). Simple, right?

Unlike in other languages I use, Lua doesn't have a built-in way to merge lists.

So, I wrote up a re-usable function.

First, the file:

-- File: merge.lua
-- Provide generalized functionality for merging tables

local merge = {}

function merge.all(base, overrides)
    local ret    = base or {}
    local second = overrides or {}
    for _, v in pairs(second) do table.insert(ret, v) end
    return ret
end

return merge

Then in my main wezterm.lua, I import it:

local merge = require 'merge'

My keybindings are in config.keys, which is initialized as a list:

config.keys = {}

Another module might return configuration, and I can merge the keybindings it provides with what I have already defined:

config.keys = merge.all(config.keys, smart_splits.keys)

It's a simple piece of functionality, but it helps me keep things organized and modular.