您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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