Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

1095 Zeilen
25 KiB

  1. #!/bin/bash
  2. # TODO: use tput
  3. # If not running interactively, don't do anything
  4. [[ $- != *i* ]] && return
  5. ##########################################
  6. null(){
  7. "$@" >/dev/null 2>&1
  8. }
  9. __try_exec(){
  10. type $1 >/dev/null 2>&1 && "$@"
  11. }
  12. ##########################
  13. # system type
  14. alias ismsys=false
  15. alias iscygwin=false
  16. alias iswindows=false
  17. alias isdarwin=false
  18. alias islinux=false
  19. alias with_coreutils=false # for mac
  20. case `uname` in
  21. (MINGW*) alias ismsys=true ;;
  22. (CYGWIN*) alias iscygwin=true ;;
  23. (Darwin*) alias isdarwin=true ;;
  24. (Linux*) alias islinux=true ;;
  25. esac
  26. null ls --version && alias with_coreutils=true
  27. ( ismsys || iscygwin ) && alias iswindows=true
  28. alias inbash=false
  29. alias inzsh=false
  30. if test -n "$BASH_VERSION"
  31. then
  32. alias inbash=true
  33. elif test -n "$ZSH_VERSION"
  34. then
  35. alias inzsh=true
  36. fi
  37. #################################
  38. __match(){
  39. # __match str1 str2
  40. # return 0 if str2 is found in str1
  41. expr "$1" : ".*$2.*" >/dev/null
  42. }
  43. if ! __match "$PATH" "$HOME/.local/bin"
  44. then
  45. if test -d "$HOME/.local/bin"
  46. then
  47. PATH="${HOME}/.local/bin:${PATH}"
  48. fi
  49. if test -d "$HOME/.local/lib/gems/bin"
  50. then
  51. PATH="$HOME/.local/lib/gems/bin:${PATH}"
  52. fi
  53. fi
  54. if ismsys && ! __match "$PATH" /c/mingw/bin
  55. then
  56. PATH="$PATH:/c/mingw/bin:/c/mingw/msys/1.0/bin"
  57. fi
  58. # # it is not so good
  59. # # http://archive.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/shared-libraries.html
  60. # # http://superuser.com/questions/324613/installing-a-library-locally-in-home-directory-but-program-doesnt-recognize-it
  61. # without this ENV i cannot run tmux. another way is to use --disable-shared
  62. # when building tmux
  63. if ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
  64. then
  65. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/.local/lib"
  66. fi
  67. ##################################
  68. export LANG=ja_JP.UTF-8
  69. export LC_MESSAGES=C
  70. export TERMCAP="${TERMCAP}:vb="
  71. ismsys && export HOSTNAME
  72. export ENV=~/.shrc
  73. if false iswindows
  74. then
  75. export PAGER='tr -d \\r | less'
  76. else
  77. export PAGER="less"
  78. fi
  79. export LESS="-iRMX"
  80. # _src_hilite_lp_path="`which src-hilite-lesspipe.sh 2>/dev/null`"
  81. for f in /usr/share/source-highlight/src-hilite-lesspipe.sh
  82. do
  83. test -z "$_src_hilite_lp_path" && test -e "$f" && _src_hilite_lp_path="$f"
  84. done
  85. test -n "$_src_hilite_lp_path" && export LESSOPEN="| $_src_hilite_lp_path %s"
  86. if null type vim
  87. then
  88. export EDITOR=vim
  89. else
  90. export EDITOR=vi
  91. fi
  92. # export CDPATH=".:~"
  93. export VISUAL="$EDITOR"
  94. export GIT_PAGER="less -FS"
  95. export GIT_EDITOR="$EDITOR"
  96. export GIT_MERGE_AUTOEDIT=no
  97. if test -n "$TMUX" && \
  98. echo $TERM | grep screen >/dev/null 2>&1 && \
  99. tmux display -p '#{client_termname}' | grep 256color >/dev/null 2>&1
  100. then
  101. TERM=screen-256color
  102. fi
  103. if test -z "$TMP"
  104. then
  105. if test -n "$TMPDIR"
  106. then
  107. export TMP=$TMPDIR
  108. elif test -n "$TEMP"
  109. then
  110. export TMP="$TEMP"
  111. else
  112. export TMP=/tmp
  113. fi
  114. fi
  115. __match "$TMP" "${USER}-tmp" >/dev/null || export TMP="${TMP}/${USER}-tmp"
  116. export TEMP="$TMP"
  117. mkdir -p "$TMP"
  118. ! iswindows && null type stty && {
  119. stty stop undef # unbind C-s to stop displaying output
  120. # stty erase '^h'
  121. }
  122. if iswindows; then
  123. export USER=$USERNAME
  124. fi
  125. _tmux_prefs(){
  126. null type tmux || return 1
  127. tmux set -g mode-keys vi
  128. }
  129. if test -d ~/dbx
  130. then
  131. export CHIT_PATH="$HOME/dbx/.chit"
  132. fi
  133. #######################
  134. iswindows && alias tty="echo cmd.exe"
  135. type fortune >/dev/null 2>&1 && {
  136. fortune
  137. echo
  138. fortune -o
  139. echo
  140. }
  141. uname -a
  142. echo TERM $TERM $(tput colors) colors connected to $(tty), \
  143. running $BASH $BASH_VERSION
  144. if test -n "$TMUX"
  145. then
  146. tmux display -p 'Using tmux #S:#I:#W.#P, client is #{client_termname}' \
  147. 2>/dev/null
  148. echo
  149. fi
  150. ###################################
  151. # some aliases and functions
  152. ( ! with_coreutils && isdarwin ) || test "$TERM" = dumb || \
  153. _coloroption=" --color=auto"
  154. ( ! with_coreutils && isdarwin ) || iswindows || \
  155. _timeoption=" --time-style=long-iso"
  156. ( ! with_coreutils && isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
  157. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  158. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  159. _timeformat_num="%Y%m%d%H%M%S"
  160. alias datenum="date +$_timeformat_num"
  161. alias ls="ls -hCF${_coloroption}${_timeoption}"
  162. if ! with_coreutils
  163. then
  164. export LSCOLORS=gxfxcxdxbxegedabagacad
  165. alias ls="ls -G"
  166. fi
  167. # export GREP_OPTIONS=""
  168. alias gr="grep -n --color=always"
  169. iswindows && alias grep="grep -n"
  170. # alias ll="ls -l"
  171. # alias la="ls -A"
  172. # alias lla="ls -Al"
  173. alias less="less -F"
  174. null type emacs && alias em="emacs -nw"
  175. null type vim && alias vi=vim
  176. alias pstree="LANG=C pstree"
  177. alias cp="cp -v"
  178. alias mv="mv -v"
  179. alias rm="rm -v"
  180. alias psaux="ps auxww"
  181. alias q=exit
  182. null type e3em && alias e3=e3em
  183. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  184. alias po=popd
  185. alias pu=pushd
  186. null type sudo && alias sudo="sudo " # use aliases through sudo
  187. null type sudoedit && alias sudoe="sudoedit"
  188. null type halt && alias halt="sudo halt"
  189. null type reboot && alias reboot="sudo reboot"
  190. null type dbus-send && {
  191. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  192. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  193. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  194. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  195. }
  196. alias rand="echo \$RANDOM"
  197. null type file-roller && alias xunp="file-roller -h"
  198. null type paco && alias pc="sudo \paco -D"
  199. alias pycalc="python -i -c 'from math import *' "
  200. null type python3 && alias py3=python3
  201. null type python2 && alias py2=python2
  202. alias _reloadrc="exec \"$SHELL\""
  203. # alias mytime="date +%Y%m%d-%H%M%S"
  204. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  205. # type trash >/dev/null 2>&1 && alias rm=trash
  206. null type mpg123 && alias mpg123="mpg123 -C -v --title"
  207. null type xmms2 && alias xm="xmms2"
  208. #export PLAYER="mpg123 -C -v --title"
  209. null type screen && alias screen="screen -e^z^z"
  210. #alias zcd="cd \`zenity --file-selection --directory\`"
  211. null type gtags && alias gtags="gtags --verbose"
  212. null type htags && alias htags="htags --xhtml --symbol --line-number \
  213. --frame --alphabet --verbose"
  214. null type aunpack && alias au=aunpack
  215. null type lv && alias lv="lv|less"
  216. null type rsync && alias rs="rsync --progress --itemize-changes --compress"
  217. isdarwin && alias updatedb="LC_ALL=C updatedb"
  218. # do not use locate installed by macports
  219. isdarwin && test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  220. # pad
  221. alias pad=notepad
  222. null type gedit && alias pad=gedit
  223. null type leafpad && alias pad=leafpad
  224. isdarwin && alias pad="open -e"
  225. null type wicd-curses && alias wic=wicd-curses
  226. null type wicd-cli && alias wil="wicd-cli -y -l | head"
  227. #alias wicn="wicd-cli -y -c -n"
  228. wicn(){
  229. if test $# -eq 0
  230. then
  231. local num
  232. wicd-cli -y -l | head
  233. echo -n "input num: "
  234. read num
  235. test -n "$num" && wicd-cli -y -c -n $num
  236. else
  237. wicd-cli -y -c -n $1
  238. fi
  239. }
  240. __find_latest_vimdir(){
  241. vimdir=/usr/share/vim
  242. if test -d "$vimdir"
  243. then
  244. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  245. else
  246. echo ""
  247. fi
  248. }
  249. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  250. do
  251. test -n "$f" || continue
  252. f="$f/macros/less.sh"
  253. test -f $f && alias vl=$f && break
  254. done
  255. alias pa=pacapt
  256. null type yaourt && alias yt=yaourt
  257. null type cower && alias cower="cower --color=auto"
  258. null type pacmatic && {
  259. alias pacman="pacmatic"
  260. export PACMAN="pacmatic"
  261. }
  262. _pacman_update_mirrorlist_with_reflector(){
  263. ml=/etc/pacman.d/mirrorlist
  264. cmd="$(expr "$(grep -m 1 reflector $ml)" : '# With: *\(.*\)')"
  265. if test -z "$cmd"
  266. then
  267. cmd="reflector --verbose -l 5 --sort rate --save $ml"
  268. fi
  269. echo "Running $cmd ..." 1>&2
  270. sudo $cmd
  271. }
  272. null type reflector && test -f /etc/pacman.d/mirrorlist && \
  273. alias reflect_mirrorlist=_pacman_update_mirrorlist_with_reflector
  274. null type apt-get && {
  275. alias aupgrade="sudo apt-get autoremove --yes && \
  276. sudo apt-get update --yes && sudo apt-get upgrade --yes"
  277. alias aptin="apt-get install"
  278. alias aptsearch="apt-cache search"
  279. alias aptshow="apt-cache show"
  280. }
  281. null type port && {
  282. alias port="port -v"
  283. alias pupgrade="sudo port -v selfupdate && \
  284. { sudo port -v upgrade outdated; }"
  285. }
  286. if iscygwin; then
  287. null type windate || \
  288. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  289. # alias cygsu="cygstart /cygwinsetup.exe"
  290. # alias ls="ls -CFG $(iswindows || test "$TERM" = dumb || echo --color=auto)"
  291. fi
  292. g(){
  293. if test $# -eq 0 && null type git-info
  294. then
  295. git info
  296. else
  297. git -c color.ui=always "$@"
  298. fi
  299. }
  300. if null type _git && inbash
  301. then
  302. # enable programmable completion for g
  303. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  304. || complete -o default -o nospace -F _git g
  305. fi
  306. git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  307. null type gitmemo && alias m=gitmemo
  308. alias setup.py="sudo python3 setup.py install --record files.txt"
  309. datestr(){
  310. # datestr yyyyMMdd-hhmmss
  311. if test -z "$1" || test "$1" == "-h"
  312. then
  313. echo "datestr: usage: datestr <yyyyMMddhhmmss>"
  314. return 1
  315. fi
  316. dfmt= # actual format for date command
  317. while test -n "$1"
  318. do
  319. fmt="$1"
  320. while test -n "$fmt"
  321. do
  322. case "$fmt" in
  323. yyyy*) # year
  324. dfmt="${dfmt}%Y"
  325. fmt="`echo "$fmt" | cut -c 5-`"
  326. ;;
  327. yy*) # last two digits of year
  328. dfmt="${dfmt}%y"
  329. fmt="`echo "$fmt" | cut -c 3-`"
  330. ;;
  331. MM*) # month (01..12)
  332. dfmt="${dfmt}%m"
  333. fmt="`echo "$fmt" | cut -c 3-`"
  334. ;;
  335. dd*) # day of month (01..12)
  336. dfmt="${dfmt}%d"
  337. fmt="`echo "$fmt" | cut -c 3-`"
  338. ;;
  339. HH* | hh*) # hour (00..23)
  340. dfmt="${dfmt}%H"
  341. fmt="`echo "$fmt" | cut -c 3-`"
  342. ;;
  343. mm*) # minute (00..59)
  344. dfmt="${dfmt}%M"
  345. fmt="`echo "$fmt" | cut -c 3-`"
  346. ;;
  347. ss*) # second (00..60)
  348. dfmt="${dfmt}%S"
  349. fmt="`echo "$fmt" | cut -c 3-`"
  350. ;;
  351. *)
  352. char=`echo "$fmt" | cut -c 1`
  353. dfmt="${dfmt}${char}"
  354. fmt="`echo "$fmt" | cut -c 2-`"
  355. ;;
  356. esac
  357. done
  358. shift
  359. done
  360. date +"$dfmt"
  361. }
  362. ssh(){
  363. __my_set_screen_title ssh
  364. command ssh "$@"
  365. }
  366. __ssh_with_cd(){
  367. # __ssh_with_cd <host> <directory> [<arg> ...]
  368. if test -z "$2"
  369. then
  370. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  371. return 1
  372. fi
  373. host="$1"
  374. shift
  375. dir="$1"
  376. shift
  377. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  378. }
  379. memo(){
  380. if test -z "$1"
  381. then
  382. $EDITOR memo.txt
  383. else
  384. $EDITOR "$1/memo.txt"
  385. fi
  386. }
  387. clk(){
  388. local tformat="%Y/%m/%d %H:%M:%S %z"
  389. cal
  390. REPLY=
  391. printf "\\r`date "+${tformat}"`"
  392. read -t 1
  393. while test $? -ne 0
  394. do
  395. printf "\\r`date "+${tformat}"`"
  396. read -t 1
  397. done
  398. }
  399. s(){
  400. if git rev-parse --git-dir >/dev/null 2>&1
  401. then
  402. echo ">> git grep -n $@" 1>&2
  403. git grep -n "$@"
  404. elif which ag >/dev/null 2>&1
  405. then
  406. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  407. ag --pager="$PAGER" "$@"
  408. elif which ack >/dev/null 2>&1
  409. then
  410. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  411. ack --pager="$PAGER" "$@"
  412. else
  413. echo \
  414. ">> find . " \
  415. "-path '*/.git' -prune -o" \
  416. "-path '*/.svn' -prune -o" \
  417. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  418. if test $# -eq 0
  419. then
  420. echo "No search word given." 1>&2
  421. return 1
  422. fi
  423. find . \
  424. -path '*/.git' -prune -o \
  425. -path '*/.svn' -prune -o \
  426. -type -f -exec grep -nH -e --color=always "$@" {} + \
  427. | $PAGER
  428. fi
  429. }
  430. man(){
  431. env \
  432. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  433. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  434. LESS_TERMCAP_me=$(printf "\e[0m") \
  435. LESS_TERMCAP_se=$(printf "\e[0m") \
  436. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  437. LESS_TERMCAP_ue=$(printf "\e[0m") \
  438. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  439. man "$@"
  440. }
  441. netwait(){
  442. while ! ping -c 1 -t 1 example.com
  443. do
  444. true
  445. done
  446. echo network works.
  447. }
  448. cd(){
  449. if test $# -eq 0
  450. then
  451. pushd ~/ >/dev/null
  452. elif test "$1" = -
  453. then
  454. local pwd="$PWD"
  455. command cd "$OLDPWD"
  456. pushd -n "$pwd" >/dev/null # stack last dir
  457. elif ! test -d "$1"
  458. then
  459. echo `basename ${SHELL}`: cd: "$1": No such file or directory 1>&2
  460. return 1
  461. else
  462. pushd "$1" >/dev/null
  463. fi
  464. __dirs_rm_dup "$PWD"
  465. echo "$PWD"
  466. }
  467. __dirs_rm_dup(){
  468. for d in "$@"
  469. do
  470. local next="$(__realpath --strip "$d")"
  471. for l in $(\dirs -v -l | cut -d $'\n' -f 2- | \
  472. \grep -x " *[0-9]\+ \+${next}" | \grep -o "^ *[0-9]\+ " | tac)
  473. do
  474. popd +$l -n >/dev/null
  475. done
  476. done
  477. }
  478. __realpath(){
  479. if type realpath >/dev/null 2>&1
  480. then
  481. command realpath "$@"
  482. else
  483. while ! test -d $1
  484. do
  485. shift
  486. done
  487. local d="$OLDPWD"
  488. command cd "$1"
  489. echo "$PWD"
  490. command cd "$d"
  491. fi
  492. }
  493. dh(){
  494. if test $# -eq 0
  495. then
  496. dirs -v -l
  497. else
  498. local dir="$(dirs -v -l | \grep "^ *$1 \+" | sed "s/^ *[0-9]* *//g")"
  499. cd "$dir"
  500. fi
  501. }
  502. input(){
  503. local foo
  504. stty -echo
  505. read foo
  506. stty echo
  507. echo $foo
  508. }
  509. # tmux(){
  510. # if test $# -eq 0
  511. # then
  512. # (cd ~; command tmux start;)
  513. # if command tmux has -t main
  514. # then
  515. # command tmux attach -t main
  516. # else
  517. # (cd ~; command tmux new -s main;)
  518. # fi
  519. # else
  520. # command tmux "$@"
  521. # fi
  522. # }
  523. tx(){
  524. if test $# -eq 0
  525. then
  526. tmux ls
  527. echo "tx <session> to attach."
  528. elif tmux has -t "$1"
  529. then
  530. tmux attach -t "$1"
  531. else
  532. tmux new -s "$1"
  533. fi
  534. }
  535. dt(){
  536. # dt [<name>] [<command ...>]
  537. __dtach_dir="${TMP}/dtach"
  538. install -d "${__dtach_dir}"
  539. if test -n "${__MY_DTACH}"
  540. then
  541. echo "Current session: ${__MY_DTACH}"
  542. fi
  543. if test -z "$1"
  544. then
  545. echo "Sessions:"
  546. ls "${__dtach_dir}"
  547. return 0
  548. elif test "$1" = "-h"
  549. then
  550. echo "dt: usage: dt <name> [<command ...>]" 1>&2
  551. return 1
  552. fi
  553. soc_name="${__dtach_dir}/$1"
  554. shift
  555. if test -n "$__MY_DTACH"
  556. then
  557. echo "dtach session cannot be nested." 1>&2
  558. return 1
  559. elif test -S "$soc_name"
  560. then
  561. dtach -a "$soc_name" -e ^^
  562. elif test -e "$soc_name"
  563. then
  564. echo "dt: File named $soc_name already exists."
  565. return 1
  566. elif test -z "$1"
  567. then
  568. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  569. # echo "dt: Socket named $soc_name not exists and no command specified."
  570. # return 1
  571. else
  572. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  573. fi
  574. }
  575. scr(){
  576. test -n "$1" && pf="${1}-"
  577. local _tformat="%Y%m%d-%H%M%S%z"
  578. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  579. __MY_SCRIPT=${_file} script ${_file} "$@"
  580. }
  581. dtscr(){
  582. # dtscr <command ...>
  583. if test -z "$1"
  584. then
  585. echo "dtscr: usage: dtscr <command ...>"
  586. return 1
  587. fi
  588. local _cmdstr="`echo $@ | tr ' ' +`"
  589. local _tformat="%Y%m%d-%H%M%S%z"
  590. local _name="${pf}`date +${_tformat}`-${_cmdstr}"
  591. local _scr_file="${HOME}/${_name}.script"
  592. local _dt_dir="${TMP}/dtscr"
  593. install -d "$_dt_dir"
  594. dtach -n "${_dt_dir}/${_name}" script "${_scr_file_}" "$@"
  595. # echo $_name
  596. # echo $_file
  597. }
  598. mcrypt-stream(){
  599. test $# -eq 2 || return 1
  600. case $1 in
  601. en)
  602. mcrypt --key $2 | base64 ;;
  603. de)
  604. base64 -d | mcrypt -d --key $2 ;;
  605. esac
  606. }
  607. gpg-stream(){
  608. test $# -eq 2 || return 1
  609. case $1 in
  610. en)
  611. gpg --passphrase $2 -c --batch |base64 ;;
  612. de)
  613. base64 -d|gpg --passphrase $2 -d --batch ;;
  614. esac
  615. }
  616. dgpg(){
  617. if test "$1" = help || test -z "$2"
  618. then
  619. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  620. return
  621. fi
  622. local srcs="$2"
  623. local dsts="$3"
  624. test -z "$dsts" && dsts="${srcs}.out"
  625. local pw
  626. echo -n "dgpg pw: "
  627. read -s pw
  628. echo ""
  629. test -z "$pw" && return 1
  630. for f in *${srcs}
  631. do
  632. local d="$(basename "$f" "${srcs}")${dsts}"
  633. echo -n "Processing $f to $d..."
  634. if test -d "$f"
  635. then
  636. echo "`printf 'failed (%s is directory)' $f`"
  637. elif test -f "$d"
  638. then
  639. echo "`printf 'failed (%s is already exists)' $d`"
  640. elif <"$f" gpg-stream $1 $pw >"$d" 2>/dev/null
  641. then
  642. echo "done"
  643. else
  644. echo "failed"
  645. test -f "$d" && rm "$d"
  646. fi
  647. done
  648. }
  649. alias enst="gpg-stream en"
  650. alias dest="gpg-stream de"
  651. showinfo(){
  652. echo "Japanese letters are 表示可能"
  653. __try_exec diskinfo
  654. ! isdarwin && test -n "${DISPLAY}" && {
  655. __try_exec xrandr | \grep --color=never ^Screen
  656. }
  657. iswindows || __try_exec finger $USER
  658. LANG=C __try_exec id
  659. __try_exec xset q
  660. }
  661. x(){
  662. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  663. #mkdir -p ~/.my/log
  664. # nohup startx >~/.my/log/xorg.log 2>&1 &
  665. # exit
  666. exec startx
  667. else
  668. echo "X cant be started! Another X is already running?" 1>&2
  669. fi
  670. }
  671. bak(){
  672. for file in "$@"
  673. do
  674. cp -v ${file} ${file}.bak
  675. done
  676. }
  677. di(){
  678. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  679. then
  680. local diffcmd=colordiff
  681. else
  682. local diffcmd=diff
  683. fi
  684. ${diffcmd} -u "$@" | ${PAGER}
  685. }
  686. tb(){
  687. local datenum=`date +%Y%m%d-%H%M%S`
  688. local tb="$HOME/.my/tb/$datenum"
  689. mkdir -p "$tb"
  690. for file in "$@"
  691. do
  692. mv -t "$tb" "$file"
  693. done
  694. }
  695. mkcd(){
  696. if test -z "$1"
  697. then
  698. echo "mkcd: usage: mkcd <dir>"
  699. return 1
  700. elif test -d "$1"
  701. then
  702. echo "Dir \"$1\" already exists."
  703. else
  704. mkdir -p "$1"
  705. echo "Dir \"$1\" created."
  706. fi
  707. cd "$1"
  708. }
  709. mkcdd(){
  710. # make and change date directory
  711. _d=`date +%Y%m%d-%H%M%S`
  712. mkcd "$_d"
  713. }
  714. if test -n "$TMUX" && null type reattach-to-user-namespace
  715. then
  716. alias pbpaste="reattach-to-user-namespace pbpaste"
  717. alias pbcopy="reattach-to-user-namespace pbcopy"
  718. fi
  719. catclip(){
  720. if iswindows
  721. then
  722. cat /dev/clipboard | tr -d \\r
  723. elif isdarwin
  724. then
  725. pbpaste
  726. else
  727. xclip -o -selection "clipboard"
  728. fi
  729. }
  730. setclip(){
  731. if test $# -eq 0
  732. then
  733. exec 3<&0
  734. else
  735. exec 3<<__EOF__
  736. `cat "$@"`
  737. __EOF__
  738. fi
  739. if iswindows
  740. then
  741. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  742. elif isdarwin
  743. then
  744. pbcopy 0<&3
  745. else
  746. 0<&3 xclip -i -f -selection "primary" | \
  747. xclip -i -f -selection "clipboard"
  748. fi
  749. exec 3<&-
  750. }
  751. open_file(){
  752. if iswindows
  753. then
  754. cmd.exe //c start "" "$@"
  755. elif isdarwin
  756. then
  757. touch "$@"
  758. open "$@"
  759. elif islinux
  760. then
  761. touch "$@"
  762. if null type pcmanfm; then
  763. LC_MESSAGES= pcmanfm "$@"
  764. else
  765. LC_MESSAGES= xdg-open "$@" &
  766. fi
  767. else
  768. cat "$@"
  769. fi
  770. }
  771. o(){
  772. if test $# -eq 0
  773. then
  774. open_file .
  775. else
  776. for f in "$@"
  777. do
  778. open_file "$(realpath "$f")"
  779. done
  780. fi
  781. }
  782. convmv-sjis2utf8-test(){
  783. convmv -r -f sjis -t utf8 *
  784. }
  785. convmv-sjis2utf8-notest(){
  786. convmv -r -f sjis -t utf8 * --notest
  787. }
  788. winln(){
  789. # for windose make link (actually junction)
  790. if test $# -eq 0
  791. then
  792. {
  793. echo "usage: winln TARGET LINK_NAME"
  794. echo "Create a link to TARGET with the name LINK_NAME \
  795. (that is, TARGET must already exist)."
  796. echo "About other features run 'junction'."
  797. } 1>&2
  798. return 1
  799. else
  800. junction "$2" "$1"
  801. fi
  802. }
  803. #################################################
  804. ## pastebin services
  805. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  806. sprunge(){
  807. # http://sprunge.us
  808. if test -z "$1"
  809. then
  810. curl -F 'sprunge=<-' http://sprunge.us
  811. else
  812. curl http://sprunge.us/$1
  813. fi
  814. }
  815. dpaste(){
  816. # http://dpaste.de
  817. if test -z "$1"
  818. then
  819. curl -F 'content=<-' https://dpaste.de/api/
  820. echo
  821. else
  822. curl https://dpaste.de/$1/raw/
  823. fi
  824. }
  825. ######################################
  826. ## Prompt
  827. __my_moc_state(){
  828. type mocp >/dev/null 2>&1 || return
  829. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  830. printf "$1" "`mocp -Q %title 2>/dev/null`"
  831. }
  832. __my_parse_svn_branch() {
  833. local LANG=C
  834. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  835. local svn_repository_root=$(svn info 2>/dev/null | \
  836. sed -ne 's#^Repository Root: ##p')
  837. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  838. awk '{print $1}'
  839. }
  840. __my_svn_ps1(){
  841. if svn status >/dev/null 2>&1
  842. then
  843. local svn_branch=$(__my_parse_svn_branch)
  844. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  845. fi
  846. }
  847. __my_battery_status(){
  848. local dir=/sys/class/power_supply/BAT0
  849. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  850. test -r $dir/charge_now
  851. then
  852. local st=$(cat $dir/status)
  853. local full=$(cat $dir/charge_full)
  854. local now=$(cat $dir/charge_now)
  855. local rate=$(expr $now \* 100 / $full)
  856. printf "$1" "${st}:${rate}%"
  857. fi
  858. }
  859. alias bat='__my_battery_status %s\\n'
  860. ip-address(){
  861. type ip >/dev/null 2>&1 || return 1
  862. local ip=$(LANG=C ip addr show scope global | \
  863. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  864. test -n "$ip" && printf $1 $ip
  865. }
  866. __my_ps1_str=""
  867. test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  868. test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  869. test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
  870. __my_ps1_scale(){
  871. local last=$?
  872. if null type stty && ! ismsys
  873. then
  874. stty size | tr -d $'\n' | tr " " x
  875. printf " "
  876. fi
  877. return $last
  878. }
  879. __my_ps1_tmux(){
  880. local last=$?
  881. null type tmux || return $last
  882. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  883. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  884. return $last
  885. }
  886. __my_ps1_moc(){
  887. local last=$?
  888. __my_moc_state "[MOC:%s]"
  889. return $last
  890. }
  891. for f in /usr/share/git/git-prompt.sh \
  892. /opt/local/share/git-core/git-prompt.sh \
  893. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  894. do
  895. test -r "$f" && . "$f" && break
  896. done
  897. GIT_PS1_SHOWDIRTYSTATE=t
  898. GIT_PS1_SHOWUPSTREAM=t
  899. __my_ps1_git(){
  900. local last=$?
  901. null type __git_ps1 || return $last
  902. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  903. __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  904. return $last
  905. }
  906. __my_ps1_ipaddr(){
  907. local last=$?
  908. ! iswindows && ip-address [Addr:%s]
  909. return $last
  910. }
  911. __my_ps1_bttry(){
  912. local last=$?
  913. local bst="${TMP}/batterystatus"
  914. if test -z "$DISPLAY" && ! iswindows
  915. then
  916. test -f $bst && local bstr="$(cat $bst)"
  917. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  918. echo "[Battery:$bstr]"
  919. __my_battery_status %s >$bst &
  920. fi
  921. return $last
  922. }
  923. __my_ps1_dirs(){
  924. dirs | wc -l
  925. }
  926. __my_ps1_jobs(){
  927. jobs | wc -l
  928. }
  929. if test "$TERM" != dumb
  930. then
  931. __my_c1="\[\e[0;33m\]" # color for PWD
  932. __my_c2="\[\e[0;36m\]" # color for user
  933. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  934. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  935. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
  936. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  937. else __my_c4="\[\e[1;32m\]" # color for ::
  938. fi
  939. __my_c5="\[\e[30;47m\]" # color for SCR
  940. __my_cdef="\[\e[0m\]"
  941. fi
  942. # export _LAST_STATUS=a
  943. # __my_export_last_status(){
  944. # local last=$?
  945. # echo $last
  946. # export _LAST_STATUS=$last
  947. # echo $_LAST_STATUS
  948. # return $last
  949. # }
  950. _ps1_bash="\
  951. ${__my_c4}:: ${__my_cdef}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_git)\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\$(__my_ps1_moc)\n\
  952. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$? $(__my_ps1_scale) \D{%T} ${__my_ps1_str}\$ "
  953. inbash && PS1=$_ps1_bash
  954. __my_set_screen_title(){
  955. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  956. then
  957. echo -ne "\033k$1\033\\"
  958. fi
  959. }
  960. __my_set_title(){
  961. case $TERM in
  962. (rxvt*|xterm*|aterm|screen*)
  963. title="$(echo $@)"
  964. test -t 1 &&
  965. test -n "$DISPLAY" &&
  966. test -z "$EMACS" &&
  967. echo -n -e "\033]0;${title}\007"
  968. ;;
  969. esac
  970. }
  971. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  972. __my_set_screen_title \$(basename \"\$PWD\")/"