Skip to content

Instantly share code, notes, and snippets.

@Congee
Created May 26, 2016 15:10
Show Gist options
  • Save Congee/3c849cc8c3ebe24d5c0b592ae74cb6fd to your computer and use it in GitHub Desktop.
Save Congee/3c849cc8c3ebe24d5c0b592ae74cb6fd to your computer and use it in GitHub Desktop.
shortening pwd
autoload -U colors && colors
precmd() {
red_at="%{$fg[red]}@%{$reset_color%}"
red_dollar="%{$fg[red]%}$%{$reset_color%}"
typeset host_info
[[ -n $SSH_CONNECTION ]] && host_info="$USER${red_at}$HOST "
if [[ $USER == "CC" ]]; then
# number of chracters of the path of the prompt is less 30
# use '~' to represent $HOME as long as possible
if [[ $PWD =~ $HOME ]]; then
pwd="${PWD[@]//$HOME/~}"
prefix_slash=
else
pwd=$PWD
prefix_slash=/
fi
# ugly work around
# IFS=/ read -A pwdarr <<< "$pwd"
pwdarr=(${(ps:/:)pwd})
if [[ ${#pwd[@]} -le 30 ]]; then
prompt="%~ ${red_dollar} "
else
prompt="${prefix_slash}${host_info}${pwdarr[1]}/.../${pwdarr[-1]} ${red_dollar} "
fi
else # other user
prompt=${red_at}'%c % '
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment