profile-util 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. CYAN="\e[96m"
  2. GREEN="\e[92m"
  3. RED="\e[91m"
  4. RESET="\e[0m"
  5. SUCCESS() {
  6. if [[ $1 -eq 0 ]] || [[ $1 -gt 128 ]]; then
  7. echo -en $GREEN
  8. else
  9. echo -en $RED
  10. fi
  11. }
  12. CODE() { [[ $1 -eq 0 ]] || [[ $1 -gt 128 ]] || echo "(rc=$1) "; }
  13. export PS1='$(rc=$?; echo -e "\[$CYAN\]\D{%F %T} \[$(SUCCESS $rc)\]\u@\h:[\w] $(CODE $rc)\[$RESET\]$ ")'
  14. PATH="$HOME/usr/local/bin:$HOME/bin:$HOME/usr/bin:$PATH"
  15. export PATH
  16. export LC_ALL=en_US.UTF-8
  17. if [[ -z "$BASEPATH" ]]; then
  18. export BASEPATH="$PATH"
  19. fi
  20. alias reset-path="export PATH=\"$BASEPATH\""
  21. # Turn a number of symlink files into copies of their original
  22. # Usage:
  23. # detach file...
  24. detach() {
  25. for i; do [ -L "$i" ] && cp --remove-destination $(readlink "$i") "$i"; done
  26. }
  27. link-tools() {
  28. PATH=$HOME/tools/bin:$HOME/tools/usr/bin:$PATH
  29. LD_LIBRARY_PATH=$HOME/tools/lib:$HOME/tools/usr/lib:$LD_LIBRARY_PATH
  30. export PATH
  31. export LD_LIBRARY_PATH
  32. }
  33. unlink-tools() {
  34. PATH=$(echo $PATH | perl -pne "s/(?<=:)$HOME/tools.*?://g")
  35. LD_LIBRARY_PATH=$(echo $LD_LIBRARY_PATH | perl -pne "s/(?<=:)$HOME/tools.*?://g")
  36. export PATH
  37. export LD_LIBRARY_PATH
  38. }
  39. alias listall="list -A"
  40. alias disk-usage="ls -A | xargs du -shc | sort -h"
  41. alias reload="source ~/.bashrc"
  42. alias crontab="VIM_CRONTAB=true crontab"
  43. refreshterm() {
  44. clear
  45. uname -a
  46. cat /etc/motd
  47. echo ""
  48. pwd
  49. list
  50. }
  51. _mkcd_fun() {
  52. if [ ! -d "$2" ]; then
  53. if [ -e "$2" ]; then
  54. echo "mkcd: file exists and is not a directory"
  55. exit
  56. else
  57. $1 "$2"
  58. fi
  59. fi
  60. cd "$2"
  61. }
  62. alias mkcd="_mkcd_fun mkdir"
  63. mkdirs() {
  64. mkdir -p "$1"
  65. }
  66. alias mkcds="_mkcd_fun mkdirs"
  67. alias diffr="diff --ignore-blank-lines --minimal --side-by-side --report-identical-files --suppress-common-lines"
  68. alias diffl="diff --changed-group-format='%<' --unchanged-group-format=''"
  69. _recall_fun() {
  70. if [ -z "$1" ]; then echo "recall: usage: recall string"; return 1; fi
  71. history | cut -c 8- | grep --extended-regexp "$1" | \
  72. grep --invert-match --extended-regexp "^(history|recall|redo)" | sort | uniq
  73. }
  74. redo() {
  75. IFS=$'\n'
  76. hist=($(_recall_fun $1))
  77. for ((i=0; i<${#hist[@]}; i++));
  78. do
  79. echo "[$i] ${hist[$i]}"
  80. done
  81. printf "Index: "
  82. read idx
  83. if [[ $idx =~ ^[0-9]+$ ]];
  84. then
  85. eval "${hist[$idx]}"
  86. fi
  87. unset IFS
  88. }
  89. recall(){
  90. _recall_fun $1 | sed 's/^/ /'
  91. }
  92. clear() {
  93. /usr/bin/clear
  94. if [[ ! -z "${TMUX}" ]]; then tmux clear-history; fi
  95. }
  96. swap() {
  97. if [[ $# -ne 2 ]]; then
  98. echo "swap file1 file2" >&2
  99. return 1
  100. fi
  101. local tmpfile=$(mktemp)
  102. mv "${1}" "${tmpfile}"
  103. mv "${2}" "${1}"
  104. mv "${tmpfile}" "${2}"
  105. }
  106. has() {
  107. [[ "$(type -t "${1}")" =~ function|alias|file ]]
  108. }
  109. maybe_do() {
  110. has "${1}" && "$@"
  111. }
  112. alias coms="${HOME}/tools/comstore/commands.sh"
  113. clrun() {
  114. clear
  115. echo "$@"
  116. "$@"
  117. }
  118. if [[ $(uname) == "Darwin" ]]; then # OSX / FreeBSD
  119. . ${HOME}/.profile-bsd
  120. else
  121. . ${HOME}/.profile-linux
  122. fi
  123. has git && . ${HOME}/.profile-git
  124. has p4 && . ${HOME}/.profile-p4