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.
 
 
 
 
 
 

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