25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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