Skip to content

Instantly share code, notes, and snippets.

@tqwewe
Created June 13, 2025 06:20
Show Gist options
  • Select an option

  • Save tqwewe/bc7198e1961c86153ede1ed3c0f0fb17 to your computer and use it in GitHub Desktop.

Select an option

Save tqwewe/bc7198e1961c86153ede1ed3c0f0fb17 to your computer and use it in GitHub Desktop.
Fish shell function to auto-rename Zellij tabs based on current directory

Zellij Auto Tab Rename

A Fish shell function that automatically renames Zellij tabs based on the current directory when using cd.

Features

  • Automatically updates tab names to match the current directory
  • Only renames default tabs (e.g., "Tab #1") or previously auto-renamed tabs
  • Preserves manually renamed tabs
  • Configurable prefix to mark auto-generated names

Installation

Add the function to your Fish configuration:

# ~/.config/fish/functions/cd.fish or ~/.config/fish/config.fish
function cd
    # ... (paste the function code here)
end

Reload your Fish configuration:

source ~/.config/fish/config.fish

Configuration

Change navigation command (default uses z from zoxide):

# Change this line:
z $argv
# To use regular cd:
builtin cd $argv

Customize the prefix:

set prefix "*"          # Results in: *project-name
set prefix "dir:"       # Results in: dir:project-name  
set prefix "[auto] "    # Results in: [auto] project-name

Examples

Tab #1 → cd ~/projects/my-app → *my-app
Tab #2 → cd ~/documents → *documents

"my-custom-name" → cd ~/projects → "my-custom-name" (unchanged)
*old-project → cd ~/new-project → *new-project (updated)

Requirements

function cd
# Use 'z' for directory navigation - change this to 'builtin cd' if you don't use zoxide
z $argv
# Update zellij current tab name
if test $status -eq 0
# Configure the prefix for auto-generated tab names
set prefix "*"
# Get current tab name
set current_tab_name (zellij action dump-layout 2>/dev/null | grep "tab name=.*focus=true" | sed 's/.*name="\([^"]*\)".*/\1/')
# Check if it's a default tab name OR an auto-generated one with our prefix
# Use literal string matching to avoid glob pattern issues
if string match -qr '^Tab #[0-9]+$' "$current_tab_name"; or test (string sub --length (string length "$prefix") "$current_tab_name") = "$prefix"
set current_dir (basename $PWD)
zellij action rename-tab "$prefix$current_dir" 2>/dev/null
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment