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