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

13 years ago
11 years ago
11 years ago
9 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
10 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
8 years ago
12 years ago
12 years ago
12 years ago
10 years ago
12 years ago
10 years ago
12 years ago
6 years ago
12 years ago
12 years ago
9 years ago
11 years ago
11 years ago
11 years ago
12 years ago
12 years ago
10 years ago
12 years ago
12 years ago
12 years ago
10 years ago
12 years ago
11 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
10 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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
12 years ago
12 years ago
12 years ago
11 years ago
8 years ago
8 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
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
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
10 years ago
12 years ago
10 years ago
12 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
10 years ago
11 years ago
12 years ago
11 years ago
10 years ago
12 years ago
12 years ago
12 years ago
11 years ago
10 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. #!/bin/sh
  2. # TODO: decide the naming scheme of variables (global, local, ...)
  3. expr "$-" : '^.*i' >/dev/null || return
  4. ##########################################
  5. __shrc_lock=/tmp/shrc_lock.$USER.$$
  6. test -f "$__shrc_lock" && return
  7. touch "$__shrc_lock"
  8. ##########################################
  9. null(){
  10. "$@" >/dev/null 2>&1
  11. }
  12. __safe_run(){
  13. type $1 >/dev/null 2>&1 && "$@"
  14. }
  15. __match(){
  16. # __match str word
  17. # return 0 if word is found in str
  18. expr "$1" : ".*$2.*" >/dev/null
  19. }
  20. __lf=$'\n'
  21. __cr=$'\r'
  22. test -n "$TMP" || export TMP=/tmp/${USER}-tmp
  23. mkdir -p "$TMP"
  24. __homerun="$HOME/.var/run"
  25. mkdir -p "$__homerun"
  26. ##########################
  27. # system type
  28. gnu_coreutils=false # for mac
  29. null ls --version && gnu_coreutils=true
  30. inbash=false
  31. inzsh=false
  32. if test -n "$BASH_VERSION"
  33. then
  34. inbash=true
  35. elif test -n "$ZSH_VERSION"
  36. then
  37. inzsh=true
  38. fi
  39. #################################
  40. # file pathes:
  41. # shrc: Path to this file
  42. if $inbash
  43. then
  44. __shrc="$BASH_SOURCE"
  45. elif $inzsh
  46. then
  47. __shrc="$0"
  48. fi
  49. #########################
  50. # system detection
  51. __system_shrc="$__homerun/system.shrc"
  52. if test -f "$__system_shrc"
  53. then
  54. . "$__system_shrc"
  55. else
  56. ismsys=false
  57. iscygwin=false
  58. iswindows=false
  59. isdarwin=false
  60. isfreebsd=false
  61. isbsd=false
  62. islinux=false
  63. # $OSTYPE is another choice. which is better?
  64. # NOTE: sh on FreeBSD does not define OSTYPE
  65. case `uname` in
  66. MINGW*) ismsys=true ;;
  67. CYGWIN*) iscygwin=true ;;
  68. Darwin*) isdarwin=true ;;
  69. FreeBSD*) isfreebsd=true ;;
  70. Linux*) islinux=true ;;
  71. esac
  72. ($ismsys || $iscygwin) && iswindows=true
  73. # is this true?
  74. ($isdarwin || $isfreebsd) && isbsd=true
  75. # dump system detection result
  76. cat <<__EOC__ >"$__system_shrc"
  77. #!/bin/sh
  78. # $__system_shrc
  79. ismsys=$ismsys
  80. iscygwin=$iscygwin
  81. iswindows=$iswindows
  82. isdarwin=$isdarwin
  83. isfreebsd=$isfreebsd
  84. isbsd=$isbsd
  85. islinux=$islinux
  86. __EOC__
  87. fi
  88. $ismsys && export HOSTNAME
  89. # adhoc fix for ansible on cygwin
  90. # http://blog.s-uni.net/2013/08/27/ansible-running-on-cygwin/
  91. if $iscygwin
  92. then
  93. export ANSIBLE_SSH_ARGS="-o ControlMaster=no"
  94. fi
  95. ##########################
  96. # Terminal setups
  97. if ! $iswindows && null type stty
  98. then
  99. stty stop undef # unbind C-s to stop displaying output
  100. # stty erase '^h'
  101. fi
  102. # Zsh specific preferences
  103. # http://www.clear-code.com/blog/2011/9/5.html
  104. if $inzsh
  105. then
  106. bindkey -e
  107. # http://zsh.sourceforge.net/Guide/zshguide06.html#l147
  108. autoload compinit; compinit
  109. # supress cycle by tab
  110. unsetopt auto_menu
  111. # unsetopt correct
  112. setopt complete_aliases
  113. setopt auto_list
  114. setopt bash_auto_list
  115. setopt magic_equal_subst
  116. setopt list_types
  117. # what is the difference of these two?
  118. setopt auto_param_slash
  119. setopt mark_dirs
  120. zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
  121. zstyle ':completion:*' format '%B%d%b'
  122. zstyle ':completion:*' group-name ''
  123. zstyle ':completion:*' use-cache yes
  124. # zstyle ':completion:*:cd:*' tag-order local-directories
  125. zstyle ':completion:*' completer _complete _bash_completions \
  126. _history
  127. # zstyle ':completion:*:*:cd:*' completer
  128. zstyle ':completion:*' accept-exact-dirs true
  129. zstyle ':completion:*' special-dirs true
  130. autoload colors; colors
  131. # autoload -Uz promptinit
  132. # promptinit
  133. # prompt walters
  134. setopt hist_ignore_dups
  135. setopt hist_ignore_all_dups
  136. setopt hist_save_no_dups
  137. setopt extended_history
  138. setopt share_history
  139. setopt append_history
  140. HISTFILE=$HOME/.zsh-history
  141. HISTSIZE=100000
  142. SAVEHIST=100000
  143. setopt prompt_subst
  144. setopt interactive_comments
  145. fi
  146. ######################
  147. # Print welcome messages
  148. command -v fortune >/dev/null && {
  149. if command -v cowsay >/dev/null
  150. then
  151. fortune | cowsay
  152. echo
  153. fortune -o | cowsay
  154. echo
  155. else
  156. fortune
  157. echo
  158. fortune -o
  159. echo
  160. fi
  161. }
  162. if null type figlet && null type lolcat
  163. then
  164. hostname -s | figlet | lolcat
  165. fi
  166. uname -a
  167. $iswindows && alias tty="echo cmd.exe"
  168. echo TERM $TERM with $(tput colors) colors using $(tty)
  169. echo umask: `umask -S`
  170. echo
  171. __safe_run w yuk
  172. # if null type tmux
  173. # then
  174. # echo
  175. # echo tmux sessions:
  176. # tmux ls 2>/dev/null| sed -e 's/^/ /g'
  177. # echo
  178. # fi
  179. # if test -n "$TMUX"
  180. # then
  181. # tmux display -p \
  182. # 'TMUX #S:#I:#W.#P, client TERM: #{client_termname}' \
  183. # 2>/dev/null
  184. # echo
  185. # fi
  186. if test -n "$__MY_DTACH"
  187. then
  188. echo
  189. echo DSession: Current Session: "`basename "$__MY_DTACH"`"
  190. echo DSession: Press C-^ to Exit.
  191. true
  192. fi
  193. i(){
  194. w
  195. ss -lnp
  196. ps aufx
  197. df -h
  198. top -b -d 1 -n 1
  199. top -b -d 1 -n 1 -a
  200. dstat -taf 1 10
  201. }
  202. ###################################
  203. # some aliases and functions
  204. # __func_name: never used interactively
  205. # _func_name: usually not used interactively
  206. __safe_alias(){
  207. # __safe_alias <name>=<command>
  208. _bin=`expr "$1" : '^[^=]*=\([^ ]*\)'`
  209. test -n "$_bin" && \
  210. null type $_bin && \
  211. alias "$1"
  212. }
  213. $gnu_coreutils && _timeoption=" --time-style=long-iso"
  214. # color prefs
  215. if $gnu_coreutils
  216. then
  217. # http://qiita.com/yuyuchu3333/items/84fa4e051c3325098be3
  218. # gnu coreutils LS_COLORS is used
  219. null type dircolors && eval `dircolors`
  220. _coloroption=" --color=auto"
  221. else
  222. # export LSCOLORS=gxfxcxdxbxegedabagacad
  223. export LSCOLORS=DxGxcxdxCxegedabagacad
  224. export CLICOLOR=1
  225. fi
  226. alias ls="ls -hCF${_coloroption}${_timeoption}"
  227. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  228. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  229. _timeformat_num="%Y%m%d%H%M%S"
  230. alias datenum="date +$_timeformat_num"
  231. alias less="less -FX"
  232. __safe_alias em="emacs -nw"
  233. test -n "$_VI_ALT" && alias vi=$_VI_ALT
  234. alias pstree="LANG=C pstree"
  235. alias cp="cp -v"
  236. alias mv="mv -v"
  237. alias rm="rm -v"
  238. alias mkdir="mkdir -v"
  239. $gnu_coreutils && alias rmdir="rmdir -v"
  240. alias psaux="ps auxww"
  241. alias modest="ionice -c 2 -n 7 nice -n 19"
  242. alias e=exec
  243. __safe_alias e3=e3em
  244. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  245. alias po=popd
  246. alias pu=pushd
  247. __safe_alias sudoe="sudoedit"
  248. __safe_alias ge="git exec"
  249. __safe_alias make="nice make"
  250. __safe_alias brew="nice brew"
  251. __safe_alias gcc="nice gcc"
  252. __safe_alias go="nice go"
  253. __safe_alias git="nice git"
  254. __safe_alias javac="nice javac -J-Duser.language=en"
  255. __safe_alias ansible-playbook="nice ansible-playbook"
  256. alias rand="echo \$RANDOM"
  257. alias pycalc="python -i -c 'from math import *' "
  258. __safe_alias py3=python3
  259. __safe_alias py2=python2
  260. __safe_alias ipy=ipython
  261. __safe_alias ipy3=ipython3
  262. __safe_alias ipy2=ipython2
  263. __safe_alias rl=rlwrap
  264. __safe_alias igosh=gosh-rl
  265. # Sometimes SHELL cannot be used. For example, when running bash inside zsh
  266. # SHELL is set to be /bin/zsh
  267. if $inbash
  268. then
  269. alias _reloadrc="rm '$__shrc_lock'; exec bash"
  270. elif $inzsh
  271. then
  272. alias _reloadrc="rm '$__shrc_lock'; exec zsh"
  273. else
  274. alias _reloadrc="rm '$__shrc_lock'; exec $SHELL"
  275. fi
  276. # alias mytime="date +%Y%m%d-%H%M%S"
  277. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  278. # some extra utilities
  279. __safe_alias mpg123="mpg123 -C -v --title"
  280. #export PLAYER="mpg123 -C -v --title"
  281. __safe_alias screen="screen -e^z^z"
  282. #alias zcd="cd \`zenity --file-selection --directory\`"
  283. __safe_alias gtags="gtags --verbose"
  284. __safe_alias htags="htags --xhtml --symbol --line-number \
  285. --frame --alphabet --verbose"
  286. __safe_alias au=aunpack
  287. # __safe_alias lv="lv|less"
  288. __safe_alias rs="rsync --progress --itemize-changes --compress"
  289. __safe_alias vagr=vagrant
  290. __safe_alias ztw='twitter set "`zenity --entry --title ztw --text Status:`"'
  291. if $iscygwin
  292. then
  293. __my_wget_options=" --no-check-certificate"
  294. __safe_alias wget="wget $__my_wget_options"
  295. fi
  296. if $isdarwin
  297. then
  298. alias updatedb="LC_ALL=C updatedb"
  299. # do not use locate installed by macports
  300. test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  301. fi
  302. # somehow not work
  303. # typeset -ga chpwd_functions
  304. # chpwd_functions+=pwd
  305. # chpwd(){
  306. # pwd
  307. # }
  308. cd(){
  309. builtin cd "$@" && pwd
  310. }
  311. root(){
  312. if test "`git rev-parse --is-inside-work-tree 2>/dev/null`" = true
  313. then
  314. cd "`git rev-parse --show-toplevel`"
  315. else
  316. cd /
  317. fi
  318. }
  319. __find_latest_vimdir(){
  320. vimdir=/usr/share/vim
  321. if test -d "$vimdir"
  322. then
  323. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  324. else
  325. echo ""
  326. fi
  327. }
  328. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  329. do
  330. test -n "$f" || continue
  331. f="$f/macros/less.sh"
  332. test -f $f && alias vl=$f && break
  333. done
  334. __safe_alias pa="pacman --color=always"
  335. __safe_alias pa="pacmatic --color=always"
  336. __safe_alias pa="pacapt"
  337. __safe_alias yt=yaourt
  338. # variable for yaourt
  339. null type pacmatic && export PACMAN="pacmatic"
  340. __safe_alias cower="cower --color=auto"
  341. __my_pacman_update_mirrorlist_with_reflector(){
  342. f=/etc/pacman.d/mirrorlist
  343. cmd="$(expr "$(grep -m 1 reflector $f)" : '# With: *\(.*\)')"
  344. if test -z "$cmd"
  345. then
  346. cmd="reflector --verbose --number 5 --sort rate --protocol http --protocol https --save $f"
  347. fi
  348. echo ">>> $cmd ..." 1>&2
  349. sudo sh -c "$cmd" && cat $f
  350. }
  351. null type reflector && test -d /etc/pacman.d && \
  352. alias reflect_mirrorlist=__my_pacman_update_mirrorlist_with_reflector
  353. null type apt-get && {
  354. alias aupgrade="sudo sh -xc 'apt-get autoremove --yes && \
  355. apt-get update --yes && apt-get upgrade --yes'"
  356. alias aptin="apt-get install"
  357. alias aptsearch="apt-cache search"
  358. alias aptshow="apt-cache show"
  359. }
  360. null type port && {
  361. alias port="port -v"
  362. alias pupgrade="sudo sh -xc 'port -v selfupdate && \
  363. port -v upgrade outdated'"
  364. }
  365. if $iscygwin
  366. then
  367. null type windate || \
  368. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  369. # alias cygsu="cygstart /cygwinsetup.exe"
  370. fi
  371. mkpatch(){
  372. if test $# -eq 0
  373. then
  374. echo "usage: mkpatch <olddir> <newdir>"
  375. elif ! test -d "$1"
  376. then
  377. echo "mkpatch: $1 is not a directory"
  378. elif ! test -d "$2"
  379. then
  380. echo "mkpatch: $2 is not a directory"
  381. else
  382. diff -Naur "$1" "$2"
  383. fi
  384. }
  385. __sdcv_less(){
  386. command sdcv -n "$@" | less --quit-if-one-screen
  387. }
  388. command -v sdcv >/dev/null && alias dict=__sdcv_less
  389. __safe_alias g="git "
  390. if null type _git && $inbash
  391. then
  392. # enable programmable completion for g
  393. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  394. || complete -o default -o nospace -F _git g
  395. fi
  396. #git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  397. __safe_alias m=gitmemo
  398. #alias setup.py3="sudo python3 setup.py install --record files.txt"
  399. randomstr(){
  400. len=$1
  401. test -z "$len" && len=8
  402. uuidgen | tr -d - | cut -c 1-len
  403. }
  404. datestr(){
  405. # datestr YYYYMMDDhhmmss
  406. if test -z "$1" || test "$1" = "-h"
  407. then
  408. echo "datestr: usage: datestr <YYYYMMDDhhmmss>"
  409. return 1
  410. fi
  411. # actual format for date command
  412. dfmt="`echo "$1" | \
  413. sed -e 's/YYYY/%Y/g' -e 's/yyyy/%Y/g' -e 's/YY/%y/g' -e 's/yy/%y/g' |\
  414. sed -e 's/MM/%m/g' -e 's/DD/%d/g' -e 's/dd/%d/g' | \
  415. sed -e 's/HH/%H/g' -e 's/hh/%H/g' -e 's/mm/%M/g' -e 's/SS/%S/g' -e 's/ss/%S/g'`"
  416. date +"$dfmt"
  417. }
  418. # ssh(){
  419. # # __my_terminal_title ssh
  420. # command ssh "$@"
  421. # }
  422. __ssh_with_cd(){
  423. # __ssh_with_cd <host> <directory> [<arg> ...]
  424. if test -z "$2"
  425. then
  426. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  427. return 1
  428. fi
  429. host="$1"
  430. shift
  431. dir="$1"
  432. shift
  433. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  434. }
  435. memo(){
  436. if test -z "$1"
  437. then
  438. _memo="memo.txt"
  439. else
  440. _memo="$1/memo.txt"
  441. fi
  442. $EDITOR "$_memo"
  443. if test -z "`cat "$_memo"`"
  444. then
  445. echo "$_memo is empty. Removing."
  446. rm "$_memo"
  447. fi
  448. }
  449. now(){
  450. ___tformat="%Y/%m/%d %H:%M:%S %z"
  451. cal
  452. REPLY=
  453. printf "\\r`date "+${___tformat}"`"
  454. read -t 1
  455. while test $? -ne 0
  456. do
  457. printf "\\r`date "+${___tformat}"`"
  458. read -t 1
  459. done
  460. }
  461. s(){
  462. if git rev-parse --git-dir >/dev/null 2>&1
  463. then
  464. echo ">> git grep -n $@" 1>&2
  465. git grep -n "$@"
  466. elif which ag >/dev/null 2>&1
  467. then
  468. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  469. ag --pager="$PAGER" "$@"
  470. elif which ack >/dev/null 2>&1
  471. then
  472. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  473. ack --pager="$PAGER" "$@"
  474. else
  475. echo \
  476. ">> find . " \
  477. "-path '*/.git' -prune -o" \
  478. "-path '*/.svn' -prune -o" \
  479. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  480. if test $# -eq 0
  481. then
  482. echo "No search word given." 1>&2
  483. return 1
  484. fi
  485. find . \
  486. -path '*/.git' -prune -o \
  487. -path '*/.svn' -prune -o \
  488. -type -f -exec grep -nH -e --color=always "$@" {} + \
  489. | $PAGER
  490. fi
  491. }
  492. MYMANPATH='/usr/lib/erlang/man'
  493. if $inbash || $inzsh
  494. then
  495. man(){
  496. env \
  497. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  498. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  499. LESS_TERMCAP_me=$(printf "\e[0m") \
  500. LESS_TERMCAP_se=$(printf "\e[0m") \
  501. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  502. LESS_TERMCAP_ue=$(printf "\e[0m") \
  503. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  504. MANPATH="`manpath`:$MYMANPATH" \
  505. man "$@"
  506. }
  507. fi
  508. netwait(){
  509. while ! ping -c 1 -t 1 example.com
  510. do
  511. true
  512. done
  513. echo network works.
  514. }
  515. __realpath(){
  516. if type realpath >/dev/null 2>&1
  517. then
  518. command realpath "$@"
  519. else
  520. while ! test -d $1
  521. do
  522. shift
  523. done
  524. (command cd "$d" && echo "$PWD")
  525. # local d="$OLDPWD"
  526. # command cd "$1"
  527. # echo "$PWD"
  528. # command cd "$d"
  529. fi
  530. }
  531. if $isdarwin
  532. then
  533. # I want to use env -u, but darwin env does not provide this option
  534. null type tmux && alias tmux='SHLVL= tmux'
  535. else
  536. null type tmux && alias tmux='env -u SHLVL tmux'
  537. fi
  538. tx(){
  539. tmux start-server
  540. if test -z "$1"
  541. then
  542. echo "usage: tx [<session_name>]"
  543. tmux list-sessions
  544. elif test -z "$TMUX"
  545. then
  546. # -A: attach if already exists
  547. # tmux new-session -A -c "$HOME" -s "$1"
  548. tmux new-session -A -s "$1"
  549. else
  550. # create new session if not exists yet
  551. # tmux has-session -t "$1" || TMUX= tmux new-session -d -c "$HOME" -s "$1"
  552. tmux has-session -t "$1" || TMUX= tmux new-session -d -s "$1"
  553. tmux switch-client -t "$1"
  554. fi
  555. }
  556. __dsession(){
  557. # dt [-h] [<name>] [<command ...>]
  558. __dtach_dir="${TMP}/dtach"
  559. mkdir -p "${__dtach_dir}"
  560. if test -n "${__MY_DTACH}"
  561. then
  562. # if already in dtach session print current session
  563. soc_name="`basename "$__MY_DTACH"`"
  564. echo "Current session: ${soc_name}"
  565. fi
  566. if test -z "$1"
  567. then
  568. # if no arg given show list of sessions
  569. echo "Usage: ds [<session>]"
  570. echo "Sessions:"
  571. ls "${__dtach_dir}"
  572. return 0
  573. elif test "$1" = "-h"
  574. then
  575. echo "dt: usage: dt [-h] [<name>] [<command ...>]" 1>&2
  576. return 1
  577. fi
  578. # set socket name
  579. soc_name="${__dtach_dir}/$1"
  580. shift
  581. if test -n "$__MY_DTACH"
  582. then
  583. echo "dtach session cannot be nested." 1>&2
  584. return 1
  585. elif test -S "$soc_name"
  586. then
  587. dtach -a "$soc_name" -e ^^
  588. elif test -e "$soc_name"
  589. then
  590. echo "dt: File named $soc_name already exists."
  591. return 1
  592. elif test -z "$1"
  593. then
  594. # if no command given invoke current shell
  595. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  596. # echo "dt: Socket named $soc_name not exists and no command specified."
  597. # return 1
  598. else
  599. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  600. fi
  601. }
  602. null type dtach && alias ds=__dsession
  603. scr(){
  604. test -n "$1" && pf="${1}-"
  605. ___tformat="%Y%m%d-%H%M%S%z"
  606. ___file="${PWD}/${pf}`date +${___tformat}`.script"
  607. __MY_SCRIPT=${___file} script ${___file} "$@"
  608. }
  609. dtscr(){
  610. # dtscr <command ...>
  611. if test -z "$1"
  612. then
  613. echo "dtscr: usage: dtscr <command ...>"
  614. return 1
  615. fi
  616. ___cmdstr="`echo $@ | tr ' ' +`"
  617. ___tformat="%Y%m%d-%H%M%S%z"
  618. ___name="${pf}`date +${___tformat}`-${___cmdstr}"
  619. ___scr_file="${HOME}/${___name}.script"
  620. ___dt_dir="${TMP}/dtscr"
  621. mkdir -p "$___dt_dir"
  622. dtach -n "${___dt_dir}/${___name}" script "${___scr_file_}" "$@"
  623. # echo $_name
  624. # echo $_file
  625. }
  626. mcrypt_stream(){
  627. test $# -eq 2 || return 1
  628. case $1 in
  629. en)
  630. mcrypt --key $2 | base64 ;;
  631. de)
  632. base64 -d | mcrypt -d --key $2 ;;
  633. esac
  634. }
  635. gpg_stream(){
  636. test $# -eq 2 || return 1
  637. case $1 in
  638. en)
  639. gpg --passphrase $2 -c --batch |base64 ;;
  640. de)
  641. base64 -d|gpg --passphrase $2 -d --batch ;;
  642. esac
  643. }
  644. dgpg(){
  645. if test "$1" = help || test -z "$2"
  646. then
  647. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  648. return
  649. fi
  650. ___srcs="$2"
  651. ___dsts="$3"
  652. test -z "$___dsts" && ___dsts="${___srcs}.out"
  653. ___pw=
  654. echo -n "dgpg pw: "
  655. read -s ___pw
  656. echo ""
  657. test -z "$___pw" && return 1
  658. for f in *${___srcs}
  659. do
  660. ___d="$(basename "$f" "${___srcs}")${___dsts}"
  661. echo -n "Processing $f to $___d..."
  662. if test -d "$f"
  663. then
  664. echo "`printf 'failed (%s is directory)' $f`"
  665. elif test -f "$___d"
  666. then
  667. echo "`printf 'failed (%s is already exists)' $___d`"
  668. elif <"$f" gpg_stream $1 $___pw >"$___d" 2>/dev/null
  669. then
  670. echo "done"
  671. else
  672. echo "failed"
  673. test -f "$___d" && rm "$___d"
  674. fi
  675. done
  676. }
  677. alias enst="gpg_stream en"
  678. alias dest="gpg_stream de"
  679. showinfo(){
  680. echo "Japanese letters are 表示可能"
  681. __safe_run diskinfo
  682. ! $isdarwin && test -n "${DISPLAY}" && {
  683. __safe_run xrandr | \grep --color=never ^Screen
  684. }
  685. $iswindows || __safe_run finger $USER
  686. LANG=C __safe_runc id
  687. __safe_run xset q
  688. }
  689. x(){
  690. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  691. #mkdir -p ~/.var/log
  692. # nohup startx >~/.var/log/xorg.log 2>&1 &
  693. # exit
  694. exec startx
  695. else
  696. echo "X cant be started! Another X is already running?" 1>&2
  697. fi
  698. }
  699. bak(){
  700. for file in "$@"
  701. do
  702. cp -v ${file} ${file}.bak
  703. done
  704. }
  705. di(){
  706. if null type git
  707. then
  708. git --git-dir= diff "$@"
  709. ___diffcmd="git --git-dir= diff"
  710. else
  711. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  712. then
  713. ___diffcmd=colordiff
  714. else
  715. ___diffcmd=diff
  716. fi
  717. ${___diffcmd} -u "$@" | ${PAGER}
  718. fi
  719. }
  720. __trash_func(){
  721. __datenum=`date +%Y%m%d-%H%M%S`
  722. __tb="$HOME/.var/tb/$__datenum"
  723. mkdir -p "$__tb"
  724. mv "$@" "$__tb"
  725. }
  726. __safe_alias tb=__trash_func
  727. __safe_alias tb=trash
  728. mkdd(){
  729. _d=`date +%Y%m%d-%H%M%S` && \
  730. mkdir -p "$_d"
  731. }
  732. mkcd(){
  733. if test -z "$1"
  734. then
  735. echo "mkcd: usage: mkcd <dir>"
  736. return 1
  737. elif test -d "$1"
  738. then
  739. echo "Dir \"$1\" already exists."
  740. else
  741. mkdir -p "$1"
  742. echo "Dir \"$1\" created."
  743. fi
  744. cd "$1"
  745. }
  746. mkcdd(){
  747. # make and change date directory
  748. _d=`date +%Y%m%d-%H%M%S` && \
  749. mkcd "$_d"
  750. }
  751. if test -n "$TMUX" && null type reattach-to-user-namespace
  752. then
  753. alias pbpaste="reattach-to-user-namespace pbpaste"
  754. alias pbcopy="reattach-to-user-namespace pbcopy"
  755. fi
  756. __catclip(){
  757. if $iswindows
  758. then
  759. cat /dev/clipboard | tr -d \\r
  760. elif $isdarwin
  761. then
  762. pbpaste
  763. else
  764. xclip -o -selection "clipboard"
  765. fi
  766. }
  767. __setclip(){
  768. if test $# -eq 0
  769. then
  770. exec 3<&0
  771. else
  772. exec 3<<__EOF__
  773. `cat "$@"`
  774. __EOF__
  775. fi
  776. if $iswindows
  777. then
  778. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  779. elif $isdarwin
  780. then
  781. pbcopy 0<&3
  782. else
  783. 0<&3 xclip -i -f -selection "primary" | \
  784. xclip -i -f -selection "clipboard"
  785. fi
  786. exec 3<&-
  787. }
  788. # Now Homebrew 10sr/pkg/bin provides these commands:
  789. # Fallback to aliases if commands are not installed yet
  790. which catclip >/dev/null || alias catclip=__catclip
  791. which setclip >/dev/null || alias setclip=__setclip
  792. convmv_sjis2utf8_test(){
  793. convmv -r -f sjis -t utf8 *
  794. }
  795. convmv_sjis2utf8_notest(){
  796. convmv -r -f sjis -t utf8 * --notest
  797. }
  798. #################################################
  799. ## pastebin services
  800. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  801. sprunge(){
  802. # http://sprunge.us
  803. if test -z "$1"
  804. then
  805. curl -F 'sprunge=<-' http://sprunge.us
  806. else
  807. curl http://sprunge.us/$1
  808. fi
  809. }
  810. dpaste(){
  811. # http://dpaste.de
  812. if test -z "$1"
  813. then
  814. curl -F 'content=<-' https://dpaste.de/api/
  815. echo
  816. else
  817. curl https://dpaste.de/$1/raw/
  818. fi
  819. }
  820. ######################################
  821. ## Prompt Settings
  822. __my_moc_state(){
  823. type mocp >/dev/null 2>&1 || return
  824. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  825. printf "$1" "`mocp -Q %title 2>/dev/null`"
  826. }
  827. __my_parse_svn_branch() {
  828. ___svn_url=$(LANG=C svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  829. ___svn_repository_root=$(LANG=C svn info 2>/dev/null | \
  830. sed -ne 's#^Repository Root: ##p')
  831. echo ${___svn_url} | sed -e 's#^'"${___svn_repository_root}"'##g' | \
  832. awk '{print $1}'
  833. }
  834. __my_svn_ps1(){
  835. if svn status >/dev/null 2>&1
  836. then
  837. ___svn_branch=$(__my_parse_svn_branch)
  838. test -n "${___svn_branch}" && printf "$1" "{$___svn_branch}"
  839. fi
  840. }
  841. __my_battery_status(){
  842. ___dir=/sys/class/power_supply/BAT0
  843. if test -d $___dir && test -r $___dir/status && test -r $___dir/charge_full && \
  844. test -r $___dir/charge_now
  845. then
  846. ___st=$(cat $___dir/status)
  847. ___full=$(cat $___dir/charge_full)
  848. ___now=$(cat $___dir/charge_now)
  849. ___rate=$(expr "$___now" \* 100 / "$___full")
  850. printf "$1" "${___st}:${___rate}%"
  851. fi
  852. }
  853. __my_ps1_scale(){
  854. if null type stty && ! $ismsys
  855. then
  856. echo "[LxC:`stty size | tr -d $'\n' | tr " " x`]"
  857. fi
  858. }
  859. __my_ps1_tmux(){
  860. null type tmux || return $last
  861. ___tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  862. test -n "$TMUX" && echo "[TMUX:$___tmuxc]"
  863. }
  864. __my_ps1_moc(){
  865. __my_moc_state "[MOC:%s]"
  866. }
  867. for f in /usr/share/git/git-prompt.sh \
  868. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh \
  869. /usr/local/opt/bash-git-prompt/share/gitprompt.sh \
  870. "`brew --prefix 2>/dev/null`"/etc/bash_completion.d/git-prompt.sh \
  871. /usr/share/git-core/contrib/completion/git-prompt.sh \
  872. /usr/local/share/git-core/contrib/completion/git-prompt.sh \
  873. /etc/bash_completion.d/git-prompt \
  874. /opt/local/share/git-core/git-prompt.sh \
  875. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  876. do
  877. if test -r "$f" && ($inbash || $inzsh)
  878. then
  879. . "$f"
  880. break
  881. fi
  882. done
  883. export GIT_PS1_SHOWDIRTYSTATE=t
  884. export GIT_PS1_SHOWUPSTREAM=t
  885. __my_ps1_git(){
  886. null type __git_ps1 || return $last
  887. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  888. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  889. }
  890. __printf_ipaddr(){
  891. # ipaddress <fmt>
  892. type ip >/dev/null 2>&1 || return 1
  893. ___ip=$(LANG=C ip addr show scope global | \
  894. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  895. test -n "$___ip" && printf "$1" $___ip
  896. }
  897. alias addr="__printf_ipaddr '%s'"
  898. __my_ps1_ipaddr(){
  899. ! $iswindows && __printf_ipaddr '[Addr:%s]'
  900. }
  901. __my_ps1_bttry(){
  902. ___bst="$__homerun/batterystatus"
  903. if test -z "$DISPLAY" && ! $iswindows
  904. then
  905. test -f $___bst && ___bstr="$(cat $___bst)"
  906. test -n "$___bstr" && ! echo $___bstr | grep 100 >/dev/null 2>&1 && \
  907. echo "[Battery:${___bstr}]"
  908. __my_battery_status %s >$___bst &
  909. fi
  910. }
  911. __my_ps1_memo(){
  912. test -f memo.txt && echo "m:$(echo $(wc -c memo.txt) | cut -f 1 -d " ")"
  913. }
  914. __my_ps1_dirs(){
  915. dirs | wc -l
  916. }
  917. __my_ps1_jobs(){
  918. # __my_ps1_jobs [<num>]
  919. if test -n "$1"
  920. then
  921. jobs="$1"
  922. else
  923. jobs="`jobs | wc -l`"
  924. fi
  925. if test "$jobs" -gt 0
  926. then
  927. echo "JOBS:$jobs"
  928. fi
  929. }
  930. __my_ps1_dollar(){
  931. if test -z "$SHLVL"
  932. then
  933. printf "$1"
  934. else
  935. perl -e 'while($ARGV[0]-- > 0){print "$ARGV[1]";}' $SHLVL "$1"
  936. fi
  937. }
  938. __my_alert_fail(){
  939. test $laststatus -eq 0 || \
  940. echo "STATUS:${laststatus}"
  941. }
  942. # About ansi escape sequences
  943. # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
  944. # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
  945. if $inbash || $inzsh
  946. then
  947. if $inzsh
  948. then
  949. __attr_beg=$'%{\033['
  950. __attr_end='m%}'
  951. else
  952. __attr_beg='\[\033['
  953. __attr_end='m\]'
  954. fi
  955. __color_default="${__attr_beg}0${__attr_end}"
  956. __color_black="${__attr_beg}0;30${__attr_end}"
  957. __color_red="${__attr_beg}0;31${__attr_end}"
  958. __color_green="${__attr_beg}0;32${__attr_end}"
  959. __color_yellow="${__attr_beg}0;33${__attr_end}"
  960. __color_blue="${__attr_beg}0;34${__attr_end}"
  961. __color_magenta="${__attr_beg}0;35${__attr_end}"
  962. __color_cyan="${__attr_beg}0;36${__attr_end}"
  963. __color_white="${__attr_beg}0;37${__attr_end}"
  964. __color_light_black="${__attr_beg}1;30${__attr_end}"
  965. __color_light_red="${__attr_beg}1;31${__attr_end}"
  966. __color_light_green="${__attr_beg}1;32${__attr_end}"
  967. __color_light_yellow="${__attr_beg}1;33${__attr_end}"
  968. __color_light_blue="${__attr_beg}1;34${__attr_end}"
  969. __color_light_magenta="${__attr_beg}1;35${__attr_end}"
  970. __color_light_cyan="${__attr_beg}1;36${__attr_end}"
  971. __color_light_white="${__attr_beg}1;37${__attr_end}"
  972. __color_bg_black="${__attr_beg}40${__attr_end}"
  973. __color_bg_red="${__attr_beg}41${__attr_end}"
  974. __color_bg_green="${__attr_beg}42${__attr_end}"
  975. __color_bg_yellow="${__attr_beg}43${__attr_end}"
  976. __color_bg_blue="${__attr_beg}44${__attr_end}"
  977. __color_bg_magenta="${__attr_beg}45${__attr_end}"
  978. __color_bg_cyan="${__attr_beg}46${__attr_end}"
  979. __color_bg_white="${__attr_beg}47${__attr_end}"
  980. __attr_underline="${__attr_beg}4${__attr_end}"
  981. __attr_reverse="${__attr_beg}7${__attr_end}"
  982. __attr_bold="${__attr_beg}1${__attr_end}"
  983. fi
  984. # NOTE: tput is another easy way to set colors and background
  985. # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
  986. # color blue.
  987. # http://www.ibm.com/developerworks/jp/linux/aix/library/au-learningtput/index.html
  988. if test "$TERM" != dumb
  989. then
  990. __my_c1="$__attr_bold$__attr_underline" # color for PWD
  991. __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
  992. # color for ::
  993. if test -z "$_HOSTCOLOR_1"
  994. then
  995. # default color used by tmux status line
  996. _HOSTCOLOR_1=green
  997. fi
  998. eval "__my_c4=\${__color_light_${_HOSTCOLOR_1}}"
  999. __my_c5="$__color_black$__color_bg_white" # color for SCR
  1000. __my_cdef="$__color_default"
  1001. fi
  1002. if $inbash
  1003. then
  1004. __my_ps1_sh="[BASH:$BASH_VERSION]"
  1005. elif $inzsh
  1006. then
  1007. __my_ps1_sh="[ZSH:$ZSH_VERSION]"
  1008. fi
  1009. __my_ps1_info1(){
  1010. # first line of PS1
  1011. echo "${__my_ps1_sh}$(__my_ps1_scale)$(__my_ps1_git)$(__my_ps1_bttry)$(__my_ps1_moc)"
  1012. }
  1013. test -n "$__MY_SCRIPT" && \
  1014. __my_ps1_str_scr="SCR"
  1015. test -n "$SSH_CONNECTION" && \
  1016. __my_ps1_str_ssh="SSH"
  1017. test -n "$__MY_DTACH" && \
  1018. __my_ps1_str_dt="DT:`basename "$__MY_DTACH$"`"
  1019. __my_ps1_info2(){
  1020. # second line of PS1
  1021. echo $(__my_ps1_memo) $(__my_ps1_jobs) ${__my_ps1_str_scr} \
  1022. ${__my_ps1_str_ssh} ${__my_ps1_str_dt} $(__my_alert_fail) \
  1023. | sed -e 's/ /|/g'
  1024. }
  1025. __my_ps1_beg="${__my_c4}:: ${__my_cdef}"
  1026. __my_ps1_save_pos="\[\033[s\]"
  1027. __my_ps1_restore_pos="\[\033[u\]"
  1028. __my_ps1_move_rightmost="\[\033[\$(tput cols)C\]"
  1029. __my_ps1_move_15left="\[\033[15D\]"
  1030. # collapse when command line is too long and try to write over this string
  1031. # __my_ps1_right="${__my_ps1_save_pos}${__my_ps1_move_rightmost}"
  1032. # ${__my_ps1_move_15left}\D{%Y/%m/%d %H:%M}${__my_ps1_restore_pos}
  1033. if $inzsh
  1034. then
  1035. PROMPT="\
  1036. ${__my_ps1_beg}[${__my_c2}%n@%M${__my_cdef}:${__my_c1}%~/${__my_cdef}]\$(__my_ps1_info1)
  1037. ${__my_ps1_beg}\$(__my_ps1_info2) $(__my_ps1_dollar %#) "
  1038. RPROMPT="%D{%Y/%m/%d %H:%M}"
  1039. elif $inbash
  1040. then
  1041. PS1="\
  1042. ${__my_ps1_beg}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_info1)\n\
  1043. ${__my_ps1_beg}\D{%Y/%m/%d %H:%M} \$(__my_ps1_info2)${__my_ps1_right} $(__my_ps1_dollar \\$) "
  1044. else
  1045. true
  1046. # PS1="$(printf $(whoami)@$(hostname)$ )"
  1047. fi
  1048. ###################################
  1049. # set header and titles
  1050. __my_set_header_line(){
  1051. # save current position
  1052. printf "\033[s"
  1053. # move to 0,0
  1054. printf "\033[0;0H"
  1055. # clear curent to eol
  1056. printf "\033[K"
  1057. # inverse color
  1058. printf "\033[7m"
  1059. printf "$1"
  1060. # restore color
  1061. printf "\033[0m"
  1062. # restore saved position
  1063. printf "\033[u"
  1064. }
  1065. __my_set_screen_name(){
  1066. # set window name
  1067. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1068. then
  1069. echo -ne "\033k$1\033\\"
  1070. fi
  1071. }
  1072. __my_set_title(){
  1073. case $TERM in
  1074. (rxvt*|xterm*|aterm|screen*)
  1075. test -t 1 &&
  1076. test -z "$EMACS" &&
  1077. echo -n -e "\033]0;$1\007"
  1078. ;;
  1079. esac
  1080. }
  1081. if test -n "$TMUX"
  1082. then
  1083. # running tmux locally
  1084. __terminal_title="\$(basename \${PWD})"
  1085. elif test -n "$SSH_CONNECTION" && expr "$TERM" : '^screen' >/dev/null
  1086. then
  1087. # ssh connect from tmux terminal
  1088. __terminal_title="`whoami`@`hostname`:\$(basename \${PWD})"
  1089. else
  1090. __terminal_title="`whoami`@`hostname`:\${PWD}"
  1091. fi
  1092. if $inzsh
  1093. then
  1094. precmd(){
  1095. laststatus=$?
  1096. eval __my_set_title ${__terminal_title}
  1097. }
  1098. else
  1099. if test -n "$PROMPT_COMMAND" && ! expr "$PROMPT_COMMAND" : '.*; *$' >/dev/null
  1100. then
  1101. PROMPT_COMMAND="${PROMPT_COMMAND};"
  1102. fi
  1103. if test -n "$PROMPT_COMMAND"
  1104. then
  1105. PROMPT_COMMAND="laststatus=\$?;${PROMPT_COMMAND}__my_set_title \"${__terminal_title}\";"
  1106. else
  1107. PROMPT_COMMAND="laststatus=\$?;__my_set_title \"${__terminal_title}\";"
  1108. fi
  1109. fi
  1110. laststatus=0