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 27 KiB

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