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.
 
 
 
 
 
 

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