How to create a Tmux plugin
masterTmux plugins are essentially shell scripts that TPM (Tmux Plugin Manager) executes when the plugin is sourced. To create a plugin, you need a git repository containing one or more *.tmux files. TPM automatically executes all *.tmux files found in the plugin's directory.
Plugin Structure
A standard plugin structure typically includes:
- A
*.tmuxfile in the root directory (the plugin run file). - A
scripts/directory containing the actual logic/shell scripts.
Implementation Steps
- Initialize a Git project: TPM requires plugins to be git repositories.
- Create a plugin run file: Create a file ending in
.tmux(e.g.,my_plugin.tmux) and make it executable (chmod u+x). This file is responsible for setting up the plugin's environment, such as key bindings. - Define logic: Use the plugin run file to bind tmux keys to shell scripts located within your plugin directory.
- Publish: Push your code to a git provider (like GitHub). Users can then install it using the
@pluginsyntax in their.tmux.conf.
# Example plugin run file (my_plugin.tmux)
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
tmux bind-key T run-shell "$CURRENT_DIR/scripts/tmux_list_plugins.sh"