Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

bashrc 24 KiB

13 år sedan
13 år sedan
11 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
11 år sedan
11 år sedan
12 år sedan
11 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
11 år sedan
12 år sedan
12 år sedan
13 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
11 år sedan
12 år sedan
13 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
11 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
11 år sedan
11 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
13 år sedan
12 år sedan
11 år sedan
12 år sedan
11 år sedan
11 år sedan
12 år sedan
12 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
11 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
13 år sedan
12 år sedan
13 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
13 år sedan
12 år sedan
13 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
13 år sedan
12 år sedan
12 år sedan
12 år sedan
13 år sedan
12 år sedan
13 år sedan
12 år sedan
13 år sedan
12 år sedan
13 år sedan
12 år sedan
13 år sedan
12 år sedan
12 år sedan
11 år sedan
11 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
11 år sedan
12 år sedan
11 år sedan
12 år sedan
11 år sedan
11 år sedan
12 år sedan
11 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. #!/bin/bash
  2. # TODO: use tput
  3. # If not running interactively, don't do anything
  4. [[ $- != *i* ]] && return
  5. ##########################################
  6. null(){
  7. "$@" >/dev/null 2>&1
  8. }
  9. __try_exec(){
  10. type $1 >/dev/null 2>&1 && "$@"
  11. }
  12. ##########################
  13. # system type
  14. alias ismsys=false
  15. alias iscygwin=false
  16. alias iswindows=false
  17. alias isdarwin=false
  18. alias islinux=false
  19. alias with_coreutils=false # for mac
  20. case `uname` in
  21. (MINGW*) alias ismsys=true ;;
  22. (CYGWIN*) alias iscygwin=true ;;
  23. (Darwin*) alias isdarwin=true ;;
  24. (Linux*) alias islinux=true ;;
  25. esac
  26. null ls --version && alias with_coreutils=true
  27. ( ismsys || iscygwin ) && alias iswindows=true
  28. alias inbash=false
  29. alias inzsh=false
  30. if test -n "$BASH_VERSION"
  31. then
  32. alias inbash=true
  33. elif test -n "$ZSH_VERSION"
  34. then
  35. alias inzsh=true
  36. fi
  37. #################################
  38. __match(){
  39. # __match str1 str2
  40. # return 0 if str2 is found in str1
  41. expr "$1" : ".*$2.*" >/dev/null
  42. }
  43. if ! __match "$PATH" "$HOME/.local/bin"
  44. then
  45. if test -d "$HOME/.local/bin"
  46. then
  47. PATH="${HOME}/.local/bin:${PATH}"
  48. fi
  49. if test -d "$HOME/.local/lib/gems/bin"
  50. then
  51. PATH="$HOME/.local/lib/gems/bin:${PATH}"
  52. fi
  53. fi
  54. if ismsys && ! __match "$PATH" /c/mingw/bin
  55. then
  56. PATH="$PATH:/c/mingw/bin:/c/mingw/msys/1.0/bin"
  57. fi
  58. # # it is not so good
  59. # # http://archive.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/shared-libraries.html
  60. # # http://superuser.com/questions/324613/installing-a-library-locally-in-home-directory-but-program-doesnt-recognize-it
  61. # without this ENV i cannot run tmux. another way is to use --disable-shared
  62. # when building tmux
  63. if ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
  64. then
  65. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/.local/lib"
  66. fi
  67. ##################################
  68. export LANG=ja_JP.UTF-8
  69. export LC_MESSAGES=C
  70. export TERMCAP="${TERMCAP}:vb="
  71. ismsys && export HOSTNAME
  72. export ENV=~/.shrc
  73. if false iswindows
  74. then
  75. export PAGER='tr -d \\r | less'
  76. else
  77. export PAGER="less"
  78. fi
  79. export LESS="-iRMX"
  80. _src_hilite_lp_path="`which src-hilite-lesspipe.sh 2>/dev/null`"
  81. if test -n "$_src_hilite_lp_path"
  82. then
  83. export LESSOPEN="| $_src_hilite_lp_path %s"
  84. fi
  85. if null type vim
  86. then
  87. export EDITOR=vim
  88. else
  89. export EDITOR=vi
  90. fi
  91. # export CDPATH=".:~"
  92. export VISUAL="$EDITOR"
  93. export GIT_PAGER="less -FS"
  94. export GIT_EDITOR="$EDITOR"
  95. export GIT_MERGE_AUTOEDIT=no
  96. if test -n "$TMUX" && \
  97. echo $TERM | grep screen >/dev/null 2>&1 && \
  98. tmux display -p '#{client_termname}' | grep 256color >/dev/null 2>&1
  99. then
  100. TERM=screen-256color
  101. fi
  102. if test -z "$TMP"
  103. then
  104. if test -n "$TMPDIR"
  105. then
  106. export TMP=$TMPDIR
  107. elif test -n "$TEMP"
  108. then
  109. export TMP="$TEMP"
  110. else
  111. export TMP=/tmp
  112. fi
  113. fi
  114. __match "$TMP" "${USER}-tmp" >/dev/null || export TMP="${TMP}/${USER}-tmp"
  115. export TEMP="$TMP"
  116. mkdir -p "$TMP"
  117. ! iswindows && null type stty && {
  118. stty stop undef # unbind C-s to stop displaying output
  119. # stty erase '^h'
  120. }
  121. if iswindows; then
  122. export USER=$USERNAME
  123. fi
  124. _tmux_prefs(){
  125. null type tmux || return 1
  126. tmux set -g mode-keys vi
  127. }
  128. if test -d ~/dbx
  129. then
  130. export CHIT_PATH="$HOME/dbx/.chit"
  131. fi
  132. #######################
  133. iswindows && alias tty="echo cmd.exe"
  134. type fortune >/dev/null 2>&1 && {
  135. fortune
  136. echo
  137. fortune -o
  138. echo
  139. }
  140. uname -a
  141. echo TERM $TERM $(tput colors) colors connected to $(tty), \
  142. running $BASH $BASH_VERSION
  143. if test -n "$TMUX"
  144. then
  145. tmux display -p 'Using tmux #S:#I:#W.#P, client is #{client_termname}' \
  146. 2>/dev/null
  147. echo
  148. fi
  149. ###################################
  150. # some aliases and functions
  151. ( ! with_coreutils && isdarwin ) || test "$TERM" = dumb || \
  152. _coloroption=" --color=auto"
  153. ( ! with_coreutils && isdarwin ) || iswindows || \
  154. _timeoption=" --time-style=long-iso"
  155. ( ! with_coreutils && isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
  156. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  157. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  158. _timeformat_num="%Y%m%d%H%M%S"
  159. alias datenum="date +$_timeformat_num"
  160. alias ls="ls -hCF${_coloroption}${_timeoption}"
  161. if ! with_coreutils
  162. then
  163. export LSCOLORS=gxfxcxdxbxegedabagacad
  164. alias ls="ls -G"
  165. fi
  166. # export GREP_OPTIONS=""
  167. alias gr="grep -n --color=always"
  168. iswindows && alias grep="grep -n"
  169. # alias ll="ls -l"
  170. # alias la="ls -A"
  171. # alias lla="ls -Al"
  172. alias less="less -F"
  173. null type emacs && alias em="emacs -nw"
  174. null type vim && alias vi=vim
  175. alias pstree="LANG=C pstree"
  176. alias cp="cp -v"
  177. alias mv="mv -v"
  178. alias rm="rm -v"
  179. alias psaux="ps auxww"
  180. alias q=exit
  181. null type e3em && alias e3=e3em
  182. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  183. alias po=popd
  184. alias pu=pushd
  185. null type sudo && alias sudo="sudo " # use aliases through sudo
  186. null type sudoedit && alias sudoe="sudoedit"
  187. null type halt && alias halt="sudo halt"
  188. null type reboot && alias reboot="sudo reboot"
  189. null type dbus-send && {
  190. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  191. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  192. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  193. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  194. }
  195. alias rand="echo \$RANDOM"
  196. null type file-roller && alias xunp="file-roller -h"
  197. null type paco && alias pc="sudo \paco -D"
  198. alias pycalc="python -i -c 'from math import *' "
  199. null type python3 && alias py3=python3
  200. null type python2 && alias py2=python2
  201. alias _reloadrc="exec \"$SHELL\""
  202. # alias mytime="date +%Y%m%d-%H%M%S"
  203. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  204. # type trash >/dev/null 2>&1 && alias rm=trash
  205. null type mpg123 && alias mpg123="mpg123 -C -v --title"
  206. null type xmms2 && alias xm="xmms2"
  207. #export PLAYER="mpg123 -C -v --title"
  208. null type screen && alias screen="screen -e^z^z"
  209. #alias zcd="cd \`zenity --file-selection --directory\`"
  210. null type gtags && alias gtags="gtags --verbose"
  211. null type htags && alias htags="htags --xhtml --symbol --line-number \
  212. --frame --alphabet --verbose"
  213. null type aunpack && alias au=aunpack
  214. null type lv && alias lv="lv|less"
  215. null type rsync && alias rs="rsync --progress --itemize-changes --compress"
  216. isdarwin && alias updatedb="LC_ALL=C updatedb"
  217. # do not use locate installed by macports
  218. isdarwin && test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  219. # pad
  220. alias pad=notepad
  221. null type gedit && alias pad=gedit
  222. null type leafpad && alias pad=leafpad
  223. isdarwin && alias pad="open -e"
  224. null type wicd-curses && alias wic=wicd-curses
  225. null type wicd-cli && alias wil="wicd-cli -y -l | head"
  226. #alias wicn="wicd-cli -y -c -n"
  227. wicn(){
  228. if test $# -eq 0
  229. then
  230. local num
  231. wicd-cli -y -l | head
  232. echo -n "input num: "
  233. read num
  234. test -n "$num" && wicd-cli -y -c -n $num
  235. else
  236. wicd-cli -y -c -n $1
  237. fi
  238. }
  239. for f in /usr/share/vim/vimcurrent/macros/less.sh \
  240. /usr/share/vim/vim73/macros/less.sh \
  241. /usr/share/vim/vim72/macros/less.sh
  242. do
  243. test -f $f && alias vl=$f && break
  244. done
  245. alias pa=pacapt
  246. null type yaourt && alias yt=yaourt
  247. null type cower && alias cower="cower --color=auto"
  248. null type pacmatic && {
  249. alias pacman="pacmatic"
  250. export PACMAN="pacmatic"
  251. }
  252. _pacman_update_mirrorlist_with_reflector(){
  253. ml=/etc/pacman.d/mirrorlist
  254. cmd="$(expr "$(grep -m 1 reflector $ml)" : '# With: *\(.*\)')"
  255. if test -z "$cmd"
  256. then
  257. cmd="reflector --verbose -l 5 --sort rate --save $ml"
  258. fi
  259. echo "Running $cmd ..." 1>&2
  260. sudo $cmd
  261. }
  262. null type reflector && test -f /etc/pacman.d/mirrorlist && \
  263. alias reflect_mirrorlist=_pacman_update_mirrorlist_with_reflector
  264. null type apt-get && {
  265. alias aupgrade="sudo apt-get autoremove --yes && \
  266. sudo apt-get update --yes && sudo apt-get upgrade --yes"
  267. alias aptin="apt-get install"
  268. alias aptsearch="apt-cache search"
  269. alias aptshow="apt-cache show"
  270. }
  271. null type port && {
  272. alias port="port -v"
  273. alias pupgrade="sudo port -v selfupdate && \
  274. { sudo port -v upgrade outdated; }"
  275. }
  276. if iscygwin; then
  277. null type windate || \
  278. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  279. # alias cygsu="cygstart /cygwinsetup.exe"
  280. # alias ls="ls -CFG $(iswindows || test "$TERM" = dumb || echo --color=auto)"
  281. fi
  282. g(){
  283. if test $# -eq 0 && null type git-info
  284. then
  285. git info
  286. else
  287. git -c color.ui=always "$@"
  288. fi
  289. }
  290. if null type _git && inbash
  291. then
  292. # enable programmable completion for g
  293. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  294. || complete -o default -o nospace -F _git g
  295. fi
  296. git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  297. null type gitmemo && alias m=gitmemo
  298. alias setup.py="sudo python3 setup.py install --record files.txt"
  299. datestr(){
  300. # datestr yyyyMMdd-hhmmss
  301. if test -z "$1" || test "$1" == "-h"
  302. then
  303. echo "datestr: usage: datestr <yyyyMMddhhmmss>"
  304. return 1
  305. fi
  306. dfmt= # actual format for date command
  307. while test -n "$1"
  308. do
  309. fmt="$1"
  310. while test -n "$fmt"
  311. do
  312. case "$fmt" in
  313. yyyy*) # year
  314. dfmt="${dfmt}%Y"
  315. fmt="`echo "$fmt" | cut -c 5-`"
  316. ;;
  317. yy*) # last two digits of year
  318. dfmt="${dfmt}%y"
  319. fmt="`echo "$fmt" | cut -c 3-`"
  320. ;;
  321. MM*) # month (01..12)
  322. dfmt="${dfmt}%m"
  323. fmt="`echo "$fmt" | cut -c 3-`"
  324. ;;
  325. dd*) # day of month (01..12)
  326. dfmt="${dfmt}%d"
  327. fmt="`echo "$fmt" | cut -c 3-`"
  328. ;;
  329. HH* | hh*) # hour (00..23)
  330. dfmt="${dfmt}%H"
  331. fmt="`echo "$fmt" | cut -c 3-`"
  332. ;;
  333. mm*) # minute (00..59)
  334. dfmt="${dfmt}%M"
  335. fmt="`echo "$fmt" | cut -c 3-`"
  336. ;;
  337. ss*) # second (00..60)
  338. dfmt="${dfmt}%S"
  339. fmt="`echo "$fmt" | cut -c 3-`"
  340. ;;
  341. *)
  342. char=`echo "$fmt" | cut -c 1`
  343. dfmt="${dfmt}${char}"
  344. fmt="`echo "$fmt" | cut -c 2-`"
  345. ;;
  346. esac
  347. done
  348. shift
  349. done
  350. date +"$dfmt"
  351. }
  352. ssh(){
  353. __my_set_screen_title ssh
  354. command ssh "$@"
  355. }
  356. memo(){
  357. if test -z "$1"
  358. then
  359. $EDITOR memo.txt
  360. else
  361. $EDITOR "$1/memo.txt"
  362. fi
  363. }
  364. clk(){
  365. local tformat="%Y/%m/%d %H:%M:%S %z"
  366. cal
  367. REPLY=
  368. printf "\\r`date "+${tformat}"`"
  369. read -t 1
  370. while test $? -ne 0
  371. do
  372. printf "\\r`date "+${tformat}"`"
  373. read -t 1
  374. done
  375. }
  376. s(){
  377. if git rev-parse --git-dir >/dev/null 2>&1
  378. then
  379. echo ">> git grep -n $@" 1>&2
  380. git grep -n "$@"
  381. elif which ag >/dev/null 2>&1
  382. then
  383. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  384. ag --pager="$PAGER" "$@"
  385. elif which ack >/dev/null 2>&1
  386. then
  387. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  388. ack --pager="$PAGER" "$@"
  389. else
  390. echo \
  391. ">> find . " \
  392. "-path '*/.git' -prune -o" \
  393. "-path '*/.svn' -prune -o" \
  394. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  395. if test $# -eq 0
  396. then
  397. echo "No search word given." 1>&2
  398. return 1
  399. fi
  400. find . \
  401. -path '*/.git' -prune -o \
  402. -path '*/.svn' -prune -o \
  403. -type -f -exec grep -nH -e --color=always "$@" {} + \
  404. | $PAGER
  405. fi
  406. }
  407. man(){
  408. env \
  409. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  410. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  411. LESS_TERMCAP_me=$(printf "\e[0m") \
  412. LESS_TERMCAP_se=$(printf "\e[0m") \
  413. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  414. LESS_TERMCAP_ue=$(printf "\e[0m") \
  415. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  416. man "$@"
  417. }
  418. netwait(){
  419. while ! ping -c 1 -t 1 example.com
  420. do
  421. true
  422. done
  423. echo network works.
  424. }
  425. cd(){
  426. if test $# -eq 0
  427. then
  428. pushd ~/ >/dev/null
  429. elif test "$1" = -
  430. then
  431. local pwd="$PWD"
  432. command cd "$OLDPWD"
  433. pushd -n "$pwd" >/dev/null # stack last dir
  434. elif ! test -d "$1"
  435. then
  436. echo `basename ${SHELL}`: cd: "$1": No such file or directory 1>&2
  437. return 1
  438. else
  439. pushd "$1" >/dev/null
  440. fi
  441. __dirs_rm_dup "$PWD"
  442. echo "$PWD"
  443. }
  444. __dirs_rm_dup(){
  445. for d in "$@"
  446. do
  447. local next="$(__realpath --strip "$d")"
  448. for l in $(\dirs -v -l | cut -d $'\n' -f 2- | \
  449. \grep -x " *[0-9]\+ \+${next}" | \grep -o "^ *[0-9]\+ " | tac)
  450. do
  451. popd +$l -n >/dev/null
  452. done
  453. done
  454. }
  455. __realpath(){
  456. if type realpath >/dev/null 2>&1
  457. then
  458. command realpath "$@"
  459. else
  460. while ! test -d $1
  461. do
  462. shift
  463. done
  464. local d="$OLDPWD"
  465. command cd "$1"
  466. echo "$PWD"
  467. command cd "$d"
  468. fi
  469. }
  470. dh(){
  471. if test $# -eq 0
  472. then
  473. dirs -v -l
  474. else
  475. local dir="$(dirs -v -l | \grep "^ *$1 \+" | sed "s/^ *[0-9]* *//g")"
  476. cd "$dir"
  477. fi
  478. }
  479. input(){
  480. local foo
  481. stty -echo
  482. read foo
  483. stty echo
  484. echo $foo
  485. }
  486. # tmux(){
  487. # if test $# -eq 0
  488. # then
  489. # (cd ~; command tmux start;)
  490. # if command tmux has -t main
  491. # then
  492. # command tmux attach -t main
  493. # else
  494. # (cd ~; command tmux new -s main;)
  495. # fi
  496. # else
  497. # command tmux "$@"
  498. # fi
  499. # }
  500. tx(){
  501. if test $# -eq 0
  502. then
  503. tmux ls
  504. echo "tx <session> to attach."
  505. elif tmux has -t "$1"
  506. then
  507. tmux attach -t "$1"
  508. else
  509. tmux new -s "$1"
  510. fi
  511. }
  512. dt(){
  513. # dt [<name>] [<command ...>]
  514. __dtach_dir="${TMP}/dtach"
  515. install -d "${__dtach_dir}"
  516. if test -n "${__MY_DTACH}"
  517. then
  518. echo "Current session: ${__MY_DTACH}"
  519. fi
  520. if test -z "$1"
  521. then
  522. echo "Sessions:"
  523. ls "${__dtach_dir}"
  524. return 0
  525. elif test "$1" = "-h"
  526. then
  527. echo "dt: usage: dt <name> [<command ...>]" 1>&2
  528. return 1
  529. fi
  530. soc_name="${__dtach_dir}/$1"
  531. shift
  532. if test -n "$__MY_DTACH"
  533. then
  534. echo "dtach session cannot be nested." 1>&2
  535. return 1
  536. elif test -S "$soc_name"
  537. then
  538. dtach -a "$soc_name" -e ^^
  539. elif test -e "$soc_name"
  540. then
  541. echo "dt: File named $soc_name already exists."
  542. return 1
  543. elif test -z "$1"
  544. then
  545. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  546. # echo "dt: Socket named $soc_name not exists and no command specified."
  547. # return 1
  548. else
  549. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  550. fi
  551. }
  552. scr(){
  553. test -n "$1" && pf="${1}-"
  554. local _tformat="%Y%m%d-%H%M%S%z"
  555. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  556. __MY_SCRIPT=${_file} script ${_file} "$@"
  557. }
  558. dtscr(){
  559. # dtscr <command ...>
  560. if test -z "$1"
  561. then
  562. echo "dtscr: usage: dtscr <command ...>"
  563. return 1
  564. fi
  565. local _cmdstr="`echo $@ | tr ' ' +`"
  566. local _tformat="%Y%m%d-%H%M%S%z"
  567. local _name="${pf}`date +${_tformat}`-${_cmdstr}"
  568. local _scr_file="${HOME}/${_name}.script"
  569. local _dt_dir="${TMP}/dtscr"
  570. install -d "$_dt_dir"
  571. dtach -n "${_dt_dir}/${_name}" script "${_scr_file_}" "$@"
  572. # echo $_name
  573. # echo $_file
  574. }
  575. mcrypt-stream(){
  576. test $# -eq 2 || return 1
  577. case $1 in
  578. en)
  579. mcrypt --key $2 | base64 ;;
  580. de)
  581. base64 -d | mcrypt -d --key $2 ;;
  582. esac
  583. }
  584. gpg-stream(){
  585. test $# -eq 2 || return 1
  586. case $1 in
  587. en)
  588. gpg --passphrase $2 -c --batch |base64 ;;
  589. de)
  590. base64 -d|gpg --passphrase $2 -d --batch ;;
  591. esac
  592. }
  593. dgpg(){
  594. if test "$1" = help || test -z "$2"
  595. then
  596. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  597. return
  598. fi
  599. local srcs="$2"
  600. local dsts="$3"
  601. test -z "$dsts" && dsts="${srcs}.out"
  602. local pw
  603. echo -n "dgpg pw: "
  604. read -s pw
  605. echo ""
  606. test -z "$pw" && return 1
  607. for f in *${srcs}
  608. do
  609. local d="$(basename "$f" "${srcs}")${dsts}"
  610. echo -n "Processing $f to $d..."
  611. if test -d "$f"
  612. then
  613. echo "`printf 'failed (%s is directory)' $f`"
  614. elif test -f "$d"
  615. then
  616. echo "`printf 'failed (%s is already exists)' $d`"
  617. elif <"$f" gpg-stream $1 $pw >"$d" 2>/dev/null
  618. then
  619. echo "done"
  620. else
  621. echo "failed"
  622. test -f "$d" && rm "$d"
  623. fi
  624. done
  625. }
  626. alias enst="gpg-stream en"
  627. alias dest="gpg-stream de"
  628. showinfo(){
  629. echo "Japanese letters are 表示可能"
  630. __try_exec diskinfo
  631. ! isdarwin && test -n "${DISPLAY}" && {
  632. __try_exec xrandr | \grep --color=never ^Screen
  633. }
  634. iswindows || __try_exec finger $USER
  635. LANG=C __try_exec id
  636. __try_exec xset q
  637. }
  638. x(){
  639. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  640. #mkdir -p ~/.my/log
  641. # nohup startx >~/.my/log/xorg.log 2>&1 &
  642. # exit
  643. exec startx
  644. else
  645. echo "X cant be started! Another X is already running?" 1>&2
  646. fi
  647. }
  648. bak(){
  649. for file in "$@"
  650. do
  651. cp -v ${file} ${file}.bak
  652. done
  653. }
  654. di(){
  655. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  656. then
  657. local diffcmd=colordiff
  658. else
  659. local diffcmd=diff
  660. fi
  661. ${diffcmd} -u "$@" | ${PAGER}
  662. }
  663. tb(){
  664. local tb="$HOME/.my/tb"
  665. mkdir -p "$tb"
  666. for file in "$@"
  667. do
  668. mv -t "$tb" "$file"
  669. done
  670. }
  671. mkcd(){
  672. if test -z "$1"
  673. then
  674. echo "mkcd: usage: mkcd <dir>"
  675. return 1
  676. elif test -d "$1"
  677. then
  678. echo "Dir \"$1\" already exists."
  679. else
  680. mkdir -p "$1"
  681. echo "Dir \"$1\" created."
  682. fi
  683. cd "$1"
  684. }
  685. mkcdd(){
  686. # make and change date directory
  687. _d=`date +%Y%m%d-%H%M%S`
  688. mkcd "$_d"
  689. }
  690. if test -n "$TMUX" && null type reattach-to-user-namespace
  691. then
  692. alias pbpaste="reattach-to-user-namespace pbpaste"
  693. alias pbcopy="reattach-to-user-namespace pbcopy"
  694. fi
  695. catclip(){
  696. if iswindows
  697. then
  698. cat /dev/clipboard | tr -d \\r
  699. elif isdarwin
  700. then
  701. pbpaste
  702. else
  703. xclip -o -selection "clipboard"
  704. fi
  705. }
  706. setclip(){
  707. if test $# -eq 0
  708. then
  709. exec 3<&0
  710. else
  711. exec 3<<__EOF__
  712. `cat "$@"`
  713. __EOF__
  714. fi
  715. if iswindows
  716. then
  717. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  718. elif isdarwin
  719. then
  720. pbcopy 0<&3
  721. else
  722. 0<&3 xclip -i -f -selection "primary" | \
  723. xclip -i -f -selection "clipboard"
  724. fi
  725. exec 3<&-
  726. }
  727. open_file(){
  728. if iswindows
  729. then
  730. cmd.exe //c start "" "$@"
  731. elif isdarwin
  732. then
  733. touch "$@"
  734. open "$@"
  735. elif islinux
  736. then
  737. touch "$@"
  738. if null type pcmanfm; then
  739. LC_MESSAGES= pcmanfm "$@"
  740. else
  741. LC_MESSAGES= xdg-open "$@" &
  742. fi
  743. else
  744. cat "$@"
  745. fi
  746. }
  747. o(){
  748. if test $# -eq 0
  749. then
  750. open_file .
  751. else
  752. for f in "$@"
  753. do
  754. open_file "$(realpath "$f")"
  755. done
  756. fi
  757. }
  758. convmv-sjis2utf8-test(){
  759. convmv -r -f sjis -t utf8 *
  760. }
  761. convmv-sjis2utf8-notest(){
  762. convmv -r -f sjis -t utf8 * --notest
  763. }
  764. winln(){
  765. # for windose make link (actually junction)
  766. if test $# -eq 0
  767. then
  768. {
  769. echo "usage: winln TARGET LINK_NAME"
  770. echo "Create a link to TARGET with the name LINK_NAME \
  771. (that is, TARGET must already exist)."
  772. echo "About other features run 'junction'."
  773. } 1>&2
  774. return 1
  775. else
  776. junction "$2" "$1"
  777. fi
  778. }
  779. #################################################
  780. ## pastebin services
  781. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  782. sprunge(){
  783. # http://sprunge.us
  784. if test -z "$1"
  785. then
  786. curl -F 'sprunge=<-' http://sprunge.us
  787. else
  788. curl http://sprunge.us/$1
  789. fi
  790. }
  791. dpaste(){
  792. # http://dpaste.de
  793. if test -z "$1"
  794. then
  795. curl -F 'content=<-' https://dpaste.de/api/
  796. echo
  797. else
  798. curl https://dpaste.de/$1/raw/
  799. fi
  800. }
  801. ######################################
  802. ## Prompt
  803. __my_moc_state(){
  804. type mocp >/dev/null 2>&1 || return
  805. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  806. printf "$1" "`mocp -Q %title 2>/dev/null`"
  807. }
  808. __my_parse_svn_branch() {
  809. local LANG=C
  810. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  811. local svn_repository_root=$(svn info 2>/dev/null | \
  812. sed -ne 's#^Repository Root: ##p')
  813. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  814. awk '{print $1}'
  815. }
  816. __my_svn_ps1(){
  817. if svn status >/dev/null 2>&1
  818. then
  819. local svn_branch=$(__my_parse_svn_branch)
  820. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  821. fi
  822. }
  823. __my_battery_status(){
  824. local dir=/sys/class/power_supply/BAT0
  825. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  826. test -r $dir/charge_now
  827. then
  828. local st=$(cat $dir/status)
  829. local full=$(cat $dir/charge_full)
  830. local now=$(cat $dir/charge_now)
  831. local rate=$(expr $now \* 100 / $full)
  832. printf "$1" "${st}:${rate}%"
  833. fi
  834. }
  835. alias bat='__my_battery_status %s\\n'
  836. ip-address(){
  837. type ip >/dev/null 2>&1 || return 1
  838. local ip=$(LANG=C ip addr show scope global | \
  839. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  840. test -n "$ip" && printf $1 $ip
  841. }
  842. __my_ps1_str=""
  843. test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  844. test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  845. test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
  846. __my_ps1_scale(){
  847. local last=$?
  848. if null type stty && ! ismsys
  849. then
  850. stty size | tr -d $'\n' | tr " " x
  851. printf " "
  852. fi
  853. return $last
  854. }
  855. __my_ps1_tmux(){
  856. local last=$?
  857. null type tmux || return $last
  858. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  859. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  860. return $last
  861. }
  862. __my_ps1_moc(){
  863. local last=$?
  864. __my_moc_state "[MOC:%s]"
  865. return $last
  866. }
  867. for f in /usr/share/git/git-prompt.sh \
  868. /opt/local/share/git-core/git-prompt.sh \
  869. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  870. do
  871. test -r "$f" && . "$f" && break
  872. done
  873. GIT_PS1_SHOWDIRTYSTATE=t
  874. GIT_PS1_SHOWUPSTREAM=t
  875. __my_ps1_git(){
  876. local last=$?
  877. null type __git_ps1 || return $last
  878. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  879. __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  880. return $last
  881. }
  882. __my_ps1_ipaddr(){
  883. local last=$?
  884. ! iswindows && ip-address [Addr:%s]
  885. return $last
  886. }
  887. __my_ps1_bttry(){
  888. local last=$?
  889. local bst="${TMP}/batterystatus"
  890. if test -z "$DISPLAY" && ! iswindows
  891. then
  892. test -f $bst && local bstr="$(cat $bst)"
  893. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  894. echo "[Battery:$bstr]"
  895. __my_battery_status %s >$bst &
  896. fi
  897. return $last
  898. }
  899. __my_ps1_dirs(){
  900. dirs | wc -l
  901. }
  902. __my_ps1_jobs(){
  903. jobs | wc -l
  904. }
  905. if test "$TERM" != dumb
  906. then
  907. __my_c1="\[\e[0;33m\]" # color for PWD
  908. __my_c2="\[\e[0;36m\]" # color for user
  909. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  910. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  911. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
  912. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  913. else __my_c4="\[\e[1;32m\]" # color for ::
  914. fi
  915. __my_c5="\[\e[30;47m\]" # color for SCR
  916. __my_cdef="\[\e[0m\]"
  917. fi
  918. # export _LAST_STATUS=a
  919. # __my_export_last_status(){
  920. # local last=$?
  921. # echo $last
  922. # export _LAST_STATUS=$last
  923. # echo $_LAST_STATUS
  924. # return $last
  925. # }
  926. _ps1_bash="\
  927. ${__my_c4}:: ${__my_cdef}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_git)\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\$(__my_ps1_moc)\n\
  928. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$? $(__my_ps1_scale) \D{%T} ${__my_ps1_str}\$ "
  929. inbash && PS1=$_ps1_bash
  930. __my_set_screen_title(){
  931. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  932. then
  933. echo -ne "\033k$1\033\\"
  934. fi
  935. }
  936. __my_set_title(){
  937. case $TERM in
  938. (rxvt*|xterm*|aterm|screen*)
  939. title="$(echo $@)"
  940. test -t 1 &&
  941. test -n "$DISPLAY" &&
  942. test -z "$EMACS" &&
  943. echo -n -e "\033]0;${title}\007"
  944. ;;
  945. esac
  946. }
  947. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  948. __my_set_screen_title \$(basename \"\$PWD\")/"