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

shrc 29 KiB

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