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

14 년 전
12 년 전
1 년 전
12 년 전
10 년 전
12 년 전
5 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
6 년 전
12 년 전
12 년 전
12 년 전
12 년 전
13 년 전
4 년 전
6 년 전
6 년 전
4 년 전
13 년 전
13 년 전
9 년 전
13 년 전
13 년 전
13 년 전
13 년 전
13 년 전
7 년 전
13 년 전
4 년 전
5 년 전
5 년 전
4 년 전
6 달 전
4 년 전
5 년 전
12 년 전
12 년 전
12 년 전
13 년 전
13 년 전
11 년 전
13 년 전
13 년 전
11 년 전
12 년 전
13 년 전
12 년 전
13 년 전
12 년 전
11 년 전
13 년 전
13 년 전
14 년 전
13 년 전
13 년 전
12 년 전
12 년 전
13 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
6 년 전
12 년 전
13 년 전
13 년 전
13 년 전
13 년 전
12 년 전
12 년 전
13 년 전
13 년 전
13 년 전
6 년 전
13 년 전
13 년 전
13 년 전
13 년 전
13 년 전
13 년 전
13 년 전
13 년 전
13 년 전
13 년 전
13 년 전
14 년 전
13 년 전
14 년 전
13 년 전
1 년 전
6 년 전
6 년 전
12 년 전
13 년 전
13 년 전
6 년 전
13 년 전
13 년 전
13 년 전
13 년 전
13 년 전
13 년 전
13 년 전
13 년 전
12 년 전
12 년 전
13 년 전
2 년 전
2 년 전
12 년 전
13 년 전
13 년 전
13 년 전
12 년 전
6 년 전
12 년 전
2 년 전
2 년 전
13 년 전
6 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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 pat
  17. # return 0 if pat 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. alias fcd='cd `find . -type d 2>/dev/null | fzf`'
  294. # Sometimes SHELL cannot be used. For example, when running bash inside zsh
  295. # SHELL is set to be /bin/zsh
  296. if $inbash
  297. then
  298. alias _reloadrc="rm '$__shrc_lock'; exec bash"
  299. elif $inzsh
  300. then
  301. alias _reloadrc="rm '$__shrc_lock'; exec zsh"
  302. else
  303. alias _reloadrc="rm '$__shrc_lock'; exec $SHELL"
  304. fi
  305. # alias mytime="date +%Y%m%d-%H%M%S"
  306. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  307. # some extra utilities
  308. __safe_alias mpg123="mpg123 -C -v --title"
  309. #export PLAYER="mpg123 -C -v --title"
  310. __safe_alias screen="screen -e^z^z"
  311. #alias zcd="cd \`zenity --file-selection --directory\`"
  312. __safe_alias gtags="gtags --verbose"
  313. __safe_alias htags="htags --xhtml --symbol --line-number \
  314. --frame --alphabet --verbose"
  315. __safe_alias au=aunpack
  316. # __safe_alias lv="lv|less"
  317. __safe_alias rs="rsync --progress --itemize-changes --compress"
  318. __safe_alias vagr=vagrant
  319. __safe_alias ztw='twitter set "`zenity --entry --title ztw --text Status:`"'
  320. if $isdarwin
  321. then
  322. alias updatedb="LC_ALL=C updatedb"
  323. # do not use locate installed by macports
  324. test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  325. fi
  326. # somehow not work
  327. # typeset -ga chpwd_functions
  328. # chpwd_functions+=pwd
  329. # chpwd(){
  330. # pwd
  331. # }
  332. cd(){
  333. builtin cd "$@" && pwd
  334. }
  335. root(){
  336. if test "`git rev-parse --is-inside-work-tree 2>/dev/null`" = true
  337. then
  338. cd "`git rev-parse --show-toplevel`"
  339. else
  340. cd /
  341. fi
  342. }
  343. __find_latest_vimdir(){
  344. vimdir=/usr/share/vim
  345. if test -d "$vimdir"
  346. then
  347. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  348. else
  349. echo ""
  350. fi
  351. }
  352. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  353. do
  354. test -n "$f" || continue
  355. f="$f/macros/less.sh"
  356. test -f $f && alias vl=$f && break
  357. done
  358. __safe_alias pa="pacman --color=always"
  359. __safe_alias pa="pacmatic --color=always"
  360. __safe_alias pa="pacapt"
  361. __safe_alias yt=yaourt
  362. # variable for yaourt
  363. null type pacmatic && export PACMAN="pacmatic"
  364. __safe_alias cower="cower --color=auto"
  365. __my_pacman_update_mirrorlist_with_reflector(){
  366. f=/etc/pacman.d/mirrorlist
  367. cmd="$(expr "$(grep -m 1 reflector $f)" : '# With: *\(.*\)')"
  368. if test -z "$cmd"
  369. then
  370. cmd="reflector --verbose --number 5 --sort rate --protocol http --protocol https --save $f"
  371. fi
  372. echo ">>> $cmd ..." 1>&2
  373. sudo sh -c "$cmd" && cat $f
  374. }
  375. null type reflector && test -d /etc/pacman.d && \
  376. alias reflect_mirrorlist=__my_pacman_update_mirrorlist_with_reflector
  377. null type apt-get && {
  378. alias aupgrade="sudo sh -xc 'apt-get autoremove --yes && \
  379. apt-get update --yes && apt-get upgrade --yes'"
  380. alias aptin="apt-get install"
  381. alias aptsearch="apt-cache search"
  382. alias aptshow="apt-cache show"
  383. }
  384. null type port && {
  385. alias port="port -v"
  386. alias pupgrade="sudo sh -xc 'port -v selfupdate && \
  387. port -v upgrade outdated'"
  388. }
  389. if $iscygwin
  390. then
  391. null type windate || \
  392. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  393. # alias cygsu="cygstart /cygwinsetup.exe"
  394. fi
  395. mkpatch(){
  396. if test $# -eq 0
  397. then
  398. echo "usage: mkpatch <olddir> <newdir>"
  399. elif ! test -d "$1"
  400. then
  401. echo "mkpatch: $1 is not a directory"
  402. elif ! test -d "$2"
  403. then
  404. echo "mkpatch: $2 is not a directory"
  405. else
  406. diff -Naur "$1" "$2"
  407. fi
  408. }
  409. __sdcv_less(){
  410. command sdcv -n "$@" | less --quit-if-one-screen
  411. }
  412. command -v sdcv >/dev/null && alias dict=__sdcv_less
  413. __safe_alias g="git "
  414. if null type _git && $inbash
  415. then
  416. # enable programmable completion for g
  417. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  418. || complete -o default -o nospace -F _git g
  419. fi
  420. #git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  421. __safe_alias m=gitmemo
  422. #alias setup.py3="sudo python3 setup.py install --record files.txt"
  423. randomstr(){
  424. len=$1
  425. test -z "$len" && len=8
  426. uuidgen | tr -d - | cut -c 1-len
  427. }
  428. datestr(){
  429. # datestr YYYYMMDDhhmmss
  430. if test -z "$1" || test "$1" = "-h"
  431. then
  432. echo "datestr: usage: datestr <YYYYMMDDhhmmss>"
  433. return 1
  434. fi
  435. # actual format for date command
  436. dfmt="`echo "$1" | \
  437. sed -e 's/YYYY/%Y/g' -e 's/yyyy/%Y/g' -e 's/YY/%y/g' -e 's/yy/%y/g' |\
  438. sed -e 's/MM/%m/g' -e 's/DD/%d/g' -e 's/dd/%d/g' | \
  439. 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'`"
  440. date +"$dfmt"
  441. }
  442. # ssh(){
  443. # # __my_terminal_title ssh
  444. # command ssh "$@"
  445. # }
  446. __ssh_with_cd(){
  447. # __ssh_with_cd <host> <directory> [<arg> ...]
  448. if test -z "$2"
  449. then
  450. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  451. return 1
  452. fi
  453. host="$1"
  454. shift
  455. dir="$1"
  456. shift
  457. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  458. }
  459. memo(){
  460. if test -z "$1"
  461. then
  462. _memo="memo.txt"
  463. else
  464. _memo="$1/memo.txt"
  465. fi
  466. $EDITOR "$_memo"
  467. if test -z "`cat "$_memo"`"
  468. then
  469. echo "$_memo is empty. Removing."
  470. rm "$_memo"
  471. fi
  472. }
  473. now(){
  474. ___tformat="%Y/%m/%d %H:%M:%S %z"
  475. cal
  476. REPLY=
  477. printf "\\r`date "+${___tformat}"`"
  478. read -t 1
  479. while test $? -ne 0
  480. do
  481. printf "\\r`date "+${___tformat}"`"
  482. read -t 1
  483. done
  484. }
  485. s(){
  486. if git rev-parse --git-dir >/dev/null 2>&1
  487. then
  488. echo ">> git grep -n $@" 1>&2
  489. git grep -n "$@"
  490. elif which ag >/dev/null 2>&1
  491. then
  492. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  493. ag --pager="$PAGER" "$@"
  494. elif which ack >/dev/null 2>&1
  495. then
  496. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  497. ack --pager="$PAGER" "$@"
  498. else
  499. echo \
  500. ">> find . " \
  501. "-path '*/.git' -prune -o" \
  502. "-path '*/.svn' -prune -o" \
  503. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  504. if test $# -eq 0
  505. then
  506. echo "No search word given." 1>&2
  507. return 1
  508. fi
  509. find . \
  510. -path '*/.git' -prune -o \
  511. -path '*/.svn' -prune -o \
  512. -type -f -exec grep -nH -e --color=always "$@" {} + \
  513. | $PAGER
  514. fi
  515. }
  516. MYMANPATH='/usr/lib/erlang/man'
  517. if $inbash || $inzsh
  518. then
  519. man(){
  520. env \
  521. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  522. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  523. LESS_TERMCAP_me=$(printf "\e[0m") \
  524. LESS_TERMCAP_se=$(printf "\e[0m") \
  525. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  526. LESS_TERMCAP_ue=$(printf "\e[0m") \
  527. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  528. MANPATH="`manpath`:$MYMANPATH" \
  529. man "$@"
  530. }
  531. fi
  532. netwait(){
  533. while ! ping -c 1 -t 1 example.com
  534. do
  535. true
  536. done
  537. echo network works.
  538. }
  539. __realpath(){
  540. if type realpath >/dev/null 2>&1
  541. then
  542. command realpath "$@"
  543. else
  544. while ! test -d $1
  545. do
  546. shift
  547. done
  548. (command cd "$d" && echo "$PWD")
  549. # local d="$OLDPWD"
  550. # command cd "$1"
  551. # echo "$PWD"
  552. # command cd "$d"
  553. fi
  554. }
  555. if $isdarwin
  556. then
  557. # I want to use env -u, but darwin env does not provide this option
  558. null type tmux && alias tmux='SHLVL= tmux'
  559. else
  560. null type tmux && alias tmux='env -u SHLVL tmux'
  561. fi
  562. tx(){
  563. tmux start-server
  564. if test -z "$1"
  565. then
  566. echo "usage: tx [<session_name>]"
  567. tmux list-sessions
  568. elif test -z "$TMUX"
  569. then
  570. # -A: attach if already exists
  571. # tmux new-session -A -c "$HOME" -s "$1"
  572. tmux new-session -A -s "$1"
  573. else
  574. # create new session if not exists yet
  575. # tmux has-session -t "$1" || TMUX= tmux new-session -d -c "$HOME" -s "$1"
  576. tmux has-session -t "$1" || TMUX= tmux new-session -d -s "$1"
  577. tmux switch-client -t "$1"
  578. fi
  579. }
  580. scr(){
  581. test -n "$1" && pf="${1}-"
  582. ___tformat="%Y%m%d-%H%M%S%z"
  583. ___file="${PWD}/${pf}`date +${___tformat}`.script"
  584. __MY_SCRIPT=${___file} script ${___file} "$@"
  585. }
  586. dtscr(){
  587. # dtscr <command ...>
  588. if test -z "$1"
  589. then
  590. echo "dtscr: usage: dtscr <command ...>"
  591. return 1
  592. fi
  593. ___cmdstr="`echo $@ | tr ' ' +`"
  594. ___tformat="%Y%m%d-%H%M%S%z"
  595. ___name="${pf}`date +${___tformat}`-${___cmdstr}"
  596. ___scr_file="${HOME}/${___name}.script"
  597. ___dt_dir="${TMP}/dtscr"
  598. mkdir -p "$___dt_dir"
  599. dtach -n "${___dt_dir}/${___name}" script "${___scr_file_}" "$@"
  600. # echo $_name
  601. # echo $_file
  602. }
  603. mcrypt_stream(){
  604. test $# -eq 2 || return 1
  605. case $1 in
  606. en)
  607. mcrypt --key $2 | base64 ;;
  608. de)
  609. base64 -d | mcrypt -d --key $2 ;;
  610. esac
  611. }
  612. gpg_stream(){
  613. test $# -eq 2 || return 1
  614. case $1 in
  615. en)
  616. gpg --passphrase $2 -c --batch |base64 ;;
  617. de)
  618. base64 -d|gpg --passphrase $2 -d --batch ;;
  619. esac
  620. }
  621. dgpg(){
  622. if test "$1" = help || test -z "$2"
  623. then
  624. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  625. return
  626. fi
  627. ___srcs="$2"
  628. ___dsts="$3"
  629. test -z "$___dsts" && ___dsts="${___srcs}.out"
  630. ___pw=
  631. echo -n "dgpg pw: "
  632. read -s ___pw
  633. echo ""
  634. test -z "$___pw" && return 1
  635. for f in *${___srcs}
  636. do
  637. ___d="$(basename "$f" "${___srcs}")${___dsts}"
  638. echo -n "Processing $f to $___d..."
  639. if test -d "$f"
  640. then
  641. echo "`printf 'failed (%s is directory)' $f`"
  642. elif test -f "$___d"
  643. then
  644. echo "`printf 'failed (%s is already exists)' $___d`"
  645. elif <"$f" gpg_stream $1 $___pw >"$___d" 2>/dev/null
  646. then
  647. echo "done"
  648. else
  649. echo "failed"
  650. test -f "$___d" && rm "$___d"
  651. fi
  652. done
  653. }
  654. alias enst="gpg_stream en"
  655. alias dest="gpg_stream de"
  656. showinfo(){
  657. echo "Japanese letters are 表示可能"
  658. __safe_run diskinfo
  659. ! $isdarwin && test -n "${DISPLAY}" && {
  660. __safe_run xrandr | \grep --color=never ^Screen
  661. }
  662. $iswindows || __safe_run finger $USER
  663. LANG=C __safe_run id
  664. __safe_run xset q
  665. }
  666. x(){
  667. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  668. #mkdir -p ~/.var/log
  669. # nohup startx >~/.var/log/xorg.log 2>&1 &
  670. # exit
  671. exec startx
  672. else
  673. echo "X cant be started! Another X is already running?" 1>&2
  674. fi
  675. }
  676. bak(){
  677. for file in "$@"
  678. do
  679. cp -v ${file} ${file}.bak
  680. done
  681. }
  682. di(){
  683. if null type git
  684. then
  685. git --git-dir= diff "$@"
  686. ___diffcmd="git --git-dir= diff"
  687. else
  688. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  689. then
  690. ___diffcmd=colordiff
  691. else
  692. ___diffcmd=diff
  693. fi
  694. ${___diffcmd} -u "$@" | ${PAGER}
  695. fi
  696. }
  697. __trash_func(){
  698. __datenum=`date +%Y%m%d-%H%M%S`
  699. __tb="$HOME/.var/tb/$__datenum"
  700. mkdir -p "$__tb"
  701. mv "$@" "$__tb"
  702. }
  703. __safe_alias tb=__trash_func
  704. __safe_alias tb=trash
  705. mkdd(){
  706. _d=`date +%Y%m%d-%H%M%S` && \
  707. mkdir -p "$_d"
  708. }
  709. mkcd(){
  710. if test -z "$1"
  711. then
  712. echo "mkcd: usage: mkcd <dir>"
  713. return 1
  714. elif test -d "$1"
  715. then
  716. echo "Dir \"$1\" already exists."
  717. else
  718. mkdir -p "$1"
  719. echo "Dir \"$1\" created."
  720. fi
  721. cd "$1"
  722. }
  723. mkcdd(){
  724. # make and change date directory
  725. _d=`date +%Y%m%d-%H%M%S` && \
  726. mkcd "$_d"
  727. }
  728. __catclip(){
  729. if $iswindows
  730. then
  731. cat /dev/clipboard | tr -d \\r
  732. elif $isdarwin
  733. then
  734. pbpaste
  735. else
  736. xclip -o -selection "clipboard"
  737. fi
  738. }
  739. __setclip(){
  740. if test $# -eq 0
  741. then
  742. exec 3<&0
  743. else
  744. exec 3<<__EOF__
  745. `cat "$@"`
  746. __EOF__
  747. fi
  748. if $iswindows
  749. then
  750. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  751. elif $isdarwin
  752. then
  753. pbcopy 0<&3
  754. else
  755. 0<&3 xclip -i -f -selection "primary" | \
  756. xclip -i -f -selection "clipboard"
  757. fi
  758. exec 3<&-
  759. }
  760. # Now Homebrew 10sr/pkg/bin provides these commands:
  761. # Fallback to aliases if commands are not installed yet
  762. which catclip >/dev/null || alias catclip=__catclip
  763. which setclip >/dev/null || alias setclip=__setclip
  764. convmv_sjis2utf8_test(){
  765. convmv -r -f sjis -t utf8 *
  766. }
  767. convmv_sjis2utf8_notest(){
  768. convmv -r -f sjis -t utf8 * --notest
  769. }
  770. if test -f /Applications/Vivaldi.app/Contents/MacOS/Vivaldi && null type snowsql
  771. then
  772. # Use Vivaldi for snowsql external auth login
  773. alias snowsql="BROWSER=/Applications/Vivaldi.app/Contents/MacOS/Vivaldi snowsql"
  774. fi
  775. #################################################
  776. ## pastebin services
  777. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  778. sprunge(){
  779. # http://sprunge.us
  780. if test -z "$1"
  781. then
  782. curl -F 'sprunge=<-' http://sprunge.us
  783. else
  784. curl http://sprunge.us/$1
  785. fi
  786. }
  787. dpaste(){
  788. # http://dpaste.de
  789. if test -z "$1"
  790. then
  791. curl -F 'content=<-' https://dpaste.de/api/
  792. echo
  793. else
  794. curl https://dpaste.de/$1/raw/
  795. fi
  796. }
  797. ######################################
  798. ## Prompt Settings
  799. __my_moc_state(){
  800. type mocp >/dev/null 2>&1 || return
  801. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  802. printf "$1" "`mocp -Q %title 2>/dev/null`"
  803. }
  804. __my_parse_svn_branch() {
  805. ___svn_url=$(LANG=C svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  806. ___svn_repository_root=$(LANG=C svn info 2>/dev/null | \
  807. sed -ne 's#^Repository Root: ##p')
  808. echo ${___svn_url} | sed -e 's#^'"${___svn_repository_root}"'##g' | \
  809. awk '{print $1}'
  810. }
  811. __my_svn_ps1(){
  812. if svn status >/dev/null 2>&1
  813. then
  814. ___svn_branch=$(__my_parse_svn_branch)
  815. test -n "${___svn_branch}" && printf "$1" "{$___svn_branch}"
  816. fi
  817. }
  818. __my_battery_status(){
  819. ___dir=/sys/class/power_supply/BAT0
  820. if test -d $___dir && test -r $___dir/status && test -r $___dir/charge_full && \
  821. test -r $___dir/charge_now
  822. then
  823. ___st=$(cat $___dir/status)
  824. ___full=$(cat $___dir/charge_full)
  825. ___now=$(cat $___dir/charge_now)
  826. ___rate=$(expr "$___now" \* 100 / "$___full")
  827. printf "$1" "${___st}:${___rate}%"
  828. fi
  829. }
  830. __my_ps1_scale(){
  831. if null type stty && ! $ismsys
  832. then
  833. echo "[LxC:`stty size | tr -d $'\n' | tr " " x`]"
  834. fi
  835. }
  836. __my_ps1_tmux(){
  837. null type tmux || return $last
  838. ___tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  839. test -n "$TMUX" && echo "[TMUX:$___tmuxc]"
  840. }
  841. __my_ps1_moc(){
  842. __my_moc_state "[MOC:%s]"
  843. }
  844. for f in /usr/share/git/git-prompt.sh \
  845. "`brew --prefix 2>/dev/null`"/etc/bash_completion.d/git-prompt.sh \
  846. /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh \
  847. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh \
  848. /usr/local/opt/bash-git-prompt/share/gitprompt.sh \
  849. /usr/share/git-core/contrib/completion/git-prompt.sh \
  850. /usr/local/share/git-core/contrib/completion/git-prompt.sh \
  851. /etc/bash_completion.d/git-prompt \
  852. /opt/local/share/git-core/git-prompt.sh \
  853. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  854. do
  855. if test -r "$f" && ($inbash || $inzsh)
  856. then
  857. . "$f"
  858. break
  859. fi
  860. done
  861. export GIT_PS1_SHOWDIRTYSTATE=t
  862. export GIT_PS1_SHOWUPSTREAM=t
  863. __my_ps1_git(){
  864. null type __git_ps1 || return $last
  865. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  866. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  867. }
  868. __printf_ipaddr(){
  869. # ipaddress <fmt>
  870. type ip >/dev/null 2>&1 || return 1
  871. ___ip=$(LANG=C ip addr show scope global | \
  872. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  873. test -n "$___ip" && printf "$1" $___ip
  874. }
  875. alias addr="__printf_ipaddr '%s'"
  876. __my_ps1_ipaddr(){
  877. ! $iswindows && __printf_ipaddr '[Addr:%s]'
  878. }
  879. __my_ps1_bttry(){
  880. ___bst="$__homerun/batterystatus"
  881. if test -z "$DISPLAY" && ! $iswindows
  882. then
  883. test -f $___bst && ___bstr="$(cat $___bst)"
  884. test -n "$___bstr" && ! echo $___bstr | grep 100 >/dev/null 2>&1 && \
  885. echo "[Battery:${___bstr}]"
  886. __my_battery_status %s >$___bst &
  887. fi
  888. }
  889. __my_ps1_memo(){
  890. test -f memo.txt && echo "m:$(echo $(wc -c memo.txt) | cut -f 1 -d " ")"
  891. }
  892. __my_ps1_dirs(){
  893. dirs | wc -l
  894. }
  895. __my_ps1_jobs(){
  896. # __my_ps1_jobs [<num>]
  897. if test -n "$1"
  898. then
  899. jobs="$1"
  900. else
  901. jobs="`jobs | wc -l`"
  902. fi
  903. if test "$jobs" -gt 0
  904. then
  905. echo "JOBS:$jobs"
  906. fi
  907. }
  908. __my_ps1_dollar(){
  909. if test -z "$2"
  910. then
  911. printf "$1"
  912. else
  913. perl -e 'while($ARGV[0]-- > 0){print "$ARGV[1]";}' "$2" "$1"
  914. fi
  915. }
  916. __my_alert_fail(){
  917. test $laststatus -eq 0 || \
  918. echo "STATUS:${laststatus}"
  919. }
  920. # About ansi escape sequences
  921. # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
  922. # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
  923. if $inbash || $inzsh
  924. then
  925. if $inzsh
  926. then
  927. __attr_beg=$'%{\033['
  928. __attr_end='m%}'
  929. else
  930. __attr_beg='\[\033['
  931. __attr_end='m\]'
  932. fi
  933. __color_default="${__attr_beg}0${__attr_end}"
  934. __color_black="${__attr_beg}0;30${__attr_end}"
  935. __color_red="${__attr_beg}0;31${__attr_end}"
  936. __color_green="${__attr_beg}0;32${__attr_end}"
  937. __color_yellow="${__attr_beg}0;33${__attr_end}"
  938. __color_blue="${__attr_beg}0;34${__attr_end}"
  939. __color_magenta="${__attr_beg}0;35${__attr_end}"
  940. __color_cyan="${__attr_beg}0;36${__attr_end}"
  941. __color_white="${__attr_beg}0;37${__attr_end}"
  942. __color_light_black="${__attr_beg}1;30${__attr_end}"
  943. __color_light_red="${__attr_beg}1;31${__attr_end}"
  944. __color_light_green="${__attr_beg}1;32${__attr_end}"
  945. __color_light_yellow="${__attr_beg}1;33${__attr_end}"
  946. __color_light_blue="${__attr_beg}1;34${__attr_end}"
  947. __color_light_magenta="${__attr_beg}1;35${__attr_end}"
  948. __color_light_cyan="${__attr_beg}1;36${__attr_end}"
  949. __color_light_white="${__attr_beg}1;37${__attr_end}"
  950. __color_bg_black="${__attr_beg}40${__attr_end}"
  951. __color_bg_red="${__attr_beg}41${__attr_end}"
  952. __color_bg_green="${__attr_beg}42${__attr_end}"
  953. __color_bg_yellow="${__attr_beg}43${__attr_end}"
  954. __color_bg_blue="${__attr_beg}44${__attr_end}"
  955. __color_bg_magenta="${__attr_beg}45${__attr_end}"
  956. __color_bg_cyan="${__attr_beg}46${__attr_end}"
  957. __color_bg_white="${__attr_beg}47${__attr_end}"
  958. __attr_underline="${__attr_beg}4${__attr_end}"
  959. __attr_reverse="${__attr_beg}7${__attr_end}"
  960. __attr_bold="${__attr_beg}1${__attr_end}"
  961. fi
  962. # NOTE: tput is another easy way to set colors and background
  963. # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
  964. # color blue.
  965. # http://www.ibm.com/developerworks/jp/linux/aix/library/au-learningtput/index.html
  966. if test "$TERM" != dumb
  967. then
  968. __my_c1="$__attr_bold$__attr_underline" # color for PWD
  969. __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
  970. # color for ::
  971. if test -z "$_HOSTCOLOR_1"
  972. then
  973. # default color used by tmux status line
  974. _HOSTCOLOR_1=green
  975. fi
  976. eval "__my_c4=\${__color_light_${_HOSTCOLOR_1}}"
  977. __my_c5="$__color_black$__color_bg_white" # color for SCR
  978. __my_cdef="$__color_default"
  979. fi
  980. if $inbash
  981. then
  982. __my_ps1_sh="[BASH:$BASH_VERSION]"
  983. elif $inzsh
  984. then
  985. __my_ps1_sh="[ZSH:$ZSH_VERSION]"
  986. fi
  987. __my_ps1_info1(){
  988. # first line of PS1
  989. echo "${__my_ps1_sh}$(__my_ps1_scale)$(__my_ps1_git)"
  990. }
  991. test -n "$__MY_SCRIPT" && \
  992. __my_ps1_str_scr="SCR"
  993. test -n "$SSH_CONNECTION" && \
  994. __my_ps1_str_ssh="SSH"
  995. test -n "$__MY_DTACH" && \
  996. __my_ps1_str_dt="DT:`basename "$__MY_DTACH"`"
  997. __my_ps1_info2(){
  998. # second line of PS1
  999. echo $(__my_ps1_memo) $(__my_ps1_jobs) ${__my_ps1_str_scr} \
  1000. ${__my_ps1_str_ssh} ${__my_ps1_str_dt} $(__my_alert_fail) \
  1001. | sed -e 's/ /|/g'
  1002. }
  1003. __my_ps1_beg="${__my_c4}:: ${__my_cdef}"
  1004. __my_ps1_save_pos="\[\033[s\]"
  1005. __my_ps1_restore_pos="\[\033[u\]"
  1006. __my_ps1_move_rightmost="\[\033[\$(tput cols)C\]"
  1007. __my_ps1_move_15left="\[\033[15D\]"
  1008. # collapse when command line is too long and try to write over this string
  1009. # __my_ps1_right="${__my_ps1_save_pos}${__my_ps1_move_rightmost}"
  1010. # ${__my_ps1_move_15left}\D{%Y/%m/%d %H:%M}${__my_ps1_restore_pos}
  1011. if $inzsh
  1012. then
  1013. PROMPT="\
  1014. ${__my_ps1_beg}[${__my_c2}%n@%M${__my_cdef}:${__my_c1}%~/${__my_cdef}]\$(__my_ps1_info1)
  1015. ${__my_ps1_beg}\$(__my_ps1_info2) $(__my_ps1_dollar %# $SHLVL) "
  1016. RPROMPT="%D{%Y/%m/%d %H:%M}"
  1017. elif $inbash
  1018. then
  1019. PS1="\
  1020. ${__my_ps1_beg}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_info1)\n\
  1021. ${__my_ps1_beg}\D{%Y/%m/%d %H:%M} \$(__my_ps1_info2)${__my_ps1_right} $(__my_ps1_dollar \\$ $SHLVL) "
  1022. else
  1023. true
  1024. # PS1="$(printf $(whoami)@$(hostname)$ )"
  1025. fi
  1026. ###################################
  1027. # set header and titles
  1028. __my_set_header_line(){
  1029. # save current position
  1030. printf "\033[s"
  1031. # move to 0,0
  1032. printf "\033[0;0H"
  1033. # clear curent to eol
  1034. printf "\033[K"
  1035. # inverse color
  1036. printf "\033[7m"
  1037. printf "$1"
  1038. # restore color
  1039. printf "\033[0m"
  1040. # restore saved position
  1041. printf "\033[u"
  1042. }
  1043. __my_set_screen_name(){
  1044. # set window name
  1045. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1046. then
  1047. echo -ne "\033k$1\033\\"
  1048. fi
  1049. }
  1050. __my_set_title(){
  1051. case $TERM in
  1052. (rxvt*|xterm*|aterm|screen*)
  1053. test -t 1 &&
  1054. test -z "$EMACS" &&
  1055. echo -n -e "\033]0;$1\007"
  1056. ;;
  1057. esac
  1058. }
  1059. if test -n "$TMUX"
  1060. then
  1061. # running tmux locally
  1062. __terminal_title="\$(basename \${PWD})"
  1063. elif test -n "$SSH_CONNECTION" && expr "$TERM" : '^screen' >/dev/null
  1064. then
  1065. # ssh connect from tmux terminal
  1066. __terminal_title="`whoami`@`hostname`:\$(basename \${PWD})"
  1067. else
  1068. __terminal_title="`whoami`@`hostname`:\${PWD}"
  1069. fi
  1070. if $inzsh
  1071. then
  1072. precmd(){
  1073. laststatus=$?
  1074. eval __my_set_title ${__terminal_title}
  1075. }
  1076. else
  1077. if test -n "$PROMPT_COMMAND" && ! expr "$PROMPT_COMMAND" : '.*; *$' >/dev/null
  1078. then
  1079. PROMPT_COMMAND="${PROMPT_COMMAND};"
  1080. fi
  1081. if test -n "$PROMPT_COMMAND"
  1082. then
  1083. PROMPT_COMMAND="laststatus=\$?;${PROMPT_COMMAND}__my_set_title \"${__terminal_title}\";"
  1084. else
  1085. PROMPT_COMMAND="laststatus=\$?;__my_set_title \"${__terminal_title}\";"
  1086. fi
  1087. fi
  1088. laststatus=0