Home > Blockchain >  Tmux truncate pane current path
Tmux truncate pane current path

Time:05-17

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 "..."?

CodePudding user response:

You can do the following :

OUTPUT=$(echo ".../$(basename "$(dirname "$PWD")")/$(basename "$PWD")")

Explanation and breaking down the code :

We can split the previous code using temporary variables

PWD='/Users/SomeUser/SomeFolder/SomeFolder2/SomeFolder3'
BASENAME1=$(basename "$PWD")               # value : SomeFolder3
DIRNAME1=$(dirname "$PWD")                 # value : /Users/SomeUser/SomeFolder/SomeFolder2
BASENAME2=$(basename "$DIRNAME1")          # value : SomeFolder2

OUTPUT=$(echo ".../$BASENAME2/$BASENAME1") # value : .../SomeFolder2/SomeFolder3

CodePudding user response:

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)
  • Related