# Driving a terminal multiplexer from Neovim

> One dispatch module so every split, tab and pane binding goes through a single place.

- Author: James Hong
- Published: 2024-11-12
- Tags: neovim, productivity
- URL: https://gnohj.net/blog/neovim-mux/

Editor splits and terminal panes are the same idea at two levels.
I want one set of keys for both, and I do not want my [Neovim](https://neovim.io/) config to know which [multiplexer](https://en.wikipedia.org/wiki/Terminal_multiplexer) is running.

## The problem

Several of my keymaps open something outside the editor:

- a diff view in its own tab
- a pull request dashboard in a new window
- a coding agent in a split beside the editor

The obvious way to write each one is to call the multiplexer's command directly from the keymap.
That works until the multiplexer changes.
Then every keymap that opens a pane has to be found and rewritten.

## One module in the middle

Every one of those keymaps now calls a small [Lua](https://www.lua.org/) module, and only that module knows how to talk to the multiplexer.
It is 140 lines.

A keymap says what it wants:

```lua
mux.new_window("gh dash", { name = "prs" })
mux.agent_split("claude")
```

It does not say how.

## Finding out what is running

The module works out which multiplexer it is inside at the moment a key is pressed, not when Neovim starts.

```lua
function M.kind()
  if vim.env.TERM_PROGRAM == "rex" and vim.env.REX_BLOCK then
    return "rex"
  end
  if probed == nil then
    local out = vim.trim(vim.fn.system({ mux_bin(), "kind" }))
    probed = (vim.v.shell_error == 0 and out ~= "none") and out or false
  end
  return probed or nil
end
```

It checks the pane's own environment first, because that is free.
Only if that says nothing does it run a probe, and the answer is remembered so the probe runs once.

Remembering a negative answer matters too.
Without it, a Neovim started outside any multiplexer would run the probe on every keypress.

## Never block the editor

Opening a pane takes a few command-line calls.
None of them is worth freezing the editor for, so the module starts the work and returns immediately.

```lua
local function dispatch(args, what)
  if not M.kind() then
    vim.notify("No session detected - can't " .. what, vim.log.levels.WARN)
    return
  end
  local cmd = { mux_bin() }
  vim.list_extend(cmd, args)
  vim.fn.jobstart(cmd, { detach = true })
end
```

If there is no multiplexer, the key does not fail silently.
It says what it could not do.

## The same door for the shell

The Lua module does not contain the multiplexer logic itself.
It calls a small command-line tool, `mux`, and my shell scripts call that same tool.

So there is one implementation, with two ways in.
A fix made for a shell script is a fix for the editor as well.

## Did it pay off

Yes, sooner than I expected.
I have changed multiplexer since writing this module, and the change was made in one place.
The keymaps were not touched.

The module also made the code testable.
An environment variable swaps the real tool for a stand-in, so a test can check what would have been opened without opening anything.

## What I would keep

- **Keymaps describe intent.** "Open this beside me", not a command line.
- **Detect late.** Ask what is running when the key is pressed.
- **Cache both answers.** Yes and no.
- **Do not block.** Start the job and return.
- **Share one tool** between the editor and the shell.
