Last active
June 4, 2025 06:00
-
-
Save ObjectBoxPC/ca8bc25ce95ec895238060bbca1be17d to your computer and use it in GitHub Desktop.
Print the working directory in a compact form (originally created for tmux status bar customization)
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
#!/bin/sh | |
# Print a compact version of the current working directory. | |
# The home directory is replaced with ~, and long paths are truncated to the final part with < as a marker. | |
# Examples: | |
# /home/example/Documents becomes ~/Documents | |
# /example/of/a/really/really/long/path becomes <ly/really/long/path | |
# The maximum length can be set using the LIMIT variable. | |
LIMIT=20 | |
HOME_REGEX=$(echo "$HOME" | sed 's/\//\\\//g') | |
TRANSLATED_PWD=$(echo "$PWD" | sed "s/^$HOME_REGEX/~/") | |
PWD_LENGTH=$(printf "%s" "$TRANSLATED_PWD" | wc -m) | |
if [ $PWD_LENGTH -le $LIMIT ]; then | |
echo "$TRANSLATED_PWD" | |
else | |
CUT_LIMIT=$(($LIMIT - 1)) | |
echo "<$(echo "$TRANSLATED_PWD" | cut -c $(($PWD_LENGTH - $CUT_LIMIT + 1))-)" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Consider using
#{=/-20/<:session_path}
in tmux configuration instead. It accomplishes nearly the same thing as this script, with some minor differences:~
<
is prepended without shortening the end part by 1, so the final result may be 1 longer than the limit