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.

bashrc 23 KiB

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