~ views

Wezterm workspace switcher API


Wezterm configuration file with workspaces switcher keybindings.

Note

Apr 11, 2024

Does Neovim run slow in your terminal? On OSX, Kitty appears to run Neovim the fastest, and smart-splits.nvim lets you navigate between Neovim and other terminal panes, even when Kitty is in stacked mode. This may replace the need for a multiplexer like tmux, which will make Neovim faster as there is no pseudoterminal in between.

Kitty terminal logo

Here is an example of how to use Wezterm’s workspace API. Below, we are defining a list of workspaces that the user can switch to. Using InputSelector, the user is prompted to select a workspace from the list, causing the terminal to switch to that workspace upon selection. Each workspace has its own set of panes, windows, and tabs.

Here we use the InputSelector action to create a list of workspaces that wezterm will then use to prompt the user to select from it:

{
mods = "LEADER",
key = "1",
action = wezterm.action_callback(function(window, pane)
-- Here you can dynamically construct a longer list if needed
local home = wezterm.home_dir
local workspaces = {
{ id = home, label = "Home" },
{ id = home .. "/Code", label = "Work" },
{ id = home .. "/Documents", label = "Personal" },
{ id = home .. "/.config", label = "Config" },
}
window:perform_action(
a.InputSelector({
action = wezterm.action_callback(function(inner_window, inner_pane, id, label)
if not id and not label then
wezterm.log_info("cancelled")
else
wezterm.log_info("id = " .. id)
wezterm.log_info("label = " .. label)
inner_window:perform_action(
a.SwitchToWorkspace({
name = label,
spawn = {
label = "Workspace: " .. label,
cwd = id,
},
}),
inner_pane
)
end
end),
title = "Choose Workspace",
choices = workspaces,
fuzzy = true,
-- Nightly version only: https://wezfurlong.org/wezterm/config/lua/keyassignment/InputSelector.html?h=input+selector#:~:text=These%20additional%20fields%20are%20also%20available%3A
-- fuzzy_description = "Fuzzy find and/or make a workspace",
}),
pane
)
end),
}

Video demo

Adding a shortcut to create workspaces on the fly

Want to make new workspaces on the fly? Try this:

{
key = "N",
mods = "CTRL|SHIFT",
action = a.PromptInputLine({
description = wezterm.format({
{ Attribute = { Intensity = "Bold" } },
{ Foreground = { AnsiColor = "Fuchsia" } },
{ Text = "Enter name for new workspace" },
}),
action = wezterm.action_callback(function(window, pane, line)
-- `line` will be `nil` if user hits <ESC> without entering anything.
-- If enter is pressed, line will be the text they wrote; otherwise "".
if line then
window:perform_action(
a.SwitchToWorkspace({
name = line,
}),
pane
)
end
end),
}),
}

The final step is, as you might have done already, to reload your Wezterm configuration! Now, you can toggle between personal and work tabs, or seperate tabs and panes by project.

Back to top