Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Toggle leader key binding #656

Closed
ssoriche opened this issue Apr 6, 2021 · 10 comments
Closed

Question: Toggle leader key binding #656

ssoriche opened this issue Apr 6, 2021 · 10 comments
Labels
fixed-in-nightly This is (or is assumed to be) fixed in the nightly builds.

Comments

@ssoriche
Copy link

ssoriche commented Apr 6, 2021

I've been migrating my tmux settings directly into wezterm, but I haven't been able to find if there is a solution to this.

I've set my leader key with:

leader = {key = "s", mods = "CTRL"},

This is the same leader key I use in tmux. Some times I need to open up tmux inside of wezterm and and have the leader key sent to it. Right now my solution is to edit the wezterm configuration and comment that line out, then uncomment it when I'm done. This does not seem ideal.

With tmux when I would connect to a remote session and open tmux again (nested), I have F12 bound to switching the leader key target between inner and outer tmux sessions. Is it possible to set up a binding like that?

@wez
Copy link
Owner

wez commented Apr 6, 2021

Take a look at window:set_config_overrides; that shows how you can override the configuration for a window by pressing a key assignment of your choice.

You could modify that example to change the leader configuration when you press F12, assuming I understand your request!

@ssoriche
Copy link
Author

ssoriche commented Apr 6, 2021

That's perfect! Thank you again.

@ssoriche ssoriche closed this as completed Apr 6, 2021
@ssoriche
Copy link
Author

ssoriche commented Apr 7, 2021

I've been trying to implement this, and to me (naively) it doesn't appear that leader is exposed through window.

I tested with:

wezterm.on("toggle-leader", function(window, pane)
    wezterm.log_info("toggling the leader")
    local effective = window:effective_config()
    wezterm.log_info(effective.leader)
end)


return {
    debug_key_events = true,
    font_size = 12.0,
    leader = {key = "s", mods = "CTRL"},
    keys = {
        {
            key = "F12",
            mods = "NONE",
            action = wezterm.action {EmitEvent = "toggle-leader"},
        },
    },
}

And I get the following error:

 2021-04-07T19:55:46.613Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Function(12), modifiers: NONE, raw_key: None, raw_modifiers: NONE, raw_code: Some(111), repeat_count: 1, key_is_down: true }
 2021-04-07T19:55:46.613Z INFO  config::lua                       > lua: toggling the leader
 2021-04-07T19:55:46.614Z ERROR wezterm_gui::termwindow           > while processing toggle-leader event: callback error: stack traceback:
        [C]: in function 'wezterm.log_info'
        [string "/Users/ssoriche/.config/wezterm/wezterm.lua"]:6: in function <[string "/Users/ssoriche/.config/wezterm/wezterm.lua"]:3>

If I change:

    wezterm.log_info(effective.leader)

to

    wezterm.log_info(effective.font_size)

I get 12.0 in the log message and no errors. The error might be because I'm trying to pass a table to the function as a string, however if I change to:

wezterm.on("toggle-leader", function(window, pane)
    wezterm.log_info("toggling the leader")
    local effective = window:effective_config()
    local leader_key = effective.leader
    wezterm.log_info(leader_key.key)
end)

I get:

 2021-04-07T20:07:22.739Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Function(12), modifiers: NONE, raw_key: None, raw_modifiers: NONE, raw_code: Some(111), repeat_count: 1, key_is_down: true }
 2021-04-07T20:07:22.739Z INFO  config::lua                       > lua: toggling the leader
 2021-04-07T20:07:22.739Z ERROR wezterm_gui::termwindow           > while processing toggle-leader event: callback error: stack traceback:
        [C]: in function 'wezterm.log_info'
        [string "/Users/ssoriche/.config/wezterm/wezterm.lua"]:7: in function <[string "/Users/ssoriche/.config/wezterm/wezterm.lua"]:3>

@wez
Copy link
Owner

wez commented Apr 7, 2021

You can drill into the leader value: effective.leader.key.Char will return s for the config that you've included above. Internally, the key portion of what you wrote on the config is stored like this in lua:

   leader = {
       key={Char="s"},
   }

so you could test which value it is using:

   if effective.leader.key.Char == "s" then
      -- it's your baseline config
   else
       -- it's something else
   fi

alternatively, you can instead just look at the overrides:

  local overrides = window:get_config_overrides() or {}
  if not overrides.leader then
    -- no override set, so use alternative leader
    overrides.leader = {key="a", mods="CTRL"}
  else
    -- switch back to main leader config by clearing override
    overrides.leader = nil
  end
  window:set_config_overrides(overrides)

@ssoriche
Copy link
Author

ssoriche commented Apr 7, 2021

I think that lines up with what I have:

wezterm.on("toggle-leader", function(window, pane)
    wezterm.log_info("toggling the leader")
    local overrides = window:get_config_overrides() or {}
    if not overrides.leader then
        wezterm.log_info("leader wasn't set")
        overrides.leader = {key = "s", mods = "SUPER"};
    else
        wezterm.log_info("leader was set")
        overrides.leader = nil
    end

    window:set_config_overrides(overrides)
end)

The logs show the following

wezterm
 2021-04-07T23:43:36.728Z INFO  wezterm_mux_server_impl::local > setting up /Users/ssoriche/.local/share/wezterm/gui-sock-65625
 2021-04-07T23:43:36.835Z INFO  wezterm_gui::termwindow        > OpenGL initialized! AMD Radeon Pro 580 OpenGL Engine 4.1 ATI-4.2.15 is_context_loss_possible=false wezterm version: 20210405-110924-a5bb5be8
 2021-04-07T23:43:38.683Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Char('t'), modifiers: SUPER, raw_key: None, raw_modifiers: SUPER, raw_code: Some(17), repeat_count: 1, key_is_down: true }
 2021-04-07T23:43:41.504Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Function(12), modifiers: NONE, raw_key: None, raw_modifiers: NONE, raw_code: Some(111), repeat_count: 1, key_is_down: true }
 2021-04-07T23:43:41.504Z INFO  config::lua                       > lua: toggling the leader
 2021-04-07T23:43:41.504Z INFO  config::lua                       > lua: leader wasn't set
 2021-04-07T23:43:47.248Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Char('s'), modifiers: CTRL, raw_key: None, raw_modifiers: CTRL, raw_code: Some(1), repeat_count: 1, key_is_down: true }
 2021-04-07T23:43:48.015Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Char('n'), modifiers: NONE, raw_key: None, raw_modifiers: NONE, raw_code: Some(45), repeat_count: 1, key_is_down: true }
 2021-04-07T23:43:57.408Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Function(12), modifiers: NONE, raw_key: None, raw_modifiers: NONE, raw_code: Some(111), repeat_count: 1, key_is_down: true }
 2021-04-07T23:43:57.408Z INFO  config::lua                       > lua: toggling the leader
 2021-04-07T23:43:57.408Z INFO  config::lua                       > lua: leader was set
 2021-04-07T23:43:58.728Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Char('s'), modifiers: CTRL, raw_key: None, raw_modifiers: CTRL, raw_code: Some(1), repeat_count: 1, key_is_down: true }
 2021-04-07T23:43:59.303Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Char('n'), modifiers: NONE, raw_key: None, raw_modifiers: NONE, raw_code: Some(45), repeat_count: 1, key_is_down: true }

The result of ctrl-s n is the same with the leader being set or not set.

@ssoriche
Copy link
Author

ssoriche commented Apr 8, 2021

This gets more interesting, I copied and pasted your code above, and used the comments as wezterm.log_info statements to make it clearer what is happening, and added logging of what the effective configuration leader is like this:

wezterm.on("toggle-leader", function(window, pane)
    local overrides = window:get_config_overrides() or {}
    if not overrides.leader then
        wezterm.log_info("no override set, so use alternative leader")
        overrides.leader = {key = "a", mods = "CTRL"}
    else
        wezterm.log_info(
            "switch back to main leader config by clearing override")
        overrides.leader = nil
    end
    window:set_config_overrides(overrides)
    local effective = window:effective_config()
    wezterm.log_info("The leader is: " .. effective.leader.key.Char)
end)

I have leader c bound like this:

        {
            key = "c",
            mods = "LEADER",
            action = wezterm.action {SpawnTab = "CurrentPaneDomain"},
        },

