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

shrc 30 KiB

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