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