You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shrc 27 KiB

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