Skip to content

Instantly share code, notes, and snippets.

@audunolsen
Created September 27, 2017 18:13
Show Gist options
  • Save audunolsen/08192c0996054418a9a0db08d5c85810 to your computer and use it in GitHub Desktop.
Save audunolsen/08192c0996054418a9a0db08d5c85810 to your computer and use it in GitHub Desktop.
Show cwd, command and git dirty state all in the terminal's window/tab title.
# ~/.zshrc
# Override auto-title when static titles are desired ($ title My new title)
title() { export TITLE_OVERRIDDEN=1; echo -en "\e]0;$*\a"}
# Turn off static titles ($ autotitle)
autotitle() { export TITLE_OVERRIDDEN=0 }; autotitle
# Condition checking if title is overridden
overridden() { [[ $TITLE_OVERRIDDEN == 1 ]]; }
# Echo asterisk if git state is dirty
gitDirty() { [[ $(git status 2> /dev/null | grep -o '\w\+' | tail -n1) != ("clean"|"") ]] && echo "*" }
# Show cwd when shell prompts for input.
precmd() {
if overridden; then return; fi
pwd=$(pwd) # Store full path as variable
cwd=${pwd##*/} # Extract current working dir only
print -Pn "\e]0;$cwd$(gitDirty)\a" # Replace with $pwd to show full path
}
# Prepend command (w/o arguments) to cwd while waiting for command to complete.
preexec() {
if overridden; then return; fi
printf "\033]0;%s\a" "${1%% *} | $cwd$(gitDirty)" # Omit construct from $1 to show args
}
@audunolsen
Copy link
Author

Some context; I wrote this script to combat hyper's "broken" window/tab titles introduced in PR: #892. I've also written a bash alternative, although the code is messy and pretty unreadable.

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