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.
 
 
 
 
 
 

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