Last active
December 10, 2024 19:26
-
-
Save andkirby/389f18642fc08d1b0711d17978445f2b to your computer and use it in GitHub Desktop.
Install colorization for PS1 in bash.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
################################### | |
# PS1 color for .bashrc file | |
# | |
# curl -Ls https://gist.github.com/andkirby/389f18642fc08d1b0711d17978445f2b/raw/bashrc_ps1_install.sh | bash | |
# curl -Ls https://bit.ly/bash-ps1 | bash | |
################################### | |
touch ~/.bashrc | |
if ! cat ~/.bashrc | grep '# Colorize PS1 console' > /dev/null; then | |
echo >> ~/.bashrc | |
if [[ "$(uname -a)" =~ 'Msys' ]]; then | |
curl -Ls https://gist.github.com/andkirby/389f18642fc08d1b0711d17978445f2b/raw/bashrc_ps1_win_git_bash >> ~/.bashrc | |
else | |
# Unfortunately, this approach doens't work in GitBash. It cannot work with status code. | |
curl -Ls https://gist.github.com/andkirby/389f18642fc08d1b0711d17978445f2b/raw/bashrc_ps1_linux >> ~/.bashrc | |
fi | |
echo 'PS1 has been updated in ~/.bashrc. Please run' | |
echo ' $ . ~/.bashrc' | |
else | |
echo 'You have already installed "Colorize PS1 console".' > /dev/stderr | |
echo 'If you need to replace it, please remove it first in your ~/.bashrc file.' > /dev/stderr | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Colorize PS1 console | |
# Custom function to shorten the path | |
# Params: | |
# PATH (optional) | |
# MAX_LENGTH (optional) | |
shorten_path() { | |
local path=${1:-$(pwd)} | |
local max_length=${2:-40} | |
if [ "${#path}" -gt "$max_length" ]; then | |
local dir_array=( $(echo "$path" | tr '/' '\n') ) | |
local dir_count=${#dir_array[@]} | |
if [ "$dir_count" -gt 4 ]; then | |
path="/${dir_array[1]}/${dir_array[2]}/.../${dir_array[$dir_count-1]}" | |
local index=1 dir_item | |
while [[ "$(( ${#dir_item} + ${#path} ))" -le "$max_length" ]]; do | |
((index=index+1)) | |
dir_item=${dir_array[$dir_count-$index]} | |
path=${path/\.\.\./".../"${dir_item}} | |
done | |
fi | |
fi | |
echo "$path" | |
} | |
# curl -Ls https://gist.github.com/andkirby/389f18642fc08d1b0711d17978445f2b/raw/bashrc_ps1_install.sh | bash | |
PS1="\n\[\033[01;37m\]\$? " | |
PS1=${PS1}"\$(if [[ \$? == 0 ]]; then echo \"\[\033[01;32m\]\342\234\223\"; else echo \"\[\033[01;31m\]\342\234\227\"; fi)" | |
PS1=${PS1}" $(if [[ ${EUID} == 0 ]]; then echo '\[\033[01;31m\]\h'; else echo '\[\033[0;32m\]\u@\h'; fi)" | |
PS1=${PS1}" \[\033[00m\]\D{%T}\[\033[0;33m\] "'`shorten_path`'"\[\033[00m\] \n\$ " | |
# EOF Colorize PS1 console |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Colorize PS1 console (GitBash for Windows) | |
# curl -Ls https://bit.ly/bash-ps1 | bash | |
# curl -Ls https://gist.github.com/andkirby/389f18642fc08d1b0711d17978445f2b/raw/bashrc_ps1_install.sh | bash | |
# Custom function to shorten the path | |
# Params: | |
# PATH (optional) | |
# MAX_LENGTH (optional) | |
shorten_path() { | |
local path=${1:-$(pwd)} | |
local max_length=${2:-40} | |
if [ "${#path}" -gt "$max_length" ]; then | |
local dir_array=( $(echo "$path" | tr '/' '\n') ) | |
local dir_count=${#dir_array[@]} | |
if [ "$dir_count" -gt 4 ]; then | |
path="/${dir_array[1]}/${dir_array[2]}/.../${dir_array[$dir_count-1]}" | |
local index=1 dir_item | |
while [[ "$(( ${#dir_item} + ${#path} ))" -le "$max_length" ]]; do | |
((index=index+1)) | |
dir_item=${dir_array[$dir_count-$index]} | |
path=${path/\.\.\./".../"${dir_item}} | |
done | |
fi | |
fi | |
echo "$path" | |
} | |
PS1="\[\033]0;\n\[\033[01;37m\]\$? " | |
PS1+="\[\033[0;35m\]\h\[\033[00m\] \D{%T} \[\033[33m\]"'`shorten_path`'"\[\033[36m\]"'`__git_ps1`'"\[\033[0m\]" | |
PS1+="\n" | |
PS1+="\$(if [[ \$? == 0 ]]; then echo \"\[\033[01;32m\]\342\234\223\"; else echo \"\[\033[01;31m\]\342\234\227\"; fi) " | |
PS1+="\[\033[0m\]$ " | |
# EOF Colorize PS1 console (GitBash for Windows) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Colorize PS1 console (GitBash for Windows) | |
# curl -Ls https://gist.github.com/andkirby/389f18642fc08d1b0711d17978445f2b/raw/bashrc_ps1_install.sh | bash | |
PS1="\[\033]0;\n\[\033[01;37m\]\$? " | |
PS1=${PS1}"\[\033[0;35m\]\h\[\033[00m\] \D{%T} \[\033[33m\]\w\[\033[36m\]"'`__git_ps1`'"\[\033[0m\]" | |
PS1=${PS1}"\n" | |
PS1=${PS1}"\$(if [[ \$? == 0 ]]; then echo \"\[\033[01;32m\]\342\234\223\"; else echo \"\[\033[01;31m\]\342\234\227\"; fi) " | |
PS1=${PS1}"\[\033[0m\]$ " | |
# EOF Colorize PS1 console |
Short URL:
curl -Ls https://bit.ly/bash-ps1 | bash
Added path shortening. E.g.: /G/stm/.../apps/python/a/b/c/d/a/b/c/d
It will keep the whole path length not longer than 40 symbols by default.
It will start replacing directories starting from 3rd with "..." (would be tricky for a single letter directories 😉, but it's a rare case)
Custom installation of shorten_path
- Copy function code (find function
shorten_path()
in the code above) into.bashrc file
- Replace
\w
withshorten_path
function in your PS1 declaration.
- if your string is double-quoted:
"... \w ..."
→"... "'`shorten_path`'" ..."
. - if your string is single-quoted:
'... \w ...'
→'... `shorten_path` ...'
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add PS1 colorization in bash.
Just run:
It will add a content of this gist according to your OS type (Windows or other).