Git Product home page Git Product logo

tmux-which-key's Introduction

tmux-which-key

A plugin for tmux that allows users to select actions from a customizable popup menu. Inspired by the likes of vscode-which-key, emacs-which-key, and which-key.nvim, this plugin aims to make users more effective at using tmux by reducing keyboard shortcut memorization and increasing feature discoverability.

Demo

Key presses
Key Action
w Windows menu
/ Split horizontal
Key Action
p Panes menu
h Left pane

✨ Features

  • An action menu with many of the common tmux commands
  • Mnemonic layout for easy memorization
  • Easily customizable extensible via YAML configuration
  • Support for user macros (multiple commands in one action)
  • Transient states (menus that stay open for repeated commands)
  • Runs in pure tmux script for speed

Demo

Here are a few examples of the plugin in action:

Navigation

Navigation

Key presses
Key Action
w Windows menu
/ Split horizontal
Key Action
w Windows menu
c Create window
Key Action
w Windows menu
p Previous window
Key Action
w Windows menu
w Select window
2 Window 2
Changing layouts

Layouts

Key presses
Key Action
w Windows menu
/ Split horizontal
Key Action
w Windows menu
- Split vertical
Key Action
w Windows menu
l Layouts menu
l (6 times) Next layout
Key Action
p Panes menu
p Select pane
0 Pane 0
User macros

Macros

Key presses
Key Action
C Client menu
P Plugins menu
u Update plugins
Key Action
C Client menu
r Reload

📦 Installation

Requirements

  • tmux>=3.0
  • python>=3.8 (optional)

Add the plugin to the list of TPM plugins in your ~/.tmux.conf:

set -g @plugin 'alexwforsythe/tmux-which-key'

Hit prefix + I to install and load the plugin. You'll be presented with a wizard to complete the installation.

Manual

  1. Clone this repository flag using the --recursive flag:
git clone --recursive https://github.com/alexwforsythe/tmux-which-key $HOME/.tmux/plugins/
  1. Register the plugin in your ~/.tmux.conf:
run-shell $PATH_TO_PLUGIN/plugin.sh.tmux
  1. Reload your tmux config to load the plugin:
tmux source-file $HOME/.tmux.conf

⚙️ Configuration

Menus can be customized by either:

  1. Editing config.yaml (requires python3), or
  2. Editing plugin/init.tmux directly

YAML

This method requires python3 to be installed on your system. Most Unix systems ship with it installed by default these days, so it shouldn't be a problem for most users. If you don't have python3 installed and aren't willing to use it, you'll need to edit plugin/init.tmux directly.

You can find the default configuration in config.example.yaml. The config file follows this structure:

# The starting index to use for the command-alias option, used for macros
# (required). This value must be at least 200
command_alias_start_index: 200
# The keybindings that open the action menu (required)
keybindings:
  prefix_table: Space # The keybinding for the prefix key table (required)
  root_table: C-Space # The keybinding for the root key table (optional)
# The menu title config (optional)
title:
  style: align=centre,bold # The title style
  prefix: tmux # A prefix added to every menu title
  prefix_style: fg=green,align=centre,bold # The prefix style
# The menu position (optional)
position:
  x: R
  y: P
# tmux-only environment variables that can be used in commands and macros
# (optional)
custom_variables:
  my_var: my_value
# User-defined macros that can be triggered by the menu (optional)
macros:
  restart-pane: # The macro name
    # The macro commands
    - "respawnp -k -c #{pane_current_path}"
    - display "#{log_info} Pane restarted"
# The root menu items (required)
items:
  - name: Next pane
    key: space # The key that triggers this action
    command: next-pane # A command to run
  - name: Respawn pane
    key: R
    macro: restart-pane # A custom macro (defined above)
  - separator: true # A menu separator
  - name: +Layout # A submenu
    key: l
    menu: # The submenu items
      - name: Next
        key: l
        command: nextl
        transient: true # Whether to keep the menu open until ESC is pressed

User options

@tmux-which-key-disable-autobuild

By default, the menu is rebuilt from config.yaml each time the plugin is initialized by TPM. If you aren't using the YAML configuration or want to reduce the plugin's impact on tmux startup time, you can disable this behavior by adding this to your ~/.tmux.conf before loading the plugin:

set -g @tmux-which-key-disable-autobuild=1
# ...
set -g @plugin 'alexwforsythe/tmux-which-key'
@tmux-which-key-xdg-enable

