選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

shrc 30 KiB

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