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.
 
 
 
 
 
 

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