Dirty WezTerm Config
You are just moving to WezTerm and you want to do something vaguely troublesome with your WezTerm config and split it into multiple files which you can maybe keep in a central place with other config files? This is what I did.
My intent is to port this config to work similarly on MacOS in Zsh, but I’m starting on Windows 11 in PowerShell. I first have a single config file in one of WezTerm’s expected config locations, in my case that was C:\Users\$USER\.config\wezterm\wezterm.lua
(but I could have put it in C:\Users\$USER\.wezterm.lua
or the WezTerm program folder directly) and since I irrationally hate everything it contains the following.
local wezterm = require 'wezterm'
local config = {}
if wezterm.config_builder then
config = wezterm.config_builder()
end
First I’m doing the standard practice of setting up the config table ( Lua term ) and using the config builder (which you should be sure to use, as it enhances error reporting for your profile ).
local helpers = require 'helpers'
helpers.apply_to_config(config)
if wezterm.target_triple == 'x86_64-pc-windows-msvc' then
local windows = require 'windows'
windows.apply_to_config(config)
end
Next I’m loading my own custom Lua module helpers
in the standard way and then I’m conditionally loading a Lua module based on whether or not I’m running the WezTerm version built for Windows.[1]
return config
Then at the end I, of course, return the config table.
Once I have that file in place I can create the custom Lua modules in one of WezTerm submodule folder locations. In my case that was C:\Users\$USER\.config\wezterm\windows.lua
, for example. That file looks like the following.
local wezterm = require 'wezterm'
local module = {}
First we define the standard wezterm
and then initialize the module
table, which we will eventually return.
function module.apply_to_config(config)
local default_prog = {}
local launch_menu = {}
default_prog = { 'pwsh.exe', '-NoLogo' }
table.insert(launch_menu, {
label = 'PowerShell',
args = { 'powershell.exe', '-NoLogo' },
})
table.insert(launch_menu, {
label = 'Pwsh',
args = { 'pwsh.exe', '-NoLogo' },
})
config.default_prog = default_prog
config.launch_menu = launch_menu
end
Next I define module
’s apply_to_config
function, which will load the locally defined variables into the .wezterm.lua
’s config
table and thus affect our terminal. In this specific example I’m changing the default shell and inserting two entries in to the launch menu.[2]
return module
Lastly we return the module
table.
You can define arbitrary many custom modules (and you may not necessarily want to organize them the way I did). Is this all a good idea? Not for me to say, but probably not. Why not just do one big config file? Good question.
Can’t say as I’ve thought very hard about the way this is configured. Maybe the conditional should happen in the
windows.lua
file. ↩︎You can see here I’m defining local variables first, assigning their values, then assigning them to the corresponding record in the
config
table. I don’t necessarily need to do that, I could just assign to theconfig
table directly. ↩︎