Changes the location of configuration and init files to use XDG directories. By default, these paths are used instead of this plugin's installation directory:

  • $XDG_CONFIG_HOME/tmux/plugins/tmux-which-key/config.yaml
  • $XDG_DATA_HOME/tmux/plugins/tmux-which-key/init.tmux.

The relative path from XDG_*_HOME can be changed using the @tmux-which-key-xdg-plugin-path option for additional customization.

set -g @tmux-which-key-xdg-enable=1
set -g @tmux-which-key-xdg-plugin-path=tmux/plugins/tmux-which-key # default

# ...

set -g @plugin 'alexwforsythe/tmux-which-key'

This allows the plugin to also be used with immutable or declarative operating systems.

Example Home Manager Nix Config
{
  lib,
  pkgs,
  ...
}: let
  tmux-which-key =
    pkgs.tmuxPlugins.mkTmuxPlugin
    {
      pluginName = "tmux-which-key";
      version = "2024-01-10";
      src = pkgs.fetchFromGitHub {
        owner = "alexwforsythe";
        repo = "tmux-which-key";
        rev = "<commit hash>";
        sha256 = lib.fakeSha256;
      };
      rtpFilePath = "plugin.sh.tmux";
    };
in {
  xdg.configFile = {
    "tmux/plugins/tmux-which-key/config.yaml".text = lib.generators.toYAML {} {
      command_alias_start_index = 200;
      # rest of config here
    };
  };
  programs.tmux.plugins = [
    {
      plugin = tmux-which-key;
      extraConfig = ''
        set -g @tmux-which-key-xdg-enable 1;
      '';
    }
  ];
}

Manual

You can customize the action menu by editing plugin/init.tmux directly.

I strongly recommend using YAML to customize your action menu because editing tmux script can be error-prone and difficult to debug.

Zsh

You can open tmux-which-key from the command line by running its tmux alias:

tmux show-wk-menu-root

You can trigger the menu with Space from vicmd mode--similarly to Spacemacs or VSpaceCode--by adding a ZLE widget and keybinding to your ~/.zshrc:

tmux-which-key() { tmux show-wk-menu-root ; }
zle -N tmux-which-key
bindkey -M vicmd " " tmux-which-key

❓ Known issues

Menu or submenu doesn't appear

Menus will silently fail to open if the number of items causes them to exceed the height of the terminal. If you run into this issue frequently, consider breaking you menu into multiple submenus.

Plugin overrides custom aliases

tmux-which-key uses tmux command-alias to execute certain actions, such as macros, quickly. command-alias is a global option, though, so these actions can collide with user aliases or ones defined by other plugins.

This plugin's aliases start at index 200 by default, but if there are already aliases mapped in this range, you can change the default starting index in config.yaml.

Related Projects

Resources

tmux-which-key's People

Contributors

alexwforsythe avatar higherorderfunctor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tmux-which-key's Issues

Cannot be installed with Nix [includes PR]

Awesome plugin! Appreciate the work to make key binds more accessible.

I am unable to install with Nix as it makes the plugin directory read-only. To remedy this I added some basic support for XDG directories to enable this plugin to be installed and used on immutable or declarative operating systems.

By default this would put he config in ~/.config/tmux/plugins/tmux-which-key and the generated init file in ~/.local/share/tmux/plugins/tmux-which-key.

I also added an example config for installing with Nix via Home Manager. The config can be fully managed via Nix in a declarative manor. The init file can then be written to a directory which is cleared every boot (immutable via temporary file system).

#1

Feature: Auto-show which-key after a timeout

I was confused for a while when first using this plugin because I was typing C-b (prefix) and expecting which-key to appear after a short delay, similar to which-key.nvim.

Eventually I realized that the configured "keybind" are intended to activate which-key itself, however it'd be great to also have an auto-display option.

If this is difficult or impossible, perhaps the README could be improved to more clearly explain usage?

Not working with TMUX 3.0a

The documentation states this should work with tmux >= 3.0. However, it does not work with tmux 3.0 because an unsupported argument in "setenv".

I was able to make it work by changing the line 135 in plugin/build.py from:

        return -h 'setenv {} "{}"'.format(self.name, self.value)

to

        return 'setenv {} "{}"'.format(self.name, self.value)

The option -hwas added to tmux in version 3.2. So, I suggest updating the documentation or removing the use of this option.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.