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

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