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.

shrc 25 KiB

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