Wezterm workspace switcher API
Note
Apr 11, 2024Here 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 neededlocal home = wezterm.home_dirlocal 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 thenwezterm.log_info("cancelled")elsewezterm.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)endend),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 thenwindow:perform_action(a.SwitchToWorkspace({name = line,}),pane)endend),}),}
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