The following log output shows:

  • toggling the leader from ctrl-s to ctrl-a
  • using ctrl-s c which creates a new tab
  • toggle the leader back using
  • ctrl-s c which creates another new tab
  • toggle the leader from ctrl-s to ctrl-a
  • using ctrl-a c which doesn't create a new tab
 wezterm
 2021-04-08T13:32:22.385Z INFO  wezterm_mux_server_impl::local > setting up /Users/ssoriche/.local/share/wezterm/gui-sock-80196
 2021-04-08T13:32:22.492Z INFO  wezterm_gui::termwindow        > OpenGL initialized! AMD Radeon Pro 580 OpenGL Engine 4.1 ATI-4.2.15 is_context_loss_possible=false wezterm version: 20210405-110924-a5bb5be8
 2021-04-08T13:32:27.253Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Function(12), modifiers: NONE, raw_key: None, raw_modifiers: NONE, raw_code: Some(111), repeat_count: 1, key_is_down: true }
 2021-04-08T13:32:27.253Z INFO  config::lua                       > lua: no override set, so use alternative leader
 2021-04-08T13:32:27.273Z INFO  config::lua                       > lua: The leader is: a
 2021-04-08T13:32:30.258Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Char('s'), modifiers: CTRL, raw_key: None, raw_modifiers: CTRL, raw_code: Some(1), repeat_count: 1, key_is_down: true }
 2021-04-08T13:32:31.017Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Char('c'), modifiers: NONE, raw_key: None, raw_modifiers: NONE, raw_code: Some(8), repeat_count: 1, key_is_down: true }
 2021-04-08T13:32:34.514Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Function(12), modifiers: NONE, raw_key: None, raw_modifiers: NONE, raw_code: Some(111), repeat_count: 1, key_is_down: true }
 2021-04-08T13:32:34.514Z INFO  config::lua                       > lua: switch back to main leader config by clearing override
 2021-04-08T13:32:34.535Z INFO  config::lua                       > lua: The leader is: s
 2021-04-08T13:32:36.049Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Char('s'), modifiers: CTRL, raw_key: None, raw_modifiers: CTRL, raw_code: Some(1), repeat_count: 1, key_is_down: true }
 2021-04-08T13:32:36.617Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Char('c'), modifiers: NONE, raw_key: None, raw_modifiers: NONE, raw_code: Some(8), repeat_count: 1, key_is_down: true }
 2021-04-08T13:36:40.710Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Function(12), modifiers: NONE, raw_key: None, raw_modifiers: NONE, raw_code: Some(111), repeat_count: 1, key_is_down: true }
 2021-04-08T13:36:40.710Z INFO  config::lua                       > lua: no override set, so use alternative leader
 2021-04-08T13:36:40.733Z INFO  config::lua                       > lua: The leader is: a
 2021-04-08T13:36:42.615Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Char('a'), modifiers: CTRL, raw_key: None, raw_modifiers: CTRL, raw_code: Some(0), repeat_count: 1, key_is_down: true }
 2021-04-08T13:36:43.182Z INFO  wezterm_gui::termwindow::keyevent > key_event KeyEvent { key: Char('c'), modifiers: NONE, raw_key: None, raw_modifiers: NONE, raw_code: Some(8), repeat_count: 1, key_is_down: true }

@ssoriche ssoriche reopened this Apr 9, 2021
@wez wez added the fixed-in-nightly This is (or is assumed to be) fixed in the nightly builds. label Apr 16, 2021
@wez
Copy link
Owner

wez commented Apr 16, 2021

This is fixed now in main; give it ~20 minutes or so to show up in the binaries built out by CI

@ssoriche
Copy link
Author

Thank you. I just tested this out and it seems to be working as expected.

I think I can use a similar pattern to active copy mode and in doing so set y as CopyTo="ClipboardAndPrimarySelection".

@wez
Copy link
Owner

wez commented Apr 22, 2021

Alright, let's call this closed!

@wez wez closed this as completed Apr 22, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2023

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
fixed-in-nightly This is (or is assumed to be) fixed in the nightly builds.
Projects
None yet
Development

No branches or pull requests

2 participants