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.
 
 
 
 
 
 

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