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.
 
 
 
 
 
 

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