Skip to content

Instantly share code, notes, and snippets.

@ObjectBoxPC
Last active June 4, 2025 06:00
Show Gist options
  • Save ObjectBoxPC/ca8bc25ce95ec895238060bbca1be17d to your computer and use it in GitHub Desktop.
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)
#!/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
@ObjectBoxPC
Copy link
Author

Consider using #{=/-20/<:session_path} in tmux configuration instead. It accomplishes nearly the same thing as this script, with some minor differences:

  • The home directory is not shortened to ~
  • If the path is longer than the limit, the < is prepended without shortening the end part by 1, so the final result may be 1 longer than the limit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment