--- 
canonical: 'https://mwop.net/blog/2024-10-21-wezterm-keybindings.html'
title: 'Managing Wezterm Keybindings, or Merging with Lua'
author: "[Matthew Weier O'Phinney](https://mwop.net)"
created: '2024-10-21T17:15:17-05:00'
updated: '2024-10-21T17:15:17-05:00'
tags:
  - lua
  - wezterm

---
As I expand my [Wezterm](https://wezfurlong.org/wezterm/index.html) 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:

```lua
-- 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:

```lua
local merge = require 'merge'
```

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

```lua
config.keys = {}
```

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

```lua
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.
