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 15 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. #!/bin/bash
  2. ##########################
  3. # system type
  4. alias ismsys=false
  5. alias iscygwin=false
  6. alias iswindows=false
  7. alias isdarwin=false
  8. alias islinux=false
  9. case `uname` in
  10. (MINGW*) alias ismsys=true ;;
  11. (CYGWIN*) alias iscygwin=true ;;
  12. (Darwin*) alias isdarwin=true ;;
  13. (Linux*) alias islinux=true ;;
  14. esac
  15. ( ismsys || iscygwin ) && alias iswindows=true
  16. ##########################################
  17. null(){
  18. "$@" >/dev/null 2>&1
  19. }
  20. __try_exec(){
  21. type $1 >/dev/null 2>&1 && "$@"
  22. }
  23. export PS1 # PS1 is defined later
  24. # PROMPT_COMMAND=prompt_function
  25. if false iswindows
  26. then
  27. export PAGER='tr -d \\r | less'
  28. else
  29. export PAGER="less"
  30. fi
  31. export LESS="-iRMXF"
  32. if null type vim
  33. then
  34. export EDITOR=vim
  35. else
  36. export EDITOR=vi
  37. fi
  38. export LC_MESSAGES=C
  39. # export CDPATH=".:~"
  40. export VISUAL="$EDITOR"
  41. export GIT_PAGER="$PAGER"
  42. export GIT_EDITOR="$EDITOR"
  43. ! iswindows && null type stty && {
  44. stty stop undef # unbind C-s to stop displaying output
  45. # stty erase '^h'
  46. }
  47. if iswindows; then
  48. # export TMP=/tmp
  49. # export TEMP=/tmp
  50. # export PS1=" \[\e[32m\]\u@\H \[\e[33m\]\w\[\e[0m\] \d \t\n\s \# \j \$ "
  51. # export PS1=" [\[\e[33m\]\w\[\e[0m\]]\n\[\e[32m\]\u@\H\[\e[0m\] \d \t \s.\v\nhist:\# jobs:\j \$ "
  52. export USER=$USERNAME
  53. fi
  54. mkdir -p "/tmp/${USER}-tmp"
  55. #######################
  56. iswindows && alias tty="echo cmd.exe"
  57. type fortune >/dev/null 2>&1 && {
  58. echo
  59. fortune
  60. echo
  61. fortune -o
  62. echo
  63. }
  64. uname -a
  65. echo TERM $TERM connected to $(tty), running $BASH $BASH_VERSION
  66. echo
  67. ###################################
  68. # some aliases and functions
  69. isdarwin || test "$TERM" == dumb || _coloroption=" --color=always"
  70. isdarwin || iswindows || _timeoption=" --time-style=long-iso"
  71. alias ls="ls -hCF${_coloroption}${_timeoption}"
  72. # export GREP_OPTIONS=""
  73. alias grep="grep -n${_coloroption}"
  74. iswindows && alias grep="grep -n"
  75. # alias ll="ls -l"
  76. # alias la="ls -A"
  77. # alias lla="ls -Al"
  78. # alias less=""
  79. alias em="emacs -nw"
  80. null type vim && alias vi=vim
  81. alias pstree="LANG=C pstree"
  82. alias cp="cp -v"
  83. alias mv="mv -v"
  84. alias psaux="ps auxww"
  85. alias q=exit
  86. alias e3=e3em
  87. alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  88. alias po=popd
  89. alias pu=pushd
  90. alias sudo="sudo " # use aliases through sudo
  91. alias halt="sudo halt"
  92. alias reboot="sudo reboot"
  93. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  94. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  95. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  96. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  97. alias rand="echo \$RANDOM"
  98. alias xunp="file-roller -h"
  99. alias pc="sudo \paco -D"
  100. alias pycalc="python -i -c 'from math import *' "
  101. alias py3=python3
  102. alias py2=python2
  103. alias _reloadrc="test -f ~/.bashrc && source ~/.bashrc"
  104. # alias mytime="date +%Y%m%d-%H%M%S"
  105. alias sh="ENV=$HOME/.shrc PS1=\$\ sh"
  106. # type trash >/dev/null 2>&1 && alias rm=trash
  107. alias mpg123="mpg123 -C -v --title"
  108. export PLAYER="mpg123 -C -v --title"
  109. alias screen="screen -e^z^z"
  110. alias pad=notepad
  111. null type gedit && alias pad=gedit
  112. null type leafpad && alias pad=leafpad
  113. alias wic=wicd-curses
  114. alias wil="wicd-cli -y -l | head"
  115. #alias wicn="wicd-cli -y -c -n"
  116. wicn(){
  117. if test $# -eq 0
  118. then
  119. local num
  120. wicd-cli -y -l | head
  121. echo -n "input num: "
  122. read num
  123. test -n "$num" && wicd-cli -y -c -n $num
  124. else
  125. wicd-cli -y -c -n $1
  126. fi
  127. }
  128. alias aptin="apt-get install"
  129. alias aptsearch="apt-cache search"
  130. alias aptshow="apt-cache show"
  131. for f in /usr/share/vim/vimcurrent/macros/less.sh \
  132. /usr/share/vim/vim73/macros/less.sh
  133. do
  134. test -f $f && alias vl=$f && break
  135. done
  136. alias yt=yaourt
  137. null type pacman-color && {
  138. alias pacman=pacman-color
  139. export pacman_program=pacman-color # used by pacmatic
  140. export PACMAN=pacman-color # used by yaourt
  141. }
  142. null type pacmatic && {
  143. alias pacman="pacmatic"
  144. export PACMAN="pacmatic"
  145. }
  146. alias ubuntu-upgrade="sudo apt-get autoremove --yes && sudo apt-get update --yes && sudo apt-get upgrade --yes"
  147. alias arch-upgrade="sudo pacman -Syu"
  148. alias port-upgrade="port selfupdate && port sync && port upgrade installed"
  149. if iscygwin; then
  150. null type windate || alias windate="/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%'"
  151. alias cygsu="cygstart /cygwinsetup.exe"
  152. alias emacs="CYGWIN=tty emacs -nw"
  153. alias ls="ls -CFG $(iswindows || test "$TERM" == dumb || echo --color=auto)"
  154. fi
  155. alias g=git
  156. if null type _git # enable programmable completion for g
  157. then
  158. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  159. || complete -o default -o nospace -F _git g
  160. fi
  161. __my_moc_state(){
  162. type mocp >/dev/null 2>&1 || return
  163. test "`mocp -Q %state 2>/dev/null`" == PLAY || return
  164. printf "$1" "`mocp -Q %title 2>/dev/null`"
  165. }
  166. mcrypt-stream(){
  167. test $# -eq 2 || return 1
  168. case $1 in
  169. en)
  170. mcrypt --key $2 | base64 ;;
  171. de)
  172. base64 -d | mcrypt -d --key $2 ;;
  173. esac
  174. }
  175. gpg-stream(){
  176. test $# -eq 2 || return 1
  177. case $1 in
  178. en)
  179. gpg --passphrase $2 -c --batch |base64 ;;
  180. de)
  181. base64 -d|gpg --passphrase $2 -d --batch ;;
  182. esac
  183. }
  184. alias enst="gpg-stream en"
  185. alias dest="gpg-stream de"
  186. showinfo(){
  187. echo "Japanese letters are 表示可能"
  188. __try_exec diskinfo
  189. ! isdarwin && test -n "${DISPLAY}" && {
  190. __try_exec xrandr | \grep --color=never ^Screen
  191. }
  192. iswindows || __try_exec finger $USER
  193. LANG=C __try_exec id
  194. __try_exec xset q
  195. }
  196. x(){
  197. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  198. #mkdir -p ~/.my/log
  199. # nohup startx >~/.my/log/xorg.log 2>&1 &
  200. # exit
  201. exec startx
  202. else
  203. echo "X cant be started! Maybe another X is already running or something." 1>&2
  204. fi
  205. }
  206. bak(){
  207. for file in "$@"
  208. do
  209. cp -v ${file} ${file}.bak
  210. done
  211. }
  212. di(){
  213. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  214. then
  215. local diffcmd=colordiff
  216. else
  217. local diffcmd=diff
  218. fi
  219. ${diffcmd} -u "$@" | ${PAGER}
  220. }
  221. tb(){
  222. local tb=~/.my/tb
  223. mkdir -p $tb
  224. for file in "$@"
  225. do
  226. mv $file $tb
  227. done
  228. }
  229. mkcd(){
  230. mkdir -p $1
  231. cd $1
  232. }
  233. catclip(){
  234. if iswindows
  235. then
  236. cat /dev/clipboard | tr -d \\r
  237. else
  238. xclip -o -selection "clipboard"
  239. fi
  240. }
  241. setclip(){
  242. if iswindows
  243. then
  244. if test $# -eq 0
  245. then
  246. sed -e 's/$/\r/' | tee /dev/clipboard
  247. else
  248. cat "$@" | sed -e 's/$/\r/' | tee /dev/clipboard
  249. fi
  250. else
  251. if test $# -eq 0
  252. then
  253. xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  254. else
  255. cat "$@" | xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  256. fi
  257. fi
  258. }
  259. if iswindows; then
  260. alias _open_file='cmd.exe //c start ""'
  261. elif isdarwin; then
  262. alias _open_file=open
  263. elif islinux; then
  264. if null type pcmanfm; then
  265. alias _open_file="LC_MESSAGES= pcmanfm"
  266. else
  267. alias _open_file="LC_MESSAGES= xdg-open"
  268. fi
  269. else
  270. alias _open_file=cat
  271. fi
  272. o(){
  273. if test $# -eq 0
  274. then
  275. _open_file . >/dev/null 2>&1 &
  276. else
  277. for f in "$@"
  278. do
  279. if test -d $f
  280. then
  281. _open_file $f >/dev/null 2>&1 &
  282. else
  283. _open_file $f >/dev/null 2>&1 &
  284. fi
  285. done
  286. fi
  287. }
  288. convmv-sjis2utf8-test(){
  289. convmv -r -f sjis -t utf8 *
  290. }
  291. convmv-sjis2utf8-notest(){
  292. convmv -r -f sjis -t utf8 * --notest
  293. }
  294. _my_git_config(){
  295. git config --global user.name '10sr'
  296. git config --global user.email '8slashes+git@gmail.com'
  297. git config --global core.autocrlf false
  298. git config --global core.excludesfile '~/.gitignore'
  299. git config --global color.ui auto
  300. git config --global status.relativePaths false
  301. git config --global status.showUntrackedFiles normal
  302. git config --global log.date iso
  303. git config --global alias.graph "log --graph --date-order -C -M --pretty=tformat:\"<%h> %ad [%an] %Cgreen%d%Creset %s\" --all --date=iso"
  304. git config --global alias.st "status -s -b"
  305. git config --global alias.b "branch"
  306. git config --global alias.ci "commit --verbose"
  307. git config --global alias.co "checkout"
  308. git config --global alias.cim "commit --verbose -m"
  309. git config --global alias.di "diff --color"
  310. git config --global alias.me "merge --no-ff --stat -v"
  311. git config --global alias.ls "ls-files -v --full-name"
  312. git config --global alias.sl "!sl"
  313. # git config --global alias.my-ls "ls-files | xargs ls"
  314. # git config --global alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  315. git config --global alias.addi "add -i"
  316. if iswindows; then
  317. git config --global core.fileMode false
  318. fi
  319. }
  320. __my_parse_svn_branch() {
  321. local LANG=C
  322. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  323. local svn_repository_root=$(svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p')
  324. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | awk '{print $1}'
  325. }
  326. __my_svn_ps1(){
  327. if svn status >/dev/null 2>&1
  328. then
  329. local svn_branch=$(__my_parse_svn_branch)
  330. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  331. fi
  332. }
  333. #Change ANSI Colors
  334. _chengecolors(){
  335. echo -e \
  336. "\e]P0000000" \
  337. "\e]P1cd0000" \
  338. "\e]P200cd00" \
  339. "\e]P3cdcd00" \
  340. "\e]P41e90ff" \
  341. "\e]P5cd00cd" \
  342. "\e]P600cdcd" \
  343. "\e]P7353535" \
  344. "\e]P8666666" \
  345. "\e]P9ff9999" \
  346. "\e]Pa99ff99" \
  347. "\e]Pbffff99" \
  348. "\e]Pc9999ff" \
  349. "\e]Pdff99ff" \
  350. "\e]Pe99ffff" \
  351. "\e]Pfffffff"
  352. }
  353. # printf "\e]P7353535" \
  354. _colors(){
  355. echo -e \
  356. "\e[30mBlack" \
  357. "\e[31mRed" \
  358. "\e[32mGreen" \
  359. "\e[33mYellow" \
  360. "\e[34mBlue" \
  361. "\e[35mMagenta" \
  362. "\e[36mCyan" \
  363. "\e[37mWhite"
  364. echo -e \
  365. "\e[30;1mBright Black" \
  366. "\e[31;1mBright Red" \
  367. "\e[32;1mBright Green" \
  368. "\e[33;1mBright Yellow" \
  369. "\e[34;1mBright Blue" \
  370. "\e[35;1mBright Magenta" \
  371. "\e[36;1mBright Cyan" \
  372. "\e[37;1mBright White\n" \
  373. "\e[0m"
  374. }
  375. winln(){
  376. # for windose make link (actually junction)
  377. if test $# -eq 0
  378. then
  379. {
  380. echo "usage: winln TARGET LINK_NAME"
  381. echo "Create a link to TARGET with the name LINK_NAME (that is, TARGET must already exist)."
  382. echo "About other features run 'junction'."
  383. } 1>&2
  384. return 1
  385. else
  386. junction "$2" "$1"
  387. fi
  388. }
  389. __my_battery_status(){
  390. local dir=/sys/class/power_supply/BAT0
  391. if test -d $dir
  392. then
  393. local st=$(cat $dir/status)
  394. local full=$(cat $dir/charge_full)
  395. local now=$(cat $dir/charge_now)
  396. local rate=$(expr $now \* 100 / $full)
  397. printf "$1" "${st}:${rate}%"
  398. fi
  399. }
  400. alias bat='__my_battery_status %s\\n'
  401. ip-address(){
  402. test type ifconfig >/dev/null 2>&1 || return 1
  403. local ip=$(LANG=C ifconfig | \grep --color=never "inet " | \grep --color=never -v "127.0.0.1" | awk '{print $2}')
  404. test -n "$ip" && printf $1 $ip
  405. }
  406. __my_ps1_moc(){
  407. local last=$?
  408. __my_moc_state "[MOC:%s]"
  409. return $last
  410. }
  411. __my_ps1_git(){
  412. local last=$?
  413. __try_exec __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  414. return $last
  415. }
  416. __my_ps1_ipaddr(){
  417. local last=$?
  418. test -z "$DISPLAY" && ! iswindows && ip-address [Addr:%s]
  419. return $last
  420. }
  421. __my_ps1_bttry(){
  422. local last=$?
  423. local bst="/tmp/${USER}-tmp/batterystatus"
  424. if test -z "$DISPLAY" && ! iswindows
  425. then
  426. test -f $bst && local bstr="$(cat $bst)"
  427. test -n "$bstr" && echo "[Battery:$bstr]"
  428. __my_battery_status %s >$bst &
  429. fi
  430. return $last
  431. }
  432. __my_ps1_dirs(){
  433. dirs | wc -l
  434. }
  435. __my_ps1_jobs(){
  436. jobs | wc -l
  437. }
  438. if test "$TERM" != dumb
  439. then
  440. __my_c1="\[\e[1;31m\]" # color for PWD
  441. __my_c2="\[\e[0;36m\]" # color for user
  442. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  443. __my_c4="\[\e[1;32m\]" # color for ::
  444. __my_cdef="\[\e[0m\]"
  445. fi
  446. _PS1="\
  447. ${__my_c4}:: ${__my_cdef}[${__my_c1}\w/${__my_cdef}<${__my_c3}\${OLDPWD}${__my_cdef}]\$(__my_ps1_git)\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\$(__my_ps1_moc)\n\
  448. ${__my_c4}:: ${__my_c2}\u@\H${__my_cdef} \D{%a, %d %b %Y %T %z} ${SHELL} \V\n\
  449. ${__my_c4}:: ${__my_cdef}shlv:${SHLVL} cnum:\# jobs:\j last:\$? \$ "
  450. PS1=$_PS1
  451. __my_set_title(){
  452. title="$(echo $@)"
  453. case $TERM in
  454. (rxvt*|xterm*|aterm|screen*)
  455. test -t 1 &&
  456. test -n "$DISPLAY" &&
  457. test -z "$EMACS" &&
  458. echo -n -e "\033]0;${title}\007"
  459. ;;
  460. esac
  461. }
  462. export PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\ \${PWD};"
  463. # copied from https://wiki.archlinux.org/index.php/X_resources
  464. invader(){
  465. # ANSI color scheme script featuring Space Invaders
  466. #
  467. # Original: http://crunchbanglinux.org/forums/post/126921/#p126921
  468. # Modified by lolilolicon
  469. #
  470. f=3 b=4
  471. for j in f b; do
  472. for i in {0..7}; do
  473. printf -v $j$i %b "\e[${!j}${i}m"
  474. done
  475. done
  476. bld=$'\e[1m'
  477. rst=$'\e[0m'
  478. cat << EOF
  479. $f1 ▀▄ ▄▀ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4 ▀▄ ▄▀ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  480. $f1 ▄█▀███▀█▄ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4 ▄█▀███▀█▄ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  481. $f1█▀███████▀█ $f2▀▀███▀▀███▀▀ $f3▀█▀██▀█▀ $f4█▀███████▀█ $f5▀▀███▀▀███▀▀ $f6▀█▀██▀█▀$rst
  482. $f1▀ ▀▄▄ ▄▄▀ ▀ $f2 ▀█▄ ▀▀ ▄█▀ $f3▀▄ ▄▀ $f4▀ ▀▄▄ ▄▄▀ ▀ $f5 ▀█▄ ▀▀ ▄█▀ $f6▀▄ ▄▀$rst
  483. $bld$f1▄ ▀▄ ▄▀ ▄ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4▄ ▀▄ ▄▀ ▄ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  484. $bld$f1█▄█▀███▀█▄█ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4█▄█▀███▀█▄█ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  485. $bld$f1▀█████████▀ $f2▀▀▀██▀▀██▀▀▀ $f3▀▀█▀▀█▀▀ $f4▀█████████▀ $f5▀▀▀██▀▀██▀▀▀ $f6▀▀█▀▀█▀▀$rst
  486. $bld$f1 ▄▀ ▀▄ $f2▄▄▀▀ ▀▀ ▀▀▄▄ $f3▄▀▄▀▀▄▀▄ $f4 ▄▀ ▀▄ $f5▄▄▀▀ ▀▀ ▀▀▄▄ $f6▄▀▄▀▀▄▀▄$rst
  487. $f7▌$rst
  488. $f7▌$rst
  489. $f7 ▄█▄ $rst
  490. $f7▄█████████▄$rst
  491. $f7▀▀▀▀▀▀▀▀▀▀▀$rst
  492. EOF
  493. }
  494. #/etc/lsb-release