Stop your shell from being a cluttered mess when you are in a deep directory tree
Environment & Shell Customization (Aliases/Functions/.Bashrc)
Stop your shell from being a cluttered mess when you are in a deep directory tree
🧩 The Challenge
Everyone knows the pain of a terminal prompt that eats half your screen width because the directory path is a mile long. You end up with almost no space left to type your actual commands.
💡 The Fix
Just add a simple function to your .bashrc that trims your prompt to show only the last few directories. It keeps your terminal clean without losing your sense of place.
function __prompt_trim() {
local pwd=${PWD/#$HOME/\~}
if [ ${#pwd} -gt 30 ]; then
echo "...${pwd: -30}"
else
echo "$pwd"
fi
}
PS1='$(__prompt_trim) \$ '
⚙️ Why It Works
This works by grabbing the PWD string, checking its length against a threshold, and using bash substring expansion to slice off the beginning if things get too cramped. It effectively anchors your focus to the current folder instead of the whole file tree.
🚀 Pro-Tip: Use the prompt_command variable to update this dynamically if you want to include git branch status too.
Linux Tips & Tricks | © ngelinux.com | 11/24/2026
