Funciones personalizadas para ~/.bashrc
nano ~/.bashrc
. ~/.bashrc
1. Prompt personalizado información de Git
Este script modifica tu PS1 para mostrar:
- La fecha y hora.
- Tu usuario, equipo y directorio actual.
- El repositorio y rama de Git activa (si estás dentro de uno). Dorada si hay cambios sin confirmar
get_git_repo_branch() {
if git rev-parse --show-toplevel &>/dev/null; then
repo=$(basename "$(git rev-parse --show-toplevel)")
branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
# Verificar si hay cambios no confirmados
if git diff --quiet && git diff --cached --quiet; then
# No changes: display in green
printf ' \001\e[1;38;2;140;250;140m\002%s(%s)\001\e[0m\002' "$repo" "$branch"
else
# There are uncommitted changes: display in bold orange
printf ' \001\e[1;38;5;214m\002%s(%s)\001\e[0m\002' "$repo" "$branch"
fi
fi
}
# Colores en bold
BOLDGREEN="\[\e[1;32m\]" # verde bold
BOLDBLUE="\[\e[1;34m\]" # azul bold
BOLDORANGE="\[\e[1;38;5;214m\]" # naranja bold (256-color)
BOLDCYAN="\[\e[1;36m\]" # azul claro bold (cian)
RESET="\[\e[0m\]" # reset
# Prompt modificado para aplicar el color correspondiente
PS1="${BOLDCYAN}[\D{%m-%d} \A] ${RESET}${BOLDGREEN}\u@\h${RESET}:${BOLDBLUE}\w${RESET}\$(get_git_repo_branch)\n# "
2. Protección contra git add .
# Protección contra 'git add .'
confirm_git_add() {
if [[ "$@" == "add ." ]]; then
echo -e "\033[1;33mAre you sure you want to run an 'add .'? (y/n):\033[0m"
read -n 1 respuesta
echo
if [[ $respuesta == "y" || $respuesta == "Y" ]]; then
command git add .
else
echo "Canceled"
fi
else
command git "$@"
fi
}
alias git='confirm_git_add'
3. Script completo
get_git_repo_branch() {
if git rev-parse --show-toplevel &>/dev/null; then
repo=$(basename "$(git rev-parse --show-toplevel)")
branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
# Verificar si hay cambios no confirmados
if git diff --quiet && git diff --cached --quiet; then
# No changes: display in green
printf ' \001\e[1;38;2;140;250;140m\002%s(%s)\001\e[0m\002' "$repo" "$branch"
else
# There are uncommitted changes: display in bold orange
printf ' \001\e[1;38;5;214m\002%s(%s)\001\e[0m\002' "$repo" "$branch"
fi
fi
}
# Colores en bold
BOLDGREEN="\[\e[1;32m\]" # verde bold
BOLDBLUE="\[\e[1;34m\]" # azul bold
BOLDORANGE="\[\e[1;38;5;214m\]" # naranja bold (256-color)
BOLDCYAN="\[\e[1;36m\]" # azul claro bold (cian)
RESET="\[\e[0m\]" # reset
# Prompt modificado para aplicar el color correspondiente
PS1="${BOLDCYAN}[\D{%m-%d} \A] ${RESET}${BOLDGREEN}\u@\h${RESET}:${BOLDBLUE}\w${RESET}\$(get_git_repo_branch)\n# "
# Protección contra 'git add .'
confirm_git_add() {
if [[ "$@" == "add ." ]]; then
echo -e "\033[1;33mAre you sure you want to run an 'add .'? (y/n):\033[0m"
read -n 1 respuesta
echo
if [[ $respuesta == "y" || $respuesta == "Y" ]]; then
command git add .
else
echo "Canceled"
fi
else
command git "$@"
fi
}
alias git='confirm_git_add'