.bashrc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/bin/bash
  2. # add aliases with optimized settings
  3. alias ls='ls -vhFA --color=auto'
  4. alias grep='grep --color=auto'
  5. alias ..='cd ..'
  6. alias ...='cd ../..'
  7. alias sshfs='sshfs -o cache=yes,compression=yes,kernel_cache'
  8. # define pretty man pages colors
  9. man() {
  10. env \
  11. LESS_TERMCAP_md=$'\e[1;36m' \
  12. LESS_TERMCAP_me=$'\e[0m' \
  13. LESS_TERMCAP_se=$'\e[0m' \
  14. LESS_TERMCAP_so=$'\e[1;40;92m' \
  15. LESS_TERMCAP_ue=$'\e[0m' \
  16. LESS_TERMCAP_us=$'\e[1;32m' \
  17. man "$@"
  18. }
  19. # colors for enhanced prompt
  20. c_bold="\033[1m"
  21. c_red="\033[31m"
  22. c_green="\033[32m"
  23. c_blue="\033[34m"
  24. c_purple="\033[35m"
  25. c_cyan="\033[36m"
  26. c_sgr0="\033[0m"
  27. # promptify is an updated prompt enhancement enabled by default
  28. promptify() {
  29. local prompt_string
  30. prompt_string="${c_blue}${0} ($(date +'%R:%S.%3N'):${timer_show}s) ${c_purple}$(whoami)${c_sgr0}@${c_green}$(hostname) ${c_bold}${c_blue}$(dirs)${c_sgr0}"
  31. if git rev-parse --git-dir &> /dev/null; then
  32. git_branch=$(git branch 2> /dev/null | sed -n 's/^\*[ ]*//p')
  33. git_stats=$(git status --porcelain --untracked-files=all 2> /dev/null)
  34. if [ $(echo "${git_stats}" | sed '/^\s*$/d' | wc -l) -gt 0 ]; then
  35. prompt_string="${prompt_string} [${c_red}${git_branch}${c_sgr0}"
  36. untracked_count=$(echo "${git_stats}" | grep -ce "^??")
  37. new_file_count=$(echo "${git_stats}" | grep -ce "^A ")
  38. renamed_count=$(echo "${git_stats}" | grep -ce "^R ")
  39. modified_staged_count=$(echo "${git_stats}" | grep -ce "^M ")
  40. modified_unstaged_count=$(echo "${git_stats}" | grep -ce "^ M")
  41. deleted_staged_count=$(echo "${git_stats}" | grep -ce "^D ")
  42. deleted_unstaged_count=$(echo "${git_stats}" | grep -ce "^ D")
  43. [ $(($untracked_count + $new_file_count + $renamed_count + $modified_staged_count + $modified_unstaged_count + $deleted_staged_count + $deleted_unstaged_count)) -gt 0 ] && prompt_string="${prompt_string}:"
  44. [ $untracked_count -gt 0 ] && prompt_string="${prompt_string} ${c_red}u${untracked_count}${c_sgr0}"
  45. [ $new_file_count -gt 0 ] && prompt_string="${prompt_string} ${c_green}a${new_file_count}${c_sgr0}"
  46. [ $renamed_count -gt 0 ] && prompt_string="${prompt_string} ${c_green}r${renamed_count}${c_sgr0}"
  47. [ $modified_unstaged_count -gt 0 ] || [ $modified_staged_count -gt 0 ] && prompt_string="${prompt_string} ${c_red}m${c_sgr0}"
  48. [ $modified_unstaged_count -gt 0 ] && prompt_string="${prompt_string}${c_red}${modified_unstaged_count}${c_sgr0}" && [ $modified_staged_count -gt 0 ] && prompt_string="${prompt_string}/"
  49. [ $modified_staged_count -gt 0 ] && prompt_string="${prompt_string}${c_green}${modified_staged_count}${c_sgr0}"
  50. [ $deleted_unstaged_count -gt 0 ] || [ $deleted_staged_count -gt 0 ] && prompt_string="${prompt_string} ${c_red}d${c_sgr0}"
  51. [ $deleted_unstaged_count -gt 0 ] && prompt_string="${prompt_string}${c_red}${deleted_unstaged_count}${c_sgr0}" && [ $deleted_staged_count -gt 0 ] && prompt_string="${prompt_string}/"
  52. [ $deleted_staged_count -gt 0 ] && prompt_string="${prompt_string}${c_green}${deleted_staged_count}${c_sgr0}"
  53. prompt_string="${prompt_string}]"
  54. else
  55. prompt_string="${prompt_string} [${c_green}${git_branch}${c_sgr0}]"
  56. fi
  57. fi
  58. echo -ne "\n${prompt_string}\n "
  59. }
  60. # time every command
  61. timer_start() {
  62. timer=${timer:-$SECONDS}
  63. }
  64. timer_stop() {
  65. timer_show=$(($SECONDS - $timer))
  66. unset timer
  67. }
  68. trap 'timer_start' DEBUG
  69. # apply prompt enhancements
  70. PS2='continue-> '
  71. PS1='$(promptify)'
  72. if [ -z "$PROMPT_COMMAND" ]; then
  73. PROMPT_COMMAND="timer_stop"
  74. else
  75. PROMPT_COMMAND="$PROMPT_COMMAND; timer_stop"
  76. fi
  77. # load bash auto-completion and command-not-found
  78. . /usr/share/bash-completion/bash_completion
  79. . /usr/share/doc/pkgfile/command-not-found.bash
  80. # use shared agent to load ssh-keys; expects systemd to manage agent
  81. export SSH_AUTH_SOCK="$HOME/.ssh/socket"
  82. ssh-add -l &> /dev/null || ssh-add