Search code examples
bashtmux

Tmux truncate pane current path


I want to show current pan's path in the window status but as pan_current_path returns absolute path then it can be very large. So the question is how to truncate it like this:

PWD = /Users/SomeUser/SomeFolder/SomeFolder2/SomeFolder3
OUTPUT = .../SomeFolder2/SomeFolder3

According to this answer we can truncate it from the end to specific length but it will be like

OUTPUT = lder/SomeFolder2/SomeFolder3

So how to change remaining part of the folder name to "..."?


Solution

  • At the end I simply wrote the bash script

    #!/bin/sh
    path="$1"
    fixed_length=$2
    if (( ${#path} > $fixed_length )); then
        path=$(echo "$path" | tail -c "$fixed_length")
        if ! [[ "$path" =~ ^//* ]]; then
            path="/${path#*/}"
        fi
        path="...$path"
    fi
    echo $path
    

    Usage (where 64 is the fixed length):

    #(~/.tmux/scripts/truncate_path.sh #{pane_current_path} 64)