wez / wezterm

A GPU-accelerated cross-platform terminal emulator and multiplexer written by @wez and implemented in Rust
https://wezfurlong.org/wezterm/
Other
17.56k stars 785 forks source link

Switch to last pane like tmux's "PREFIX ;" - ActivateLastTab but for a Pane #3505

Open luv2code opened 1 year ago

luv2code commented 1 year ago

I am coming from tmux and I commonly use to vertical split panes (code on the right, term on the left) in my workflow, and I have a muscle memory prefix ; to quickly switch between them.

Describe the solution you'd like A new action similar to ActivateLastTab but for panes, so that I can bind it to the <leader> ; key.

Describe alternatives you've considered I tried to figure out how to bind a function to a key so that I could write my own last pane function; but I don't think it's possible?

luv2code commented 11 months ago

I was able to code up a workaround.

config.keys = {
  {
    key = ';',
    mods = 'LEADER',
    action = wezterm.action {EmitEvent = "activate-last-pane"},
  },
}
local last_active_by_tab = {}
wezterm.on('format-tab-title', function(tab, tabs, panes, config, hover, max_width)
  if tab.is_active and not last_active_by_tab[tab.tab_id] then
    last_active_by_tab[tab.tab_id] = tab.active_pane.pane_id
  end
end)

wezterm.on("activate-last-pane", function(window, pane)
  if pane:tab() then
    local tabid = pane:tab():tab_id()
    local lastpaneid = last_active_by_tab[tabid]
    local curpaneid = pane:pane_id()
    for _, p in pairs(pane:tab():panes()) do
      local paneid = p:pane_id()
      if paneid == lastpaneid then
        p:activate()
        last_active_by_tab[tabid] = curpaneid
      end
    end
  end
end)

I'm leaving the issue open because this seems pretty ugly to me. I'm feel like I'm abusing the format-tab-title event here; but it's the only event I know of that fires with the active pane changes. Though, it fires too many times to be truly useful for this purpose.

It doesn't work right because I can't figure out how to keep state about the last activated pane. If there are more than two panes, and you navigate between them, it gets confused.