Reduce path elements to one character save the last element, like vim buffer names.
❯ pwd
/Users/tupton/code/compact_path
❯ python compact_path.py $(pwd)
/U/t/c/compact_path
❯ python compact_path.py $(pwd | sed "s:$HOME:~:")
~/c/compact_path
Pass the --trigger
or -t
option to specify the length at which compaction will
take place. Any paths that are less than this length will not be compacted.
❯ pwd
/Users/tupton/code/compact_path
❯ python compact_path.py $(pwd) --trigger 35
/Users/tupton/code/compact_path
❯ python compact_path.py $(pwd) -t 10
/U/t/c/compact_path
You can use this in your shell prompt.
function compact_path() {
local cp="/usr/local/bin/compact_path"
if [[ -e "$cp" ]]; then
echo $("$cp" "$1" --trigger 20)
else
echo "$1"
fi
}
PROMPT='$(compact_path "${PWD/#$HOME/~}") %# '
In zsh
, that would result in a prompt that looks like the following.
~/code/compact_path % cd /usr/local/Library/Homebrew/
/u/l/L/Homebrew % cd ~/code/compact_path
~/code/compact_path %
See my zshrc
for the actual compact_path
function and how I use it in my prompt.