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.
 
 
 
 
 
 

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