Created
February 13, 2026 06:36
-
-
Save bigardone/7b98c4f480e28bc69c96ba0187d855d3 to your computer and use it in GitHub Desktop.
Zellij rename tabs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Zellij rename tabs | |
| function current_dir() { | |
| local current_dir=$PWD | |
| # Home directory fallback | |
| if [[ $current_dir == $HOME ]]; then | |
| echo "~" | |
| return | |
| fi | |
| # Try to get git info | |
| local git_common_dir branch repo_name | |
| if git_common_dir=$(git rev-parse --git-common-dir 2>/dev/null); then | |
| # Get the parent repo name (works for both regular repos and worktrees) | |
| # In main repo: --git-common-dir returns ".git" | |
| # In worktree: --git-common-dir returns "/path/to/main-repo/.git" | |
| if [[ $git_common_dir == ".git" ]]; then | |
| # We're in the main repo, use toplevel | |
| repo_name=$(git rev-parse --show-toplevel 2>/dev/null) | |
| repo_name=${repo_name##*/} | |
| else | |
| # We're in a worktree, extract from common dir path | |
| repo_name=${git_common_dir%/.git} # Remove /.git suffix | |
| repo_name=${repo_name%.git} # Remove .git suffix (bare repos) | |
| repo_name=${repo_name##*/} # Get just the folder name | |
| fi | |
| branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) | |
| if [[ -n $branch ]]; then | |
| # Abbreviate branch: split by '-', take 3 chars of each word | |
| local abbreviated=$(echo "$branch" | awk -F'-' '{ | |
| for(i=1; i<=NF; i++) { | |
| printf "%s%s", substr($i, 1, 3), (i<NF ? "-" : "") | |
| } | |
| }') | |
| echo "[$repo_name] $abbreviated" | |
| return | |
| fi | |
| echo "[$repo_name]" | |
| return | |
| fi | |
| # Not in git repo - just show directory name | |
| echo ${current_dir##*/} | |
| } | |
| function change_tab_title() { | |
| local title=$1 | |
| command nohup zellij action rename-tab $title >/dev/null 2>&1 | |
| } | |
| function set_tab_to_working_dir() { | |
| local result=$? | |
| local title=$(current_dir) | |
| # uncomment the following to show the exit code after a failed command | |
| # if [[ $result -gt 0 ]]; then | |
| # title="$title [$result]" | |
| # fi | |
| change_tab_title $title | |
| } | |
| if [[ -n $ZELLIJ ]]; then | |
| add-zsh-hook precmd set_tab_to_working_dir | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment