You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bashrc 11 KiB

13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #!/bin/bash
  2. ##########################
  3. # system type
  4. alias ismsys=false
  5. alias iscygwin=false
  6. alias isdarwin=false
  7. alias iswindows="iscygwin || ismsys"
  8. alias islinux="! iswindows && ! isdarwin" # i havent used unix yet
  9. case `uname` in
  10. (MINGW32*) alias ismsys=true ;;
  11. (CYGWIN*) alias iscygwin=true ;;
  12. (Darwin*) alias isdarwin=true ;;
  13. esac
  14. ##########################################
  15. null(){
  16. "$@" >/dev/null 2>&1
  17. }
  18. __try_exec(){
  19. type $1 >/dev/null 2>&1 && "$@"
  20. }
  21. export PS1="\$(__my_prompt_function)\$ "
  22. # PROMPT_COMMAND=prompt_function
  23. if false # iswindows
  24. then
  25. export PAGER='tr -d \\r | less'
  26. else
  27. export PAGER="less"
  28. fi
  29. if null type vim
  30. then
  31. export EDITOR=vim
  32. else
  33. export EDITOR=vi
  34. fi
  35. export VISUAL="$EDITOR"
  36. export LESS="-iRMX"
  37. export LC_MESSAGES=C
  38. export CDPATH=".:~"
  39. export GIT_PAGER="$PAGER"
  40. export GIT_EDITOR="$EDITOR"
  41. null type stty && {
  42. stty stop undef # unbind C-s to stop displaying output
  43. stty erase '^h'
  44. }
  45. if iswindows; then
  46. # export TMP=/tmp
  47. # export TEMP=/tmp
  48. # export PS1=" \[\e[32m\]\u@\H \[\e[33m\]\w\[\e[0m\] \d \t\n\s \# \j \$ "
  49. # export PS1=" [\[\e[33m\]\w\[\e[0m\]]\n\[\e[32m\]\u@\H\[\e[0m\] \d \t \s.\v\nhist:\# jobs:\j \$ "
  50. export USER=$USERNAME
  51. fi
  52. #######################
  53. uname -a
  54. if [ "${EMACS}" = "t" ]; then # for emacs shell
  55. true export PS1="\u@\H \d \t \w\nemacs shell\$ "
  56. elif echo "$EMACS" | grep term >/dev/null 2>&1; then # for emacs term
  57. echo "Emacs Term"
  58. fi
  59. # if test -f /etc/issue
  60. # then
  61. # cat /etc/issue
  62. # fi
  63. ###################################
  64. # some aliases and functions
  65. alias ls="ls -hCFG $(test "$TERM" == dumb || echo --color=auto\ )--time-style=long-iso"
  66. # alias ll="ls -l"
  67. # alias la="ls -A"
  68. # alias lla="ls -Al"
  69. # alias less=""
  70. alias vl=/usr/share/vim/vimcurrent/macros/less.sh
  71. alias em="emacs -nw"
  72. # alias apt-get="sudo apt-get"
  73. alias ut="ssh 6365454829@un001.ecc.u-tokyo.ac.jp"
  74. alias rand="echo \$RANDOM"
  75. alias xunp="file-roller -h"
  76. alias pacome="sudo \paco -D"
  77. alias psall="ps auxww"
  78. alias q=exit
  79. alias p="$PAGER"
  80. alias c=cat
  81. alias pcalc="python -i -c 'from math import *' "
  82. alias py3=python3
  83. alias _reloadrc="test -f ~/.bashrc && source ~/.bashrc"
  84. alias sudo="sudo " # use aliases through sudo
  85. alias e3=e3em
  86. alias mytime="date +%Y%m%d-%H%M%S"
  87. alias sh="ENV=$HOME/.shrc PS1=\$\ sh"
  88. alias halt="sudo halt"
  89. alias reboot="sudo reboot"
  90. # type trash >/dev/null 2>&1 && alias rm=trash
  91. alias aptin="apt-get install"
  92. alias aptsearch="apt-cache search"
  93. alias aptshow="apt-cache show"
  94. alias yt=yaourt
  95. null type pacman-color && alias pacman=pacman-color
  96. export PACMAN=pacman-color
  97. alias ubuntu-upgrade="sudo apt-get autoremove --yes && sudo apt-get update --yes && sudo apt-get upgrade --yes"
  98. alias arch-upgrade="yaourt -Syu"
  99. alias port-upgrade="port selfupdate && port sync && port upgrade installed"
  100. if iscygwin; then
  101. null type windate || alias windate="/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%'"
  102. alias cygsu="cygstart /cygwinsetup.exe"
  103. alias emacs="CYGWIN=tty emacs -nw"
  104. alias ls="ls -CFG $(test "$TERM" == dumb || echo --color=auto)"
  105. fi
  106. alias g=git
  107. if null type _git # enable programmable completion for g
  108. then
  109. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  110. || complete -o default -o nospace -F _git g
  111. fi
  112. showinfo(){
  113. echo "Japanese letters are 表示可能"
  114. __try_exec diskinfo
  115. ! isdarwin && test -n "${DISPLAY}" && {
  116. __try_exec xrandr | grep --color=never ^Screen
  117. }
  118. iswindows || __try_exec finger $USER
  119. LANG=C __try_exec id
  120. __try_exec xset q
  121. }
  122. x(){
  123. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  124. nohup startx >~/.xorg.log 2>&1 &
  125. else
  126. echo "X cant be started! Maybe another X is already started!" 1>&2
  127. fi
  128. }
  129. export __MYGITBAREREP="${HOME}/dbx/.git-bare"
  130. git-make-local-rep(){
  131. test $# -eq 0 && {
  132. echo "specify repository name." 1>&2
  133. return 1
  134. }
  135. dir="${__MYGITBAREREP}/$1.git"
  136. cdir=$PWD
  137. if test -d "$dir"
  138. then
  139. echo "dir $dir already exist!" 1>&2
  140. else
  141. mkdir -p "$dir" && {
  142. cd "$dir" &&
  143. git init --bare --shared=all
  144. }
  145. fi
  146. cd ${cdir}
  147. }
  148. bak(){
  149. for file in "$@"
  150. do
  151. mv -v ${file} ${file}.bak
  152. done
  153. }
  154. di(){
  155. if null type colordiff && test $TERM != dumb
  156. then
  157. local diffcmd=colordiff
  158. else
  159. local diffcmd=diff
  160. fi
  161. ${diffcmd} -u "$@" | ${PAGER}
  162. }
  163. throw-away(){
  164. mkdir -p ~/.backup/tb
  165. for file in "$@"
  166. do
  167. mv $file ~/.backup/tb
  168. done
  169. }
  170. mkcd(){
  171. mkdir -p $1
  172. cd $1
  173. }
  174. catclip(){
  175. if iswindows
  176. then
  177. cat /dev/clipboard | tr -d \\r
  178. else
  179. xclip -o -selection "clipboard"
  180. fi
  181. }
  182. setclip(){
  183. if iswindows
  184. then
  185. if test $# -eq 0
  186. then
  187. sed -e 's/$/\r/' | tee /dev/clipboard
  188. else
  189. cat "$@" | sed -e 's/$/\r/' | tee /dev/clipboard
  190. fi
  191. else
  192. if test $# -eq 0
  193. then
  194. xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  195. else
  196. cat "$@" | xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  197. fi
  198. fi
  199. }
  200. o(){
  201. if [ $# -eq 0 ]
  202. then
  203. local f=.
  204. else
  205. local f="$1"
  206. fi
  207. if iswindows
  208. then
  209. cmd.exe //c start "" "$f"
  210. elif isdarwin
  211. then
  212. open "$f"
  213. else
  214. xdg-open "$f"
  215. fi
  216. }
  217. convmv-sjis2utf8-test(){
  218. convmv -r -f sjis -t utf8 *
  219. }
  220. convmv-sjis2utf8-notest(){
  221. convmv -r -f sjis -t utf8 * --notest
  222. }
  223. _mygitconfig(){
  224. git config --global user.name '10sr'
  225. git config --global user.email '8slashes+git@gmail.com'
  226. git config --global core.autocrlf false
  227. git config --global color.ui auto
  228. git config --global status.relativePaths false
  229. git config --global status.showUntrackedFiles normal
  230. git config --global alias.graph "log --graph --date-order -C -M --pretty=format:\"<%h> %ad [%an] %Cgreen%d%Creset %s\" --all --date=short"
  231. git config --global alias.st "status -s"
  232. git config --global alias.b "branch"
  233. git config --global alias.ci "commit --verbose"
  234. git config --global alias.co "checkout"
  235. git config --global alias.cim "commit --verbose -m"
  236. git config --global alias.di "diff --color"
  237. git config --global alias.me "merge --no-ff --stat -v"
  238. git config --global alias.ls "ls-files -v --full-name"
  239. git config --global alias.sl "!sl"
  240. # git config --global alias.my-ls "ls-files | xargs ls"
  241. # git config --global alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  242. git config --global alias.addi "add -i"
  243. if iswindows; then
  244. git config --global core.fileMode false
  245. fi
  246. }
  247. __my_parse_svn_branch() {
  248. local LANG=C
  249. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  250. local svn_repository_root=$(svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p')
  251. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | awk '{print $1}'
  252. }
  253. __my_svn_ps1(){
  254. if svn status >/dev/null 2>&1
  255. then
  256. local svn_branch=$(__my_parse_svn_branch)
  257. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  258. fi
  259. }
  260. __my_prompt_function(){ # used by PS1
  261. local lastreturn=$?
  262. if test "${TERM}" == dumb
  263. then
  264. local c1=
  265. local c2=
  266. local c3=
  267. local cdef=
  268. else
  269. local c1="\e[33m"
  270. local c2="\e[36m"
  271. local c3="\e[37m"
  272. local cdef="\e[0m"
  273. fi
  274. if iswindows
  275. then
  276. local pwd=$PWD
  277. local oldpwd=$OLDPWD
  278. local jobnum=
  279. if git branch >/dev/null 2>&1
  280. then
  281. local git="[GIT]"
  282. else
  283. local git=
  284. fi
  285. local date=$(/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%')
  286. else
  287. local pwd=$(echo "${PWD}/" | sed -e "s#${HOME}#~#")
  288. local oldpwd=$(echo "${OLDPWD}/" | sed -e "s#${HOME}#~#")
  289. local jobnum=$(jobs | wc -l)
  290. local git=$(__try_exec __git_ps1 [GIT:%s])
  291. local date=$(LANG=C __try_exec date +"%a, %d %b %Y %T %z")
  292. fi
  293. # local svn=$(type svn >/dev/null 2>&1 && __try_exec __my_svn_ps1 [SVN:%s])
  294. test -f ~/.batterystatus && local battery="[Battery:$(cat ~/.batterystatus | sed -e 's`%`%%`g')]"
  295. local ip=$(ip-address [Addr:%s])
  296. local tty=$(__try_exec tty | sed -e 's:/dev/::')
  297. # local battery=$(battery-state [%s] | sed -e 's`%`%%`g') # very slow
  298. printf " [${c1}${pwd}${cdef}<${c3}${oldpwd}${cdef}]${git}${svn}${battery}${ip}\n"
  299. printf "${c2}${USER}@${HOSTNAME}${cdef} ${tty} ${date} ${BASH} ${BASH_VERSION}\n"
  300. printf "shlv:${SHLVL} jobs:${jobnum} last:${lastreturn} "
  301. }
  302. #Change ANSI Colors
  303. _chengecolors(){
  304. echo -e \
  305. "\e]P0000000" \
  306. "\e]P1cd0000" \
  307. "\e]P200cd00" \
  308. "\e]P3cdcd00" \
  309. "\e]P41e90ff" \
  310. "\e]P5cd00cd" \
  311. "\e]P600cdcd" \
  312. "\e]P7353535" \
  313. "\e]P8666666" \
  314. "\e]P9ff9999" \
  315. "\e]Pa99ff99" \
  316. "\e]Pbffff99" \
  317. "\e]Pc9999ff" \
  318. "\e]Pdff99ff" \
  319. "\e]Pe99ffff" \
  320. "\e]Pfffffff"
  321. }
  322. # printf "\e]P7353535" \
  323. _echocolors(){
  324. echo -e \
  325. "\e[30mBlack\n" \
  326. "\e[31mRed\n" \
  327. "\e[32mGreen\n" \
  328. "\e[33mYellow\n" \
  329. "\e[34mBlue\n" \
  330. "\e[35mMagenta\n" \
  331. "\e[36mCyan\n" \
  332. "\e[37mWhite\n" \
  333. "\e[30;1mBright Black\n" \
  334. "\e[31;1mBright Red\n" \
  335. "\e[32;1mBright Green\n" \
  336. "\e[33;1mBright Yellow\n" \
  337. "\e[34;1mBright Blue\n" \
  338. "\e[35;1mBright Magenta\n" \
  339. "\e[36;1mBright Cyan\n" \
  340. "\e[37;1mBright White\n" \
  341. "\e[0m"
  342. }
  343. # http://myminios.googlecode.com/svn-history/r10/trunk/colortable16.sh
  344. winln(){
  345. # for windose make link (actually junction)
  346. if test $# -eq 0
  347. then
  348. {
  349. echo "usage: winln TARGET LINK_NAME"
  350. echo "Create a link to TARGET with the name LINK_NAME (that is, TARGET must already exist)."
  351. echo "About other features run 'junction'."
  352. } 1>&2
  353. return 1
  354. else
  355. junction "$2" "$1"
  356. fi
  357. }
  358. battery-status(){
  359. local dir=/sys/class/power_supply/BAT0
  360. if test -d $dir
  361. then
  362. local st=$(cat $dir/status)
  363. local full=$(cat $dir/charge_full)
  364. local now=$(cat $dir/charge_now)
  365. local rate=$(expr $now \* 100 / $full)
  366. printf $1 "${st}:${rate}%"
  367. fi
  368. }
  369. alias bat='battery-status %s\\n'
  370. battery-status2(){
  371. local dir=/sys/class/power_supply/BAT0
  372. . $dir/uevent
  373. local rate=$(expr $POWER_SUPPLY_CHARGE_NOW \* 100 / $POWER_SUPPLY_CHARGE_FULL)
  374. echo ${POWER_SUPPLY_STATUS}:${rate}%
  375. }
  376. ip-address(){
  377. local ip=$(LANG=C ifconfig | grep "inet " | grep -v "127.0.0.1" | awk '{print $2}')
  378. test -n "$ip" && printf $1 $ip
  379. }