687 lines
17 KiB

  1. #!/bin/bash
  2. # If not running interactively, don't do anything
  3. [[ $- != *i* ]] && return
  4. ##########################################
  5. null(){
  6. "$@" >/dev/null 2>&1
  7. }
  8. __try_exec(){
  9. type $1 >/dev/null 2>&1 && "$@"
  10. }
  11. ##########################
  12. # system type
  13. alias ismsys=false
  14. alias iscygwin=false
  15. alias iswindows=false
  16. alias isdarwin=false
  17. alias islinux=false
  18. alias with_coreutils=false # for mac
  19. case `uname` in
  20. (MINGW*) alias ismsys=true ;;
  21. (CYGWIN*) alias iscygwin=true ;;
  22. (Darwin*) alias isdarwin=true ;;
  23. (Linux*) alias islinux=true ;;
  24. esac
  25. null ls --version && alias with_coreutils=true
  26. ( ismsys || iscygwin ) && alias iswindows=true
  27. #################################
  28. if false iswindows
  29. then
  30. export PAGER='tr -d \\r | less'
  31. else
  32. export PAGER="less"
  33. fi
  34. export LESS="-iRMXF"
  35. if null type vim
  36. then
  37. export EDITOR=vim
  38. else
  39. export EDITOR=vi
  40. fi
  41. export LANG=ja_JP.UTF-8
  42. export LC_MESSAGES=C
  43. # export CDPATH=".:~"
  44. export VISUAL="$EDITOR"
  45. export GIT_PAGER="$PAGER"
  46. export GIT_EDITOR="$EDITOR"
  47. echo "$TERM" | grep '^screen' >/dev/null 2>&1 || export TERM_ORIG=$TERM
  48. test "$TERM" = screen && test "$TERM_ORIG" = xterm-256color && TERM=screen-256color
  49. test -z "$TMP" && export TMP=/tmp/${USER}-tmp
  50. mkdir -p "$TMP"
  51. ! iswindows && null type stty && {
  52. stty stop undef # unbind C-s to stop displaying output
  53. # stty erase '^h'
  54. }
  55. if iswindows; then
  56. # export PS1=" \[\e[32m\]\u@\H \[\e[33m\]\w\[\e[0m\] \d \t\n\s \# \j \$ "
  57. # export PS1=" [\[\e[33m\]\w\[\e[0m\]]\n\[\e[32m\]\u@\H\[\e[0m\] \d \t \s.\v\nhist:\# jobs:\j \$ "
  58. export USER=$USERNAME
  59. fi
  60. _tmux_prefs(){
  61. null type tmux || return 1
  62. tmux set -g mode-keys vi
  63. }
  64. #######################
  65. iswindows && alias tty="echo cmd.exe"
  66. type fortune >/dev/null 2>&1 && {
  67. echo
  68. fortune
  69. echo
  70. fortune -o
  71. echo
  72. }
  73. uname -a
  74. echo TERM $TERM $(tput colors) colors connected to $(tty), running $BASH $BASH_VERSION
  75. echo
  76. ###################################
  77. # some aliases and functions
  78. ( ! with_coreutils && isdarwin ) || test "$TERM" == dumb || _coloroption=" --color=auto"
  79. ( ! with_coreutils && isdarwin ) || iswindows || _timeoption=" --time-style=long-iso"
  80. ( ! with_coreutils && isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
  81. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  82. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  83. alias ls="ls -hCF${_coloroption}${_timeoption}"
  84. # export GREP_OPTIONS=""
  85. alias gr="grep -n${_coloroption}"
  86. iswindows && alias grep="grep -n"
  87. # alias ll="ls -l"
  88. # alias la="ls -A"
  89. # alias lla="ls -Al"
  90. # alias less=""
  91. alias em="emacs -nw"
  92. null type vim && alias vi=vim
  93. alias pstree="LANG=C pstree"
  94. alias cp="cp -v"
  95. alias mv="mv -v"
  96. alias psaux="ps auxww"
  97. alias q=exit
  98. alias e3=e3em
  99. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  100. alias po=popd
  101. alias pu=pushd
  102. alias sudo="sudo " # use aliases through sudo
  103. alias halt="sudo halt"
  104. alias reboot="sudo reboot"
  105. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  106. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  107. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  108. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  109. alias rand="echo \$RANDOM"
  110. alias xunp="file-roller -h"
  111. alias pc="sudo \paco -D"
  112. alias pycalc="python -i -c 'from math import *' "
  113. alias py3=python3
  114. alias py2=python2
  115. alias _reloadrc="test -f ~/.bashrc && source ~/.bashrc"
  116. # alias mytime="date +%Y%m%d-%H%M%S"
  117. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  118. # type trash >/dev/null 2>&1 && alias rm=trash
  119. alias mpg123="mpg123 -C -v --title"
  120. export PLAYER="mpg123 -C -v --title"
  121. null type screen && alias screen="screen -e^z^z"
  122. alias zcd="cd \`zenity --file-selection --directory\`"
  123. null type gtags && alias gtags="gtags --verbose"
  124. null type htags && alias htags="htags --xhtml --symbol --line-number \
  125. --frame --alphabet --verbose"
  126. alias pad=notepad
  127. null type gedit && alias pad=gedit
  128. null type leafpad && alias pad=leafpad
  129. isdarwin && alias pad="open -e"
  130. alias wic=wicd-curses
  131. alias wil="wicd-cli -y -l | head"
  132. #alias wicn="wicd-cli -y -c -n"
  133. wicn(){
  134. if test $# -eq 0
  135. then
  136. local num
  137. wicd-cli -y -l | head
  138. echo -n "input num: "
  139. read num
  140. test -n "$num" && wicd-cli -y -c -n $num
  141. else
  142. wicd-cli -y -c -n $1
  143. fi
  144. }
  145. alias aptin="apt-get install"
  146. alias aptsearch="apt-cache search"
  147. alias aptshow="apt-cache show"
  148. for f in /usr/share/vim/vimcurrent/macros/less.sh \
  149. /usr/share/vim/vim73/macros/less.sh \
  150. /usr/share/vim/vim72/macros/less.sh
  151. do
  152. test -f $f && alias vl=$f && break
  153. done
  154. alias yt=yaourt
  155. null type pacman-color && {
  156. alias pacman=pacman-color
  157. export pacman_program=pacman-color # used by pacmatic
  158. export PACMAN=pacman-color # used by yaourt
  159. }
  160. null type pacmatic && {
  161. alias pacman="pacmatic"
  162. export PACMAN="pacmatic"
  163. }
  164. null type apt-get && \
  165. alias aupgrade="sudo apt-get autoremove --yes && sudo apt-get update --yes && sudo apt-get upgrade --yes"
  166. null type port && \
  167. alias port="port -v"
  168. alias pupgrade="sudo port -v selfupdate && { sudo port -v upgrade outdated; }"
  169. if iscygwin; then
  170. null type windate || alias windate="/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%'"
  171. alias cygsu="cygstart /cygwinsetup.exe"
  172. alias emacs="CYGWIN=tty emacs -nw"
  173. alias ls="ls -CFG $(iswindows || test "$TERM" == dumb || echo --color=auto)"
  174. fi
  175. alias g=git
  176. if null type _git # enable programmable completion for g
  177. then
  178. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  179. || complete -o default -o nospace -F _git g
  180. fi
  181. alias setup.py="sudo python3 setup.py install --record files.txt"
  182. scr(){
  183. _time="%Y-%m-%dT%H:%M:%S%z"
  184. script `date +${_time}`.script "$@"
  185. }
  186. netwait(){
  187. while ! ping -c 1 -t 1 example.com
  188. do
  189. true
  190. done
  191. echo network works.
  192. }
  193. cd(){
  194. if test $# -eq 0
  195. then
  196. pushd ~/ >/dev/null
  197. elif test $1 = -
  198. then
  199. local pwd="$PWD"
  200. command cd $OLDPWD
  201. pushd -n "$pwd" >/dev/null # stack last dir
  202. elif ! test -d "$1"
  203. then
  204. echo `basename ${SHELL}`: cd: "$1": No such file or directory 1>&2
  205. return 1
  206. else
  207. pushd "$1" >/dev/null
  208. fi
  209. __dirs_rm_dup "$PWD"
  210. echo "$PWD"
  211. }
  212. __dirs_rm_dup(){
  213. for d in "$@"
  214. do
  215. local next="$(__realpath --strip "$d")"
  216. for l in $(\dirs -v -l | cut -d "
  217. " -f 2- | \grep -x " *[0-9]\+ \+${next}" | \grep -o "^ *[0-9]\+ " | tac)
  218. do
  219. popd +$l -n >/dev/null
  220. done
  221. done
  222. }
  223. __realpath(){
  224. if type realpath >/dev/null 2>&1
  225. then
  226. command realpath "$@"
  227. else
  228. while ! test -d $1
  229. do
  230. shift
  231. done
  232. local d="$OLDPWD"
  233. command cd "$1"
  234. echo "$PWD"
  235. command cd "$d"
  236. fi
  237. }
  238. dh(){
  239. if test $# -eq 0
  240. then
  241. dirs -v -l
  242. else
  243. local dir="$(dirs -v -l | \grep "^ *$1 \+" | sed "s/^ *[0-9]* *//g")"
  244. cd "$dir"
  245. fi
  246. }
  247. input(){
  248. local foo
  249. stty -echo
  250. read foo
  251. stty echo
  252. echo $foo
  253. }
  254. tmux(){
  255. if test $# -eq 0
  256. then
  257. (cd ~; command tmux start;)
  258. if command tmux has -t main
  259. then
  260. command tmux attach -t main
  261. else
  262. command tmux new -s main
  263. fi
  264. else
  265. command tmux "$@"
  266. fi
  267. }
  268. mcrypt-stream(){
  269. test $# -eq 2 || return 1
  270. case $1 in
  271. en)
  272. mcrypt --key $2 | base64 ;;
  273. de)
  274. base64 -d | mcrypt -d --key $2 ;;
  275. esac
  276. }
  277. gpg-stream(){
  278. test $# -eq 2 || return 1
  279. case $1 in
  280. en)
  281. gpg --passphrase $2 -c --batch |base64 ;;
  282. de)
  283. base64 -d|gpg --passphrase $2 -d --batch ;;
  284. esac
  285. }
  286. alias enst="gpg-stream en"
  287. alias dest="gpg-stream de"
  288. showinfo(){
  289. echo "Japanese letters are 表示可能"
  290. __try_exec diskinfo
  291. ! isdarwin && test -n "${DISPLAY}" && {
  292. __try_exec xrandr | \grep --color=never ^Screen
  293. }
  294. iswindows || __try_exec finger $USER
  295. LANG=C __try_exec id
  296. __try_exec xset q
  297. }
  298. x(){
  299. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  300. #mkdir -p ~/.my/log
  301. # nohup startx >~/.my/log/xorg.log 2>&1 &
  302. # exit
  303. exec startx
  304. else
  305. echo "X cant be started! Maybe another X is already running or something." 1>&2
  306. fi
  307. }
  308. bak(){
  309. for file in "$@"
  310. do
  311. cp -v ${file} ${file}.bak
  312. done
  313. }
  314. di(){
  315. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  316. then
  317. local diffcmd=colordiff
  318. else
  319. local diffcmd=diff
  320. fi
  321. ${diffcmd} -u "$@" | ${PAGER}
  322. }
  323. tb(){
  324. local tb="$HOME/.my/tb"
  325. mkdir -p "$tb"
  326. for file in "$@"
  327. do
  328. mv -t "$tb" "$file"
  329. done
  330. }
  331. mkcd(){
  332. mkdir -p $1
  333. cd $1
  334. }
  335. catclip(){
  336. if iswindows
  337. then
  338. cat /dev/clipboard | tr -d \\r
  339. elif isdarwin
  340. then
  341. pbpaste
  342. else
  343. xclip -o -selection "clipboard"
  344. fi
  345. }
  346. setclip(){
  347. if iswindows
  348. then
  349. if test $# -eq 0
  350. then
  351. sed -e 's/$/\r/' | tee /dev/clipboard
  352. else
  353. cat "$@" | sed -e 's/$/\r/' | tee /dev/clipboard
  354. fi
  355. elif isdarwin
  356. then
  357. if test $# -eq 0
  358. then
  359. pbcopy
  360. else
  361. cat "$@" | pbcopy
  362. fi
  363. else
  364. if test $# -eq 0
  365. then
  366. xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  367. else
  368. cat "$@" | xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  369. fi
  370. fi
  371. }
  372. open_file(){
  373. if iswindows
  374. then
  375. cmd.exe //c start "" "$@"
  376. elif isdarwin
  377. then
  378. touch "$@"
  379. open "$@"
  380. elif islinux
  381. then
  382. touch "$@"
  383. if null type pcmanfm; then
  384. LC_MESSAGES= pcmanfm "$@"
  385. else
  386. LC_MESSAGES= xdg-open "$@" &
  387. fi
  388. else
  389. cat "$@"
  390. fi
  391. }
  392. o(){
  393. if test $# -eq 0
  394. then
  395. open_file .
  396. else
  397. for f in "$@"
  398. do
  399. open_file "$(realpath "$f")"
  400. done
  401. fi
  402. }
  403. convmv-sjis2utf8-test(){
  404. convmv -r -f sjis -t utf8 *
  405. }
  406. convmv-sjis2utf8-notest(){
  407. convmv -r -f sjis -t utf8 * --notest
  408. }
  409. __my_moc_state(){
  410. type mocp >/dev/null 2>&1 || return
  411. test "`mocp -Q %state 2>/dev/null`" == PLAY || return
  412. printf "$1" "`mocp -Q %title 2>/dev/null`"
  413. }
  414. __my_parse_svn_branch() {
  415. local LANG=C
  416. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  417. local svn_repository_root=$(svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p')
  418. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | awk '{print $1}'
  419. }
  420. __my_svn_ps1(){
  421. if svn status >/dev/null 2>&1
  422. then
  423. local svn_branch=$(__my_parse_svn_branch)
  424. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  425. fi
  426. }
  427. #Change ANSI Colors
  428. _chengecolors(){
  429. echo -e \
  430. "\e]P0000000" \
  431. "\e]P1cd0000" \
  432. "\e]P200cd00" \
  433. "\e]P3cdcd00" \
  434. "\e]P41e90ff" \
  435. "\e]P5cd00cd" \
  436. "\e]P600cdcd" \
  437. "\e]P7353535" \
  438. "\e]P8666666" \
  439. "\e]P9ff9999" \
  440. "\e]Pa99ff99" \
  441. "\e]Pbffff99" \
  442. "\e]Pc9999ff" \
  443. "\e]Pdff99ff" \
  444. "\e]Pe99ffff" \
  445. "\e]Pfffffff"
  446. }
  447. # printf "\e]P7353535" \
  448. _colors(){
  449. echo -e \
  450. "\e[30mBlack" \
  451. "\e[31mRed" \
  452. "\e[32mGreen" \
  453. "\e[33mYellow" \
  454. "\e[34mBlue" \
  455. "\e[35mMagenta" \
  456. "\e[36mCyan" \
  457. "\e[37mWhite"
  458. echo -e \
  459. "\e[30;1mBright Black" \
  460. "\e[31;1mBright Red" \
  461. "\e[32;1mBright Green" \
  462. "\e[33;1mBright Yellow" \
  463. "\e[34;1mBright Blue" \
  464. "\e[35;1mBright Magenta" \
  465. "\e[36;1mBright Cyan" \
  466. "\e[37;1mBright White\n" \
  467. "\e[0m"
  468. }
  469. winln(){
  470. # for windose make link (actually junction)
  471. if test $# -eq 0
  472. then
  473. {
  474. echo "usage: winln TARGET LINK_NAME"
  475. echo "Create a link to TARGET with the name LINK_NAME (that is, TARGET must already exist)."
  476. echo "About other features run 'junction'."
  477. } 1>&2
  478. return 1
  479. else
  480. junction "$2" "$1"
  481. fi
  482. }
  483. __my_battery_status(){
  484. local dir=/sys/class/power_supply/BAT0
  485. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && test -r $dir/charge_now
  486. then
  487. local st=$(cat $dir/status)
  488. local full=$(cat $dir/charge_full)
  489. local now=$(cat $dir/charge_now)
  490. local rate=$(expr $now \* 100 / $full)
  491. printf "$1" "${st}:${rate}%"
  492. fi
  493. }
  494. alias bat='__my_battery_status %s\\n'
  495. ip-address(){
  496. type ip >/dev/null 2>&1 || return 1
  497. local ip=$(LANG=C ip addr show scope global | \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  498. test -n "$ip" && printf $1 $ip
  499. }
  500. __my_ps1_scale(){
  501. local last=$?
  502. echo "[LC:${LINES}x${COLUMNS}]"
  503. return $last
  504. }
  505. __my_ps1_tmux(){
  506. local last=$?
  507. null type tmux || return $last
  508. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  509. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  510. return $last
  511. }
  512. __my_ps1_moc(){
  513. local last=$?
  514. __my_moc_state "[MOC:%s]"
  515. return $last
  516. }
  517. for f in /usr/share/git/git-prompt.sh \
  518. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  519. do
  520. test -r $f && . $f && break
  521. done
  522. GIT_PS1_SHOWDIRTYSTATE=t
  523. GIT_PS1_SHOWUPSTREAM=t
  524. __my_ps1_git(){
  525. local last=$?
  526. null type __git_ps1 || return $last
  527. null __gitdir || return $last
  528. __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  529. return $last
  530. }
  531. __my_ps1_ipaddr(){
  532. local last=$?
  533. ! iswindows && ip-address [Addr:%s]
  534. return $last
  535. }
  536. __my_ps1_bttry(){
  537. local last=$?
  538. local bst="${TMP}/batterystatus"
  539. if test -z "$DISPLAY" && ! iswindows
  540. then
  541. test -f $bst && local bstr="$(cat $bst)"
  542. test -n "$bstr" && echo "[Battery:$bstr]"
  543. __my_battery_status %s >$bst &
  544. fi
  545. return $last
  546. }
  547. __my_ps1_dirs(){
  548. dirs | wc -l
  549. }
  550. __my_ps1_jobs(){
  551. jobs | wc -l
  552. }
  553. if test "$TERM" != dumb
  554. then
  555. __my_c1="\[\e[1;31m\]" # color for PWD
  556. __my_c2="\[\e[0;36m\]" # color for user
  557. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  558. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  559. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;33m\]"
  560. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  561. else __my_c4="\[\e[1;32m\]" # color for ::
  562. fi
  563. __my_cdef="\[\e[0m\]"
  564. fi
  565. _PS1="\
  566. ${__my_c4}:: ${__my_cdef}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_scale)\$(__my_ps1_tmux)\$(__my_ps1_git)\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\$(__my_ps1_moc)\n\
  567. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$? \D{%T} \$ "
  568. PS1=$_PS1
  569. __my_set_title(){
  570. title="$(echo $@)"
  571. case $TERM in
  572. (rxvt*|xterm*|aterm|screen*)
  573. test -t 1 &&
  574. test -n "$DISPLAY" &&
  575. test -z "$EMACS" &&
  576. echo -n -e "\033]0;${title}\007"
  577. ;;
  578. esac
  579. }
  580. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\ \${PWD}"
  581. # copied from https://wiki.archlinux.org/index.php/X_resources
  582. invader(){
  583. # ANSI color scheme script featuring Space Invaders
  584. #
  585. # Original: http://crunchbanglinux.org/forums/post/126921/#p126921
  586. # Modified by lolilolicon
  587. #
  588. f=3 b=4
  589. for j in f b; do
  590. for i in {0..7}; do
  591. printf -v $j$i %b "\e[${!j}${i}m"
  592. done
  593. done
  594. bld=$'\e[1m'
  595. rst=$'\e[0m'
  596. cat << EOF
  597. $f1 ▀▄ ▄▀ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4 ▀▄ ▄▀ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  598. $f1 ▄█▀███▀█▄ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4 ▄█▀███▀█▄ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  599. $f1█▀███████▀█ $f2▀▀███▀▀███▀▀ $f3▀█▀██▀█▀ $f4█▀███████▀█ $f5▀▀███▀▀███▀▀ $f6▀█▀██▀█▀$rst
  600. $f1▀ ▀▄▄ ▄▄▀ ▀ $f2 ▀█▄ ▀▀ ▄█▀ $f3▀▄ ▄▀ $f4▀ ▀▄▄ ▄▄▀ ▀ $f5 ▀█▄ ▀▀ ▄█▀ $f6▀▄ ▄▀$rst
  601. $bld$f1▄ ▀▄ ▄▀ ▄ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4▄ ▀▄ ▄▀ ▄ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  602. $bld$f1█▄█▀███▀█▄█ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4█▄█▀███▀█▄█ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  603. $bld$f1▀█████████▀ $f2▀▀▀██▀▀██▀▀▀ $f3▀▀█▀▀█▀▀ $f4▀█████████▀ $f5▀▀▀██▀▀██▀▀▀ $f6▀▀█▀▀█▀▀$rst
  604. $bld$f1 ▄▀ ▀▄ $f2▄▄▀▀ ▀▀ ▀▀▄▄ $f3▄▀▄▀▀▄▀▄ $f4 ▄▀ ▀▄ $f5▄▄▀▀ ▀▀ ▀▀▄▄ $f6▄▀▄▀▀▄▀▄$rst
  605. $f7▌$rst
  606. $f7▌$rst
  607. $f7 ▄█▄ $rst
  608. $f7▄█████████▄$rst
  609. $f7▀▀▀▀▀▀▀▀▀▀▀$rst
  610. EOF
  611. }
  612. #/etc/lsb-release