Skip to content

Instantly share code, notes, and snippets.

@kelnishi
Created July 22, 2019 20:37
Show Gist options
  • Select an option

  • Save kelnishi/c522a97b43ea42bc50b07b25bf824a0d to your computer and use it in GitHub Desktop.

Select an option

Save kelnishi/c522a97b43ea42bc50b07b25bf824a0d to your computer and use it in GitHub Desktop.
macOS Terminal bash_profile script to color windows by directory
set_terminal_color() {
osascript -e "tell application \"Terminal\"
set current settings of window 1 to settings set \"$window_color\"
end tell"
}
colorize_terminal() {
update_terminal_cwd
if [ "$PWD" != "$MYOLDPWD" ]; then
MYOLDPWD="$PWD";
x="$PWD"
while [ "$x" != "/" ] ; do
if [[ `find "$x" -maxdepth 1 -name .window_color` ]]; then
window_color=`cat $x/.window_color`
set_terminal_color
break
fi
x=`dirname "$x"`
done
if [ "$x" == "/" ] ; then
window_color=`defaults read com.apple.Terminal "Default Window Settings"`
set_terminal_color
fi
fi
}
#
export PROMPT_COMMAND=colorize_terminal
# A convenience command to set the color for the current directory
dircolor() {
window_color=`sed -e 's/^"//' -e 's/"$//' <<<"$1"`
set_terminal_color
echo "$window_color" > .window_color
MYOLDPWD="$PWD";
}
@kelnishi

Copy link
Copy Markdown
Author

Just stick it in your .bash_profile and use dircolor something to set the window color for the directory.

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