Skip to content

Instantly share code, notes, and snippets.

@madushan1000
Last active August 28, 2023 21:06
Show Gist options
  • Save madushan1000/8d912e9aaaaf76bacdc399a4ac833db4 to your computer and use it in GitHub Desktop.
Save madushan1000/8d912e9aaaaf76bacdc399a4ac833db4 to your computer and use it in GitHub Desktop.
wezterm tmux keybindings
local config = {}
config.key_tables = {
tmux_bindings = {
{ key = "a", mods = "CTRL", action=wezterm.action{SendString="\x01"}},
{ key = "\"", mods = "SHIFT", action=wezterm.action{SplitVertical={domain="CurrentPaneDomain"}}},
{ key = "%",mods = "SHIFT", action=wezterm.action{SplitHorizontal={domain="CurrentPaneDomain"}}},
{
key = "z",
action=wezterm.action.Multiple {
"TogglePaneZoomState",
"PopKeyTable",
},
},
{ key = "x", action=wezterm.action.CloseCurrentPane { confirm = true }, },
{ key = "q", action=wezterm.action{PaneSelect={alphabet = '0123456789'}}},
{ key = 'o', mods = 'CTRL', action = wezterm.action.RotatePanes 'Clockwise' },
{ key = "o", action=wezterm.action{ActivatePaneDirection="Next"}},
{ key = "n", action=wezterm.action{ActivatePaneDirection="Next"}},
{ key = "p", action=wezterm.action{ActivatePaneDirection="Prev"}},
{ key = "RightArrow", action=wezterm.action{ActivatePaneDirection="Right"}},
{ key = "LeftArrow", action=wezterm.action{ActivatePaneDirection="Left"}},
{ key = "UpArrow", action=wezterm.action{ActivatePaneDirection="Up"}},
{ key = "DownArrow", action=wezterm.action{ActivatePaneDirection="Down"}},
{ key = "RightArrow", mods = "CTRL", action=wezterm.action{AdjustPaneSize={"Right", 2}}},
{ key = "LeftArrow", mods = "CTRL", action=wezterm.action{AdjustPaneSize={"Left", 2}}},
{ key = "UpArrow", mods = "CTRL", action=wezterm.action{AdjustPaneSize={"Up", 2}}},
{ key = "DownArrow", mods = "CTRL", action=wezterm.action{AdjustPaneSize={"Down", 2}}},
{ key = "!", mods = "SHIFT", action=wezterm.action_callback(function(win, pane) local tab, window = pane:move_to_new_window() end)},
{ key = "k", action=wezterm.action.ClearScrollback 'ScrollbackAndViewport'},
{ key = "l", action=wezterm.action.ClearScrollback 'ScrollbackOnly' },
}
}
config.keys = {
{
key = "a",
mods = 'CTRL',
action = wezterm.action.ActivateKeyTable {
name = 'tmux_bindings',
timeout_milliseconds = 500,
one_shot = false,
},
}
}
return config
@madushan1000
Copy link
Author

There is also a hacky way to merge panes back into a window, there is a command wezterm cli split-pane --move-pane-id we can use to merge any pane into split of current pane. As far as I can see, this functionality is not exposed via lua.

function find_other_window_panes(window, pane)
  local windows = wezterm.mux.all_windows()
  local panes = {}
  for _, window in ipairs(windows) do
    for _, tab in ipairs(window:tabs()) do
      for _, pane in ipairs(tab:panes()) do
        table.insert(panes, pane)
      end
    end
  end

  local current_tab_panes = {}
  for _, pane in ipairs(window:active_tab():panes()) do
      for i, v in ipairs(panes) do
        if v:pane_id() == pane:pane_id() then
          table.remove(panes, i)
        end
      end
  end

  return panes
end

config.key_tables = {
  tmux_bindings = {
    { key = "d", action=wezterm.action_callback(function(window, pane)
      local choices = {}
      local panes =  find_other_window_panes(window, pane)
      for i,pane in ipairs(panes) do
        table.insert(choices, { id = tostring(i), label = pane:pane_id() .. '/' .. pane:get_title()})
      end

      window:perform_action(wezterm.action.InputSelector {
        action = wezterm.action_callback(function(window, pane, id, label)
          print(panes[1])
          if id and label then
            wezterm.run_child_process { 'wezterm', 'cli', 'split-pane', '--move-pane-id', panes[tonumber(id)]:pane_id(), '--pane-id', pane:pane_id()}
          end
        end),
        title = "select pane",
        choices = choices,
      },
      pane)
    end)},
  }
}

config.keys = {
    { 
      key = "a", 
      mods = 'CTRL', 
      action = wezterm.action.ActivateKeyTable { 
        name = 'tmux_bindings', 
        timeout_milliseconds = 500,
        one_shot = false,
        -- until_unknown = true,
      }, 
    }

}

return config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment