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

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