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

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