#Linux

#Bash

.bashrc highlighting for write access, git and root

23 days ago

23

While setting up my System to use Linux as the main OS again, I thought it might be neat to have some basic highlighting in the Terminal, including highlighting for write access on the current Directory, or when using git or the root-User.

Why?

Well, you could use just use zsh and select a decent skin, but most of them are too bloated for my taste and I haven't seen highlighting for write access. So why not?



How?

Simple, use Bash/Prompt customization and just edit the ~/.bashrc to append the following:


export GIT_PS1_SHOWCOLORHINTS=true
export GIT_PS1_SHOWSTASHSTATE=true
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
export GIT_PS1_SHOWUPSTREAM="auto"
export GIT_PS1_DESCRIBE_STYLE="default"
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    #PROMPT_COMMAND=$(test -w "$PWD" && PS1="write" || PS1="nowrite")
    # git states:
    # + staged changes
    # * unstaged changes
    # $ stashed changes
    # % untracked changes
    # <,>,<>,= behind,ahead,divered, on track relative to upstream
    PROMPT_COMMAND='
    git_str="$(__git_ps1 " \e[2;37m\][%s\e[2;37m\]] ")"
    git_str=${git_str/\+/📝}
    git_str=${git_str/\*/✏️}
    git_str=${git_str/\%/🔆}
    git_str=${git_str/\$/📦}
    git_str=${git_str/</🔽}
    git_str=${git_str/>/🔼}
    git_str=${git_str/<>/🔀}
    git_str=${git_str/=/✅}
    title=$(dirs +0)
    if [[ "${#title}" -gt "1" || "$title" == "~" ]]; then
        title=$title/
    fi
    window_title="\e]0;$title\a"
    if [ -w . ]; then
        PS1="$window_title${debian_chroot:+($debian_chroot) }\e[2;37m\] \A [\e[1;34m\]\u@\h\e[00m\]\e[2;37m\]]:\e[1;34m\]$title\e[00m\]$git_str\e[00m\]\e[1;32m\$\e[00m\] "
    else
        PS1="$window_title${debian_chroot:+($debian_chroot) }\e[2;37m\] \A [\e[1;34m\]\u@\h\e[00m\]\e[2;37m\]]:\e[1;33m\]$title\e[00m\]$git_str\e[00m\]\e[1;32m\$\e[00m\] "
    fi
    '
    ;;
*)
    ;;
esac

I also added some basic highlighting with emojis for git.

And for the root-User in /root/.bashrc:


case "$TERM" in
xterm*|rxvt*)
    PROMPT_COMMAND='
    title=$(dirs +0)
    if [[ "${#title}" -gt "1" || "$title" == "~" ]]; then
        title=$title/
    fi
    window_title="\e]0;$title\a"
    PS1="$window_title${debian_chroot:+($debian_chroot) }\e[2;37m\] \A [\e[1;31m\]\u@\h\e[00m\]\e[2;37m\]]:\e[1;34m\]$title\e[2;31m#\e[00m\] "
    '
    ;;
*)
    ;;
esac

And here is the reuslt: