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 24 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
12 years ago
11 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
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
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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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. alias setup.py="sudo python3 setup.py install --record files.txt"
  299. datestr(){
  300. # datestr yyyyMMdd-hhmmss
  301. if test -z "$1" || test "$1" == "-h"
  302. then
  303. echo "datestr: usage: datestr <yyyyMMddhhmmss>"
  304. return 1
  305. fi
  306. dfmt= # actual format for date command
  307. while test -n "$1"
  308. do
  309. fmt="$1"
  310. while test -n "$fmt"
  311. do
  312. case "$fmt" in
  313. yyyy*) # year
  314. dfmt="${dfmt}%Y"
  315. fmt="`echo "$fmt" | cut -c 5-`"
  316. ;;
  317. yy*) # last two digits of year
  318. dfmt="${dfmt}%y"
  319. fmt="`echo "$fmt" | cut -c 3-`"
  320. ;;
  321. MM*) # month (01..12)
  322. dfmt="${dfmt}%m"
  323. fmt="`echo "$fmt" | cut -c 3-`"
  324. ;;
  325. dd*) # day of month (01..12)
  326. dfmt="${dfmt}%d"
  327. fmt="`echo "$fmt" | cut -c 3-`"
  328. ;;
  329. HH* | hh*) # hour (00..23)
  330. dfmt="${dfmt}%H"
  331. fmt="`echo "$fmt" | cut -c 3-`"
  332. ;;
  333. mm*) # minute (00..59)
  334. dfmt="${dfmt}%M"
  335. fmt="`echo "$fmt" | cut -c 3-`"
  336. ;;
  337. ss*) # second (00..60)
  338. dfmt="${dfmt}%S"
  339. fmt="`echo "$fmt" | cut -c 3-`"
  340. ;;
  341. *)
  342. char=`echo "$fmt" | cut -c 1`
  343. dfmt="${dfmt}${char}"
  344. fmt="`echo "$fmt" | cut -c 2-`"
  345. ;;
  346. esac
  347. done
  348. shift
  349. done
  350. date +"$dfmt"
  351. }
  352. ssh(){
  353. __my_set_screen_title ssh
  354. command ssh "$@"
  355. }
  356. memo(){
  357. if test -z "$1"
  358. then
  359. $EDITOR memo.txt
  360. else
  361. $EDITOR "$1/memo.txt"
  362. fi
  363. }
  364. clk(){
  365. local tformat="%Y/%m/%d %H:%M:%S %z"
  366. cal
  367. REPLY=
  368. printf "\\r`date "+${tformat}"`"
  369. read -t 1
  370. while test $? -ne 0
  371. do
  372. printf "\\r`date "+${tformat}"`"
  373. read -t 1
  374. done
  375. }
  376. s(){
  377. if git rev-parse --git-dir >/dev/null 2>&1
  378. then
  379. echo ">> git grep -n $@" 1>&2
  380. git grep -n "$@"
  381. elif which ag >/dev/null 2>&1
  382. then
  383. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  384. ag --pager="$PAGER" "$@"
  385. elif which ack >/dev/null 2>&1
  386. then
  387. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  388. ack --pager="$PAGER" "$@"
  389. else
  390. echo \
  391. ">> find . " \
  392. "-path '*/.git' -prune -o" \
  393. "-path '*/.svn' -prune -o" \
  394. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  395. if test $# -eq 0
  396. then
  397. echo "No search word given." 1>&2
  398. return 1
  399. fi
  400. find . \
  401. -path '*/.git' -prune -o \
  402. -path '*/.svn' -prune -o \
  403. -type -f -exec grep -nH -e --color=always "$@" {} + \
  404. | $PAGER
  405. fi
  406. }
  407. man(){
  408. env \
  409. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  410. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  411. LESS_TERMCAP_me=$(printf "\e[0m") \
  412. LESS_TERMCAP_se=$(printf "\e[0m") \
  413. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  414. LESS_TERMCAP_ue=$(printf "\e[0m") \
  415. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  416. man "$@"
  417. }
  418. netwait(){
  419. while ! ping -c 1 -t 1 example.com
  420. do
  421. true
  422. done
  423. echo network works.
  424. }
  425. cd(){
  426. if test $# -eq 0
  427. then
  428. pushd ~/ >/dev/null
  429. elif test "$1" = -
  430. then
  431. local pwd="$PWD"
  432. command cd "$OLDPWD"
  433. pushd -n "$pwd" >/dev/null # stack last dir
  434. elif ! test -d "$1"
  435. then
  436. echo `basename ${SHELL}`: cd: "$1": No such file or directory 1>&2
  437. return 1
  438. else
  439. pushd "$1" >/dev/null
  440. fi
  441. __dirs_rm_dup "$PWD"
  442. echo "$PWD"
  443. }
  444. __dirs_rm_dup(){
  445. for d in "$@"
  446. do
  447. local next="$(__realpath --strip "$d")"
  448. for l in $(\dirs -v -l | cut -d $'\n' -f 2- | \
  449. \grep -x " *[0-9]\+ \+${next}" | \grep -o "^ *[0-9]\+ " | tac)
  450. do
  451. popd +$l -n >/dev/null
  452. done
  453. done
  454. }
  455. __realpath(){
  456. if type realpath >/dev/null 2>&1
  457. then
  458. command realpath "$@"
  459. else
  460. while ! test -d $1
  461. do
  462. shift
  463. done
  464. local d="$OLDPWD"
  465. command cd "$1"
  466. echo "$PWD"
  467. command cd "$d"
  468. fi
  469. }
  470. dh(){
  471. if test $# -eq 0
  472. then
  473. dirs -v -l
  474. else
  475. local dir="$(dirs -v -l | \grep "^ *$1 \+" | sed "s/^ *[0-9]* *//g")"
  476. cd "$dir"
  477. fi
  478. }
  479. input(){
  480. local foo
  481. stty -echo
  482. read foo
  483. stty echo
  484. echo $foo
  485. }
  486. # tmux(){
  487. # if test $# -eq 0
  488. # then
  489. # (cd ~; command tmux start;)
  490. # if command tmux has -t main
  491. # then
  492. # command tmux attach -t main
  493. # else
  494. # (cd ~; command tmux new -s main;)
  495. # fi
  496. # else
  497. # command tmux "$@"
  498. # fi
  499. # }
  500. tx(){
  501. if test $# -eq 0
  502. then
  503. tmux ls
  504. echo "tx <session> to attach."
  505. elif tmux has -t "$1"
  506. then
  507. tmux attach -t "$1"
  508. else
  509. tmux new -s "$1"
  510. fi
  511. }
  512. dt(){
  513. # dt [<name>] [<command ...>]
  514. __dtach_dir="${TMP}/dtach"
  515. install -d "${__dtach_dir}"
  516. if test -n "${__MY_DTACH}"
  517. then
  518. echo "Current session: ${__MY_DTACH}"
  519. fi
  520. if test -z "$1"
  521. then
  522. echo "Sessions:"
  523. ls "${__dtach_dir}"
  524. return 0
  525. elif test "$1" = "-h"
  526. then
  527. echo "dt: usage: dt <name> [<command ...>]" 1>&2
  528. return 1
  529. fi
  530. soc_name="${__dtach_dir}/$1"
  531. shift
  532. if test -n "$__MY_DTACH"
  533. then
  534. echo "dtach session cannot be nested." 1>&2
  535. return 1
  536. elif test -S "$soc_name"
  537. then
  538. dtach -a "$soc_name" -e ^^
  539. elif test -e "$soc_name"
  540. then
  541. echo "dt: File named $soc_name already exists."
  542. return 1
  543. elif test -z "$1"
  544. then
  545. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  546. # echo "dt: Socket named $soc_name not exists and no command specified."
  547. # return 1
  548. else
  549. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  550. fi
  551. }
  552. scr(){
  553. test -n "$1" && pf="${1}-"
  554. local _tformat="%Y%m%d-%H%M%S%z"
  555. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  556. __MY_SCRIPT=${_file} script ${_file} "$@"
  557. }
  558. dtscr(){
  559. # dtscr <command ...>
  560. if test -z "$1"
  561. then
  562. echo "dtscr: usage: dtscr <command ...>"
  563. return 1
  564. fi
  565. local _cmdstr="`echo $@ | tr ' ' +`"
  566. local _tformat="%Y%m%d-%H%M%S%z"
  567. local _name="${pf}`date +${_tformat}`-${_cmdstr}"
  568. local _scr_file="${HOME}/${_name}.script"
  569. local _dt_dir="${TMP}/dtscr"
  570. install -d "$_dt_dir"
  571. dtach -n "${_dt_dir}/${_name}" script "${_scr_file_}" "$@"
  572. # echo $_name
  573. # echo $_file
  574. }
  575. mcrypt-stream(){
  576. test $# -eq 2 || return 1
  577. case $1 in
  578. en)
  579. mcrypt --key $2 | base64 ;;
  580. de)
  581. base64 -d | mcrypt -d --key $2 ;;
  582. esac
  583. }
  584. gpg-stream(){
  585. test $# -eq 2 || return 1
  586. case $1 in
  587. en)
  588. gpg --passphrase $2 -c --batch |base64 ;;
  589. de)
  590. base64 -d|gpg --passphrase $2 -d --batch ;;
  591. esac
  592. }
  593. dgpg(){
  594. if test "$1" = help || test -z "$2"
  595. then
  596. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  597. return
  598. fi
  599. local srcs="$2"
  600. local dsts="$3"
  601. test -z "$dsts" && dsts="${srcs}.out"
  602. local pw
  603. echo -n "dgpg pw: "
  604. read -s pw
  605. echo ""
  606. test -z "$pw" && return 1
  607. for f in *${srcs}
  608. do
  609. local d="$(basename "$f" "${srcs}")${dsts}"
  610. echo -n "Processing $f to $d..."
  611. if test -d "$f"
  612. then
  613. echo "`printf 'failed (%s is directory)' $f`"
  614. elif test -f "$d"
  615. then
  616. echo "`printf 'failed (%s is already exists)' $d`"
  617. elif <"$f" gpg-stream $1 $pw >"$d" 2>/dev/null
  618. then
  619. echo "done"
  620. else
  621. echo "failed"
  622. test -f "$d" && rm "$d"
  623. fi
  624. done
  625. }
  626. alias enst="gpg-stream en"
  627. alias dest="gpg-stream de"
  628. showinfo(){
  629. echo "Japanese letters are 表示可能"
  630. __try_exec diskinfo
  631. ! isdarwin && test -n "${DISPLAY}" && {
  632. __try_exec xrandr | \grep --color=never ^Screen
  633. }
  634. iswindows || __try_exec finger $USER
  635. LANG=C __try_exec id
  636. __try_exec xset q
  637. }
  638. x(){
  639. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  640. #mkdir -p ~/.my/log
  641. # nohup startx >~/.my/log/xorg.log 2>&1 &
  642. # exit
  643. exec startx
  644. else
  645. echo "X cant be started! Another X is already running?" 1>&2
  646. fi
  647. }
  648. bak(){
  649. for file in "$@"
  650. do
  651. cp -v ${file} ${file}.bak
  652. done
  653. }
  654. di(){
  655. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  656. then
  657. local diffcmd=colordiff
  658. else
  659. local diffcmd=diff
  660. fi
  661. ${diffcmd} -u "$@" | ${PAGER}
  662. }
  663. tb(){
  664. local datenum=`date +%Y%m%d-%H%M%S`
  665. local tb="$HOME/.my/tb/$datenum"
  666. mkdir -p "$tb"
  667. for file in "$@"
  668. do
  669. mv -t "$tb" "$file"
  670. done
  671. }
  672. mkcd(){
  673. if test -z "$1"
  674. then
  675. echo "mkcd: usage: mkcd <dir>"
  676. return 1
  677. elif test -d "$1"
  678. then
  679. echo "Dir \"$1\" already exists."
  680. else
  681. mkdir -p "$1"
  682. echo "Dir \"$1\" created."
  683. fi
  684. cd "$1"
  685. }
  686. mkcdd(){
  687. # make and change date directory
  688. _d=`date +%Y%m%d-%H%M%S`
  689. mkcd "$_d"
  690. }
  691. if test -n "$TMUX" && null type reattach-to-user-namespace
  692. then
  693. alias pbpaste="reattach-to-user-namespace pbpaste"
  694. alias pbcopy="reattach-to-user-namespace pbcopy"
  695. fi
  696. catclip(){
  697. if iswindows
  698. then
  699. cat /dev/clipboard | tr -d \\r
  700. elif isdarwin
  701. then
  702. pbpaste
  703. else
  704. xclip -o -selection "clipboard"
  705. fi
  706. }
  707. setclip(){
  708. if test $# -eq 0
  709. then
  710. exec 3<&0
  711. else
  712. exec 3<<__EOF__
  713. `cat "$@"`
  714. __EOF__
  715. fi
  716. if iswindows
  717. then
  718. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  719. elif isdarwin
  720. then
  721. pbcopy 0<&3
  722. else
  723. 0<&3 xclip -i -f -selection "primary" | \
  724. xclip -i -f -selection "clipboard"
  725. fi
  726. exec 3<&-
  727. }
  728. open_file(){
  729. if iswindows
  730. then
  731. cmd.exe //c start "" "$@"
  732. elif isdarwin
  733. then
  734. touch "$@"
  735. open "$@"
  736. elif islinux
  737. then
  738. touch "$@"
  739. if null type pcmanfm; then
  740. LC_MESSAGES= pcmanfm "$@"
  741. else
  742. LC_MESSAGES= xdg-open "$@" &
  743. fi
  744. else
  745. cat "$@"
  746. fi
  747. }
  748. o(){
  749. if test $# -eq 0
  750. then
  751. open_file .
  752. else
  753. for f in "$@"
  754. do
  755. open_file "$(realpath "$f")"
  756. done
  757. fi
  758. }
  759. convmv-sjis2utf8-test(){
  760. convmv -r -f sjis -t utf8 *
  761. }
  762. convmv-sjis2utf8-notest(){
  763. convmv -r -f sjis -t utf8 * --notest
  764. }
  765. winln(){
  766. # for windose make link (actually junction)
  767. if test $# -eq 0
  768. then
  769. {
  770. echo "usage: winln TARGET LINK_NAME"
  771. echo "Create a link to TARGET with the name LINK_NAME \
  772. (that is, TARGET must already exist)."
  773. echo "About other features run 'junction'."
  774. } 1>&2
  775. return 1
  776. else
  777. junction "$2" "$1"
  778. fi
  779. }
  780. #################################################
  781. ## pastebin services
  782. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  783. sprunge(){
  784. # http://sprunge.us
  785. if test -z "$1"
  786. then
  787. curl -F 'sprunge=<-' http://sprunge.us
  788. else
  789. curl http://sprunge.us/$1
  790. fi
  791. }
  792. dpaste(){
  793. # http://dpaste.de
  794. if test -z "$1"
  795. then
  796. curl -F 'content=<-' https://dpaste.de/api/
  797. echo
  798. else
  799. curl https://dpaste.de/$1/raw/
  800. fi
  801. }
  802. ######################################
  803. ## Prompt
  804. __my_moc_state(){
  805. type mocp >/dev/null 2>&1 || return
  806. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  807. printf "$1" "`mocp -Q %title 2>/dev/null`"
  808. }
  809. __my_parse_svn_branch() {
  810. local LANG=C
  811. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  812. local svn_repository_root=$(svn info 2>/dev/null | \
  813. sed -ne 's#^Repository Root: ##p')
  814. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  815. awk '{print $1}'
  816. }
  817. __my_svn_ps1(){
  818. if svn status >/dev/null 2>&1
  819. then
  820. local svn_branch=$(__my_parse_svn_branch)
  821. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  822. fi
  823. }
  824. __my_battery_status(){
  825. local dir=/sys/class/power_supply/BAT0
  826. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  827. test -r $dir/charge_now
  828. then
  829. local st=$(cat $dir/status)
  830. local full=$(cat $dir/charge_full)
  831. local now=$(cat $dir/charge_now)
  832. local rate=$(expr $now \* 100 / $full)
  833. printf "$1" "${st}:${rate}%"
  834. fi
  835. }
  836. alias bat='__my_battery_status %s\\n'
  837. ip-address(){
  838. type ip >/dev/null 2>&1 || return 1
  839. local ip=$(LANG=C ip addr show scope global | \
  840. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  841. test -n "$ip" && printf $1 $ip
  842. }
  843. __my_ps1_str=""
  844. test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  845. test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  846. test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
  847. __my_ps1_scale(){
  848. local last=$?
  849. if null type stty && ! ismsys
  850. then
  851. stty size | tr -d $'\n' | tr " " x
  852. printf " "
  853. fi
  854. return $last
  855. }
  856. __my_ps1_tmux(){
  857. local last=$?
  858. null type tmux || return $last
  859. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  860. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  861. return $last
  862. }
  863. __my_ps1_moc(){
  864. local last=$?
  865. __my_moc_state "[MOC:%s]"
  866. return $last
  867. }
  868. for f in /usr/share/git/git-prompt.sh \
  869. /opt/local/share/git-core/git-prompt.sh \
  870. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  871. do
  872. test -r "$f" && . "$f" && break
  873. done
  874. GIT_PS1_SHOWDIRTYSTATE=t
  875. GIT_PS1_SHOWUPSTREAM=t
  876. __my_ps1_git(){
  877. local last=$?
  878. null type __git_ps1 || return $last
  879. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  880. __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  881. return $last
  882. }
  883. __my_ps1_ipaddr(){
  884. local last=$?
  885. ! iswindows && ip-address [Addr:%s]
  886. return $last
  887. }
  888. __my_ps1_bttry(){
  889. local last=$?
  890. local bst="${TMP}/batterystatus"
  891. if test -z "$DISPLAY" && ! iswindows
  892. then
  893. test -f $bst && local bstr="$(cat $bst)"
  894. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  895. echo "[Battery:$bstr]"
  896. __my_battery_status %s >$bst &
  897. fi
  898. return $last
  899. }
  900. __my_ps1_dirs(){
  901. dirs | wc -l
  902. }
  903. __my_ps1_jobs(){
  904. jobs | wc -l
  905. }
  906. if test "$TERM" != dumb
  907. then
  908. __my_c1="\[\e[0;33m\]" # color for PWD
  909. __my_c2="\[\e[0;36m\]" # color for user
  910. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  911. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  912. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
  913. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  914. else __my_c4="\[\e[1;32m\]" # color for ::
  915. fi
  916. __my_c5="\[\e[30;47m\]" # color for SCR
  917. __my_cdef="\[\e[0m\]"
  918. fi
  919. # export _LAST_STATUS=a
  920. # __my_export_last_status(){
  921. # local last=$?
  922. # echo $last
  923. # export _LAST_STATUS=$last
  924. # echo $_LAST_STATUS
  925. # return $last
  926. # }
  927. _ps1_bash="\
  928. ${__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\
  929. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$? $(__my_ps1_scale) \D{%T} ${__my_ps1_str}\$ "
  930. inbash && PS1=$_ps1_bash
  931. __my_set_screen_title(){
  932. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  933. then
  934. echo -ne "\033k$1\033\\"
  935. fi
  936. }
  937. __my_set_title(){
  938. case $TERM in
  939. (rxvt*|xterm*|aterm|screen*)
  940. title="$(echo $@)"
  941. test -t 1 &&
  942. test -n "$DISPLAY" &&
  943. test -z "$EMACS" &&
  944. echo -n -e "\033]0;${title}\007"
  945. ;;
  946. esac
  947. }
  948. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  949. __my_set_screen_title \$(basename \"\$PWD\")/"