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

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