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
13 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
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
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
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
13 years ago
12 years ago
11 years ago
11 years ago
12 years ago
11 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
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
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
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
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
11 years ago
12 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 tb="$HOME/.my/tb"
  613. mkdir -p "$tb"
  614. for file in "$@"
  615. do
  616. mv -t "$tb" "$file"
  617. done
  618. }
  619. mkcd(){
  620. if test -z "$1"
  621. then
  622. echo "mkcd: usage: mkcd <dir>"
  623. return 1
  624. elif test -d "$1"
  625. then
  626. echo "Dir \"$1\" already exists."
  627. else
  628. mkdir -p "$1"
  629. echo "Dir \"$1\" created."
  630. fi
  631. cd "$1"
  632. }
  633. mkcdd(){
  634. # make and change date directory
  635. _d=`date +%Y%m%d-%H%M%S`
  636. mkcd "$_d"
  637. }
  638. if test -n "$TMUX" && null type reattach-to-user-namespace
  639. then
  640. alias pbpaste="reattach-to-user-namespace pbpaste"
  641. alias pbcopy="reattach-to-user-namespace pbcopy"
  642. fi
  643. catclip(){
  644. if iswindows
  645. then
  646. cat /dev/clipboard | tr -d \\r
  647. elif isdarwin
  648. then
  649. pbpaste
  650. else
  651. xclip -o -selection "clipboard"
  652. fi
  653. }
  654. setclip(){
  655. if test $# -eq 0
  656. then
  657. exec 3<&0
  658. else
  659. exec 3<<__EOF__
  660. `cat "$@"`
  661. __EOF__
  662. fi
  663. if iswindows
  664. then
  665. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  666. elif isdarwin
  667. then
  668. pbcopy 0<&3
  669. else
  670. 0<&3 xclip -i -f -selection "primary" | \
  671. xclip -i -f -selection "clipboard"
  672. fi
  673. exec 3<&-
  674. }
  675. open_file(){
  676. if iswindows
  677. then
  678. cmd.exe //c start "" "$@"
  679. elif isdarwin
  680. then
  681. touch "$@"
  682. open "$@"
  683. elif islinux
  684. then
  685. touch "$@"
  686. if null type pcmanfm; then
  687. LC_MESSAGES= pcmanfm "$@"
  688. else
  689. LC_MESSAGES= xdg-open "$@" &
  690. fi
  691. else
  692. cat "$@"
  693. fi
  694. }
  695. o(){
  696. if test $# -eq 0
  697. then
  698. open_file .
  699. else
  700. for f in "$@"
  701. do
  702. open_file "$(realpath "$f")"
  703. done
  704. fi
  705. }
  706. convmv-sjis2utf8-test(){
  707. convmv -r -f sjis -t utf8 *
  708. }
  709. convmv-sjis2utf8-notest(){
  710. convmv -r -f sjis -t utf8 * --notest
  711. }
  712. winln(){
  713. # for windose make link (actually junction)
  714. if test $# -eq 0
  715. then
  716. {
  717. echo "usage: winln TARGET LINK_NAME"
  718. echo "Create a link to TARGET with the name LINK_NAME \
  719. (that is, TARGET must already exist)."
  720. echo "About other features run 'junction'."
  721. } 1>&2
  722. return 1
  723. else
  724. junction "$2" "$1"
  725. fi
  726. }
  727. #################################################
  728. ## pastebin services
  729. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  730. sprunge(){
  731. # http://sprunge.us
  732. if test -z "$1"
  733. then
  734. curl -F 'sprunge=<-' http://sprunge.us
  735. else
  736. curl http://sprunge.us/$1
  737. fi
  738. }
  739. dpaste(){
  740. # http://dpaste.de
  741. if test -z "$1"
  742. then
  743. curl -F 'content=<-' https://dpaste.de/api/
  744. echo
  745. else
  746. curl https://dpaste.de/$1/raw/
  747. fi
  748. }
  749. ######################################
  750. ## Prompt
  751. __my_moc_state(){
  752. type mocp >/dev/null 2>&1 || return
  753. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  754. printf "$1" "`mocp -Q %title 2>/dev/null`"
  755. }
  756. __my_parse_svn_branch() {
  757. local LANG=C
  758. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  759. local svn_repository_root=$(svn info 2>/dev/null | \
  760. sed -ne 's#^Repository Root: ##p')
  761. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  762. awk '{print $1}'
  763. }
  764. __my_svn_ps1(){
  765. if svn status >/dev/null 2>&1
  766. then
  767. local svn_branch=$(__my_parse_svn_branch)
  768. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  769. fi
  770. }
  771. __my_battery_status(){
  772. local dir=/sys/class/power_supply/BAT0
  773. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  774. test -r $dir/charge_now
  775. then
  776. local st=$(cat $dir/status)
  777. local full=$(cat $dir/charge_full)
  778. local now=$(cat $dir/charge_now)
  779. local rate=$(expr $now \* 100 / $full)
  780. printf "$1" "${st}:${rate}%"
  781. fi
  782. }
  783. alias bat='__my_battery_status %s\\n'
  784. ip-address(){
  785. type ip >/dev/null 2>&1 || return 1
  786. local ip=$(LANG=C ip addr show scope global | \
  787. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  788. test -n "$ip" && printf $1 $ip
  789. }
  790. __my_ps1_str=""
  791. test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  792. test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  793. test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
  794. __my_ps1_scale(){
  795. local last=$?
  796. if null type stty && ! ismsys
  797. then
  798. stty size | tr -d $'\n' | tr " " x
  799. printf " "
  800. fi
  801. return $last
  802. }
  803. __my_ps1_tmux(){
  804. local last=$?
  805. null type tmux || return $last
  806. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  807. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  808. return $last
  809. }
  810. __my_ps1_moc(){
  811. local last=$?
  812. __my_moc_state "[MOC:%s]"
  813. return $last
  814. }
  815. for f in /usr/share/git/git-prompt.sh \
  816. /opt/local/share/git-core/git-prompt.sh \
  817. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  818. do
  819. test -r "$f" && . "$f" && break
  820. done
  821. GIT_PS1_SHOWDIRTYSTATE=t
  822. GIT_PS1_SHOWUPSTREAM=t
  823. __my_ps1_git(){
  824. local last=$?
  825. null type __git_ps1 || return $last
  826. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  827. __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  828. return $last
  829. }
  830. __my_ps1_ipaddr(){
  831. local last=$?
  832. ! iswindows && ip-address [Addr:%s]
  833. return $last
  834. }
  835. __my_ps1_bttry(){
  836. local last=$?
  837. local bst="${TMP}/batterystatus"
  838. if test -z "$DISPLAY" && ! iswindows
  839. then
  840. test -f $bst && local bstr="$(cat $bst)"
  841. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  842. echo "[Battery:$bstr]"
  843. __my_battery_status %s >$bst &
  844. fi
  845. return $last
  846. }
  847. __my_ps1_dirs(){
  848. dirs | wc -l
  849. }
  850. __my_ps1_jobs(){
  851. jobs | wc -l
  852. }
  853. if test "$TERM" != dumb
  854. then
  855. __my_c1="\[\e[0;33m\]" # color for PWD
  856. __my_c2="\[\e[0;36m\]" # color for user
  857. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  858. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  859. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
  860. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  861. else __my_c4="\[\e[1;32m\]" # color for ::
  862. fi
  863. __my_c5="\[\e[30;47m\]" # color for SCR
  864. __my_cdef="\[\e[0m\]"
  865. fi
  866. # export _LAST_STATUS=a
  867. # __my_export_last_status(){
  868. # local last=$?
  869. # echo $last
  870. # export _LAST_STATUS=$last
  871. # echo $_LAST_STATUS
  872. # return $last
  873. # }
  874. _ps1_bash="\
  875. ${__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\
  876. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$? $(__my_ps1_scale) \D{%T} ${__my_ps1_str}\$ "
  877. inbash && PS1=$_ps1_bash
  878. __my_set_screen_title(){
  879. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  880. then
  881. echo -ne "\033k$1\033\\"
  882. fi
  883. }
  884. __my_set_title(){
  885. case $TERM in
  886. (rxvt*|xterm*|aterm|screen*)
  887. title="$(echo $@)"
  888. test -t 1 &&
  889. test -n "$DISPLAY" &&
  890. test -z "$EMACS" &&
  891. echo -n -e "\033]0;${title}\007"
  892. ;;
  893. esac
  894. }
  895. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  896. __my_set_screen_title \$(basename \"\$PWD\")/"