.bashrc 4.2 KB

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