Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ vim.api.nvim_create_autocmd({ 'VimEnter', 'VimLeave' }, {
})
```

Or via `uv run --script` (resolves `libtmux` from PEP 723 metadata, no system install needed):
```lua
local loop = vim.uv

vim.api.nvim_create_autocmd({ 'VimEnter', 'VimLeave' }, {
callback = function()
if vim.env.TMUX_PLUGIN_MANAGER_PATH then
local script = vim.env.TMUX_PLUGIN_MANAGER_PATH .. '/tmux-window-name/scripts/rename_session_windows.py'
loop.spawn('uv', { args = { 'run', '--script', script } })
end
end,
})
```

### Vim Script autocmd example
```vim
" update the script path based on your setup
Expand All @@ -95,6 +109,13 @@ if !empty($TMUX) && has('job')
endif
```

Or via `uv run --script`:
```vim
if !empty($TMUX) && has('job')
autocmd VimEnter,VimLeave * call job_start('uv run --script ' . expand('$HOME/.config/tmux/plugins/tmux-window-name/scripts/rename_session_windows.py'))
endif
```

### Automatic rename after changing dir
By default `tmux-window-name` hooks `after-select-window` which trigged when switching windows, you can add hook in your `.shellrc` to execute `tmux-window-name`
##### .zshrc
Expand All @@ -106,6 +127,15 @@ tmux-window-name() {
add-zsh-hook chpwd tmux-window-name
```

Or via `uv run --script`:
```bash
tmux-window-name() {
(uv run --script $TMUX_PLUGIN_MANAGER_PATH/tmux-window-name/scripts/rename_session_windows.py &)
}

add-zsh-hook chpwd tmux-window-name
```

#### Hooks Used
Make sure the hooks that used aren't overridden.
* @resurrect-hook-pre-restore-all
Expand All @@ -130,6 +160,19 @@ To make the shortest path as possible the plugin finds the shortest not common p

## Installation

### Recommended: install [uv](https://docs.astral.sh/uv/)

If `uv` is on your `PATH`, the plugin auto-detects it and runs the script via `uv run --script`. `libtmux` is resolved from inline script metadata; the steps below for `pip install libtmux` and `dataclasses` are **not needed** in this case.

Install uv:
```sh
curl -LsSf https://astral.sh/uv/install.sh | sh
```

If `uv` is not present, the plugin falls back to `python3` and requires `libtmux` to be installed manually — see the next section.

---

### Install libtmux (must)
_**Note**_: Make sure you are using the `user` python and not `sudo` python or `virutalenv` python!

Expand Down
12 changes: 10 additions & 2 deletions scripts/rename_session_windows.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.7"
# dependencies = [
# "libtmux",
# ]
# ///

import logging
import logging.config
Expand Down Expand Up @@ -132,11 +138,13 @@ def enable_user_rename_hook(server: Server):
Indicator if we should rename the window or not
"""
current_file = Path(__file__).absolute()
launcher = os.environ.get('TMUX_WINDOW_NAME_LAUNCHER', '').strip()
invocation = f'{launcher} {current_file}' if launcher else str(current_file)
server.cmd(
'set-hook',
'-g',
f'after-rename-window[{HOOK_INDEX}]',
f'if-shell "[ #{{n:window_name}} -gt 0 ]" "set -w @tmux_window_name_enabled 0" "set -w @tmux_window_name_enabled 1; run-shell "{current_file}"',
f'if-shell "[ #{{n:window_name}} -gt 0 ]" "set -w @tmux_window_name_enabled 0" "set -w @tmux_window_name_enabled 1; run-shell \\"{invocation}\\""',
)


Expand Down Expand Up @@ -499,4 +507,4 @@ def main():


if __name__ == '__main__':
main()
main()
22 changes: 14 additions & 8 deletions tmux_window_name.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

LIBTMUX_AVAILABLE=$(python -c "import importlib.util; print(importlib.util.find_spec('libtmux') is not None)" 2>/dev/null)
if [ "$LIBTMUX_AVAILABLE" = "False" ]; then
tmux display "ERROR: tmux-window-name - Python dependency libtmux not found (Check the README)"
exit 0
if command -v uv >/dev/null 2>&1; then
LAUNCHER="uv run --script"
else
LAUNCHER="python3"
LIBTMUX_AVAILABLE=$(python3 -c "import importlib.util; print(importlib.util.find_spec('libtmux') is not None)" 2>/dev/null)
if [ "$LIBTMUX_AVAILABLE" = "False" ]; then
tmux display "ERROR: tmux-window-name - Python dependency libtmux not found (Check the README)"
exit 0
fi
fi
export TMUX_WINDOW_NAME_LAUNCHER="$LAUNCHER"

tmux set -g automatic-rename on # Set automatic-rename on to make #{automatic-rename} be on when a new window is been open without a name
tmux set-hook -g 'after-new-window[8921]' 'set -wF @tmux_window_name_enabled \#\{automatic-rename\} ; set -w automatic-rename off'
tmux set-hook -g 'after-select-window[8921]' "run-shell -b ""$CURRENT_DIR""/scripts/rename_session_windows.py"
tmux set-hook -g 'after-select-window[8921]' "run-shell -b \"$LAUNCHER $CURRENT_DIR/scripts/rename_session_windows.py\""

############################################################################################
### Hacks for preserving users custom window names, read more at enable_user_rename_hook ###
############################################################################################

"$CURRENT_DIR"/scripts/rename_session_windows.py --enable_rename_hook
$LAUNCHER "$CURRENT_DIR"/scripts/rename_session_windows.py --enable_rename_hook

# Disabling rename hooks when tmux-ressurect restores the sessions
tmux set -g @resurrect-hook-pre-restore-all ""$CURRENT_DIR"/scripts/rename_session_windows.py --disable_rename_hook"
tmux set -g @resurrect-hook-post-restore-all ""$CURRENT_DIR"/scripts/rename_session_windows.py --post_restore"
tmux set -g @resurrect-hook-pre-restore-all "$LAUNCHER $CURRENT_DIR/scripts/rename_session_windows.py --disable_rename_hook"
tmux set -g @resurrect-hook-post-restore-all "$LAUNCHER $CURRENT_DIR/scripts/rename_session_windows.py --post_restore"