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.
 
 
 
 
 
 

591 line
15 KiB

  1. #!/bin/bash
  2. # If not running interactively, don't do anything
  3. [ -z "$PS1" ] && return
  4. ##########################################
  5. null(){
  6. "$@" >/dev/null 2>&1
  7. }
  8. __try_exec(){
  9. type $1 >/dev/null 2>&1 && "$@"
  10. }
  11. ##########################
  12. # system type
  13. alias ismsys=false
  14. alias iscygwin=false
  15. alias iswindows=false
  16. alias isdarwin=false
  17. alias islinux=false
  18. alias with_coreutils=false # for mac
  19. case `uname` in
  20. (MINGW*) alias ismsys=true ;;
  21. (CYGWIN*) alias iscygwin=true ;;
  22. (Darwin*) alias isdarwin=true ;;
  23. (Linux*) alias islinux=true ;;
  24. esac
  25. null ls --version && alias with_coreutils=true
  26. ( ismsys || iscygwin ) && alias iswindows=true
  27. #################################
  28. export PS1 # PS1 is defined later
  29. # PROMPT_COMMAND=prompt_function
  30. if false iswindows
  31. then
  32. export PAGER='tr -d \\r | less'
  33. else
  34. export PAGER="less"
  35. fi
  36. export LESS="-iRMXF"
  37. if null type vim
  38. then
  39. export EDITOR=vim
  40. else
  41. export EDITOR=vi
  42. fi
  43. export LANG=ja_JP.UTF-8
  44. export LC_MESSAGES=C
  45. # export CDPATH=".:~"
  46. export VISUAL="$EDITOR"
  47. export GIT_PAGER="$PAGER"
  48. export GIT_EDITOR="$EDITOR"
  49. echo "$TERM" | grep '^screen' >/dev/null 2>&1 || export TERM_ORIG=$TERM
  50. test "$TERM" = screen && test "$TERM_ORIG" = xterm-256color && TERM=screen-256color
  51. test -z "$TMP" && export TMP=/tmp/${USER}-tmp
  52. mkdir -p "$TMP"
  53. ! iswindows && null type stty && {
  54. stty stop undef # unbind C-s to stop displaying output
  55. # stty erase '^h'
  56. }
  57. if iswindows; then
  58. # export PS1=" \[\e[32m\]\u@\H \[\e[33m\]\w\[\e[0m\] \d \t\n\s \# \j \$ "
  59. # export PS1=" [\[\e[33m\]\w\[\e[0m\]]\n\[\e[32m\]\u@\H\[\e[0m\] \d \t \s.\v\nhist:\# jobs:\j \$ "
  60. export USER=$USERNAME
  61. fi
  62. #######################
  63. iswindows && alias tty="echo cmd.exe"
  64. type fortune >/dev/null 2>&1 && {
  65. echo
  66. fortune
  67. echo
  68. fortune -o
  69. echo
  70. }
  71. uname -a
  72. echo TERM $TERM $(tput colors) colors connected to $(tty), running $BASH $BASH_VERSION
  73. echo
  74. ###################################
  75. # some aliases and functions
  76. ( ! with_coreutils && isdarwin ) || test "$TERM" == dumb || _coloroption=" --color=always"
  77. ( ! with_coreutils && isdarwin ) || iswindows || _timeoption=" --time-style=long-iso"
  78. alias ls="ls -hCF${_coloroption}${_timeoption}"
  79. # export GREP_OPTIONS=""
  80. alias grep="grep -n${_coloroption}"
  81. iswindows && alias grep="grep -n"
  82. # alias ll="ls -l"
  83. # alias la="ls -A"
  84. # alias lla="ls -Al"
  85. # alias less=""
  86. alias em="emacs -nw"
  87. null type vim && alias vi=vim
  88. alias pstree="LANG=C pstree"
  89. alias cp="cp -v"
  90. alias mv="mv -v"
  91. alias psaux="ps auxww"
  92. alias q=exit
  93. alias e3=e3em
  94. alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  95. alias po=popd
  96. alias pu=pushd
  97. alias sudo="sudo " # use aliases through sudo
  98. alias halt="sudo halt"
  99. alias reboot="sudo reboot"
  100. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  101. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  102. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  103. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  104. alias rand="echo \$RANDOM"
  105. alias xunp="file-roller -h"
  106. alias pc="sudo \paco -D"
  107. alias pycalc="python -i -c 'from math import *' "
  108. alias py3=python3
  109. alias py2=python2
  110. alias _reloadrc="test -f ~/.bashrc && source ~/.bashrc"
  111. # alias mytime="date +%Y%m%d-%H%M%S"
  112. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  113. # type trash >/dev/null 2>&1 && alias rm=trash
  114. alias mpg123="mpg123 -C -v --title"
  115. export PLAYER="mpg123 -C -v --title"
  116. alias screen="screen -e^z^z"
  117. alias zcd="cd \`zenity --file-selection --directory\`"
  118. alias pad=notepad
  119. null type gedit && alias pad=gedit
  120. null type leafpad && alias pad=leafpad
  121. alias wic=wicd-curses
  122. alias wil="wicd-cli -y -l | head"
  123. #alias wicn="wicd-cli -y -c -n"
  124. wicn(){
  125. if test $# -eq 0
  126. then
  127. local num
  128. wicd-cli -y -l | head
  129. echo -n "input num: "
  130. read num
  131. test -n "$num" && wicd-cli -y -c -n $num
  132. else
  133. wicd-cli -y -c -n $1
  134. fi
  135. }
  136. alias aptin="apt-get install"
  137. alias aptsearch="apt-cache search"
  138. alias aptshow="apt-cache show"
  139. for f in /usr/share/vim/vimcurrent/macros/less.sh \
  140. /usr/share/vim/vim73/macros/less.sh
  141. do
  142. test -f $f && alias vl=$f && break
  143. done
  144. alias yt=yaourt
  145. null type pacman-color && {
  146. alias pacman=pacman-color
  147. export pacman_program=pacman-color # used by pacmatic
  148. export PACMAN=pacman-color # used by yaourt
  149. }
  150. null type pacmatic && {
  151. alias pacman="pacmatic"
  152. export PACMAN="pacmatic"
  153. }
  154. alias ubuntu-upgrade="sudo apt-get autoremove --yes && sudo apt-get update --yes && sudo apt-get upgrade --yes"
  155. alias arch-upgrade="sudo pacman -Syu"
  156. alias port-upgrade="sudo port selfupdate && sudo port upgrade outdated && sudo port uninstall leaves"
  157. if iscygwin; then
  158. null type windate || alias windate="/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%'"
  159. alias cygsu="cygstart /cygwinsetup.exe"
  160. alias emacs="CYGWIN=tty emacs -nw"
  161. alias ls="ls -CFG $(iswindows || test "$TERM" == dumb || echo --color=auto)"
  162. fi
  163. alias g=git
  164. if null type _git # enable programmable completion for g
  165. then
  166. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  167. || complete -o default -o nospace -F _git g
  168. fi
  169. input(){
  170. local foo
  171. stty -echo
  172. read foo
  173. stty echo
  174. echo $foo
  175. }
  176. tmux(){
  177. if test $# -eq 0
  178. then
  179. if command tmux has -t main
  180. then
  181. command tmux attach -t main
  182. else
  183. command tmux new -s main
  184. fi
  185. else
  186. command tmux "$@"
  187. fi
  188. }
  189. __my_moc_state(){
  190. type mocp >/dev/null 2>&1 || return
  191. test "`mocp -Q %state 2>/dev/null`" == PLAY || return
  192. printf "$1" "`mocp -Q %title 2>/dev/null`"
  193. }
  194. mcrypt-stream(){
  195. test $# -eq 2 || return 1
  196. case $1 in
  197. en)
  198. mcrypt --key $2 | base64 ;;
  199. de)
  200. base64 -d | mcrypt -d --key $2 ;;
  201. esac
  202. }
  203. gpg-stream(){
  204. test $# -eq 2 || return 1
  205. case $1 in
  206. en)
  207. gpg --passphrase $2 -c --batch |base64 ;;
  208. de)
  209. base64 -d|gpg --passphrase $2 -d --batch ;;
  210. esac
  211. }
  212. alias enst="gpg-stream en"
  213. alias dest="gpg-stream de"
  214. showinfo(){
  215. echo "Japanese letters are 表示可能"
  216. __try_exec diskinfo
  217. ! isdarwin && test -n "${DISPLAY}" && {
  218. __try_exec xrandr | \grep --color=never ^Screen
  219. }
  220. iswindows || __try_exec finger $USER
  221. LANG=C __try_exec id
  222. __try_exec xset q
  223. }
  224. x(){
  225. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  226. #mkdir -p ~/.my/log
  227. # nohup startx >~/.my/log/xorg.log 2>&1 &
  228. # exit
  229. exec startx
  230. else
  231. echo "X cant be started! Maybe another X is already running or something." 1>&2
  232. fi
  233. }
  234. bak(){
  235. for file in "$@"
  236. do
  237. cp -v ${file} ${file}.bak
  238. done
  239. }
  240. di(){
  241. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  242. then
  243. local diffcmd=colordiff
  244. else
  245. local diffcmd=diff
  246. fi
  247. ${diffcmd} -u "$@" | ${PAGER}
  248. }
  249. tb(){
  250. local tb=~/.my/tb
  251. mkdir -p $tb
  252. for file in "$@"
  253. do
  254. mv $file $tb
  255. done
  256. }
  257. mkcd(){
  258. mkdir -p $1
  259. cd $1
  260. }
  261. catclip(){
  262. if iswindows
  263. then
  264. cat /dev/clipboard | tr -d \\r
  265. elif isdarwin
  266. then
  267. pbpaste
  268. else
  269. xclip -o -selection "clipboard"
  270. fi
  271. }
  272. setclip(){
  273. if iswindows
  274. then
  275. if test $# -eq 0
  276. then
  277. sed -e 's/$/\r/' | tee /dev/clipboard
  278. else
  279. cat "$@" | sed -e 's/$/\r/' | tee /dev/clipboard
  280. fi
  281. elif isdarwin
  282. then
  283. if test $# -eq 0
  284. then
  285. pbcopy
  286. else
  287. cat "$@" | pbcopy
  288. fi
  289. else
  290. if test $# -eq 0
  291. then
  292. xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  293. else
  294. cat "$@" | xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  295. fi
  296. fi
  297. }
  298. open_file(){
  299. if iswindows
  300. then
  301. cmd.exe //c start "" "$@"
  302. elif isdarwin
  303. then
  304. open "$@"
  305. elif islinux
  306. then
  307. if null type pcmanfm; then
  308. LC_MESSAGES= pcmanfm "$@"
  309. else
  310. LC_MESSAGES= xdg-open "$@" &
  311. fi
  312. else
  313. cat "$@"
  314. fi
  315. }
  316. o(){
  317. if test $# -eq 0
  318. then
  319. open_file .
  320. else
  321. for f in "$@"
  322. do
  323. open_file "$f"
  324. done
  325. fi
  326. }
  327. convmv-sjis2utf8-test(){
  328. convmv -r -f sjis -t utf8 *
  329. }
  330. convmv-sjis2utf8-notest(){
  331. convmv -r -f sjis -t utf8 * --notest
  332. }
  333. __my_parse_svn_branch() {
  334. local LANG=C
  335. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  336. local svn_repository_root=$(svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p')
  337. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | awk '{print $1}'
  338. }
  339. __my_svn_ps1(){
  340. if svn status >/dev/null 2>&1
  341. then
  342. local svn_branch=$(__my_parse_svn_branch)
  343. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  344. fi
  345. }
  346. #Change ANSI Colors
  347. _chengecolors(){
  348. echo -e \
  349. "\e]P0000000" \
  350. "\e]P1cd0000" \
  351. "\e]P200cd00" \
  352. "\e]P3cdcd00" \
  353. "\e]P41e90ff" \
  354. "\e]P5cd00cd" \
  355. "\e]P600cdcd" \
  356. "\e]P7353535" \
  357. "\e]P8666666" \
  358. "\e]P9ff9999" \
  359. "\e]Pa99ff99" \
  360. "\e]Pbffff99" \
  361. "\e]Pc9999ff" \
  362. "\e]Pdff99ff" \
  363. "\e]Pe99ffff" \
  364. "\e]Pfffffff"
  365. }
  366. # printf "\e]P7353535" \
  367. _colors(){
  368. echo -e \
  369. "\e[30mBlack" \
  370. "\e[31mRed" \
  371. "\e[32mGreen" \
  372. "\e[33mYellow" \
  373. "\e[34mBlue" \
  374. "\e[35mMagenta" \
  375. "\e[36mCyan" \
  376. "\e[37mWhite"
  377. echo -e \
  378. "\e[30;1mBright Black" \
  379. "\e[31;1mBright Red" \
  380. "\e[32;1mBright Green" \
  381. "\e[33;1mBright Yellow" \
  382. "\e[34;1mBright Blue" \
  383. "\e[35;1mBright Magenta" \
  384. "\e[36;1mBright Cyan" \
  385. "\e[37;1mBright White\n" \
  386. "\e[0m"
  387. }
  388. winln(){
  389. # for windose make link (actually junction)
  390. if test $# -eq 0
  391. then
  392. {
  393. echo "usage: winln TARGET LINK_NAME"
  394. echo "Create a link to TARGET with the name LINK_NAME (that is, TARGET must already exist)."
  395. echo "About other features run 'junction'."
  396. } 1>&2
  397. return 1
  398. else
  399. junction "$2" "$1"
  400. fi
  401. }
  402. __my_battery_status(){
  403. local dir=/sys/class/power_supply/BAT0
  404. if test -d $dir
  405. then
  406. local st=$(cat $dir/status)
  407. local full=$(cat $dir/charge_full)
  408. local now=$(cat $dir/charge_now)
  409. local rate=$(expr $now \* 100 / $full)
  410. printf "$1" "${st}:${rate}%"
  411. fi
  412. }
  413. alias bat='__my_battery_status %s\\n'
  414. ip-address(){
  415. test type ifconfig >/dev/null 2>&1 || return 1
  416. local ip=$(LANG=C ifconfig | \grep --color=never "inet " | \grep --color=never -v "127.0.0.1" | awk '{print $2}')
  417. test -n "$ip" && printf $1 $ip
  418. }
  419. __my_ps1_scale(){
  420. local last=$?
  421. echo "[LC:${LINES}x${COLUMNS}]"
  422. return $last
  423. }
  424. __my_ps1_tmux(){
  425. local last=$?
  426. local tmuxc="$(__try_exec tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  427. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  428. return $last
  429. }
  430. __my_ps1_moc(){
  431. local last=$?
  432. __my_moc_state "[MOC:%s]"
  433. return $last
  434. }
  435. __my_ps1_git(){
  436. local last=$?
  437. null type __git_ps1 || return $last
  438. __try_exec __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  439. return $last
  440. }
  441. __my_ps1_ipaddr(){
  442. local last=$?
  443. test -z "$DISPLAY" && ! iswindows && ip-address [Addr:%s]
  444. return $last
  445. }
  446. __my_ps1_bttry(){
  447. local last=$?
  448. local bst="${TMP}/batterystatus"
  449. if test -z "$DISPLAY" && ! iswindows
  450. then
  451. test -f $bst && local bstr="$(cat $bst)"
  452. test -n "$bstr" && echo "[Battery:$bstr]"
  453. __my_battery_status %s >$bst &
  454. fi
  455. return $last
  456. }
  457. __my_ps1_dirs(){
  458. dirs | wc -l
  459. }
  460. __my_ps1_jobs(){
  461. jobs | wc -l
  462. }
  463. if test "$TERM" != dumb
  464. then
  465. __my_c1="\[\e[1;31m\]" # color for PWD
  466. __my_c2="\[\e[0;36m\]" # color for user
  467. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  468. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  469. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;33m\]"
  470. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  471. else __my_c4="\[\e[1;32m\]" # color for ::
  472. fi
  473. __my_cdef="\[\e[0m\]"
  474. fi
  475. _PS1="\
  476. ${__my_c4}:: ${__my_cdef}[${__my_c1}\w/${__my_cdef}<${__my_c3}\${OLDPWD}${__my_cdef}]\$(__my_ps1_scale)\$(__my_ps1_tmux)\$(__my_ps1_git)\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\$(__my_ps1_moc)\n\
  477. ${__my_c4}:: ${__my_c2}\u@\H${__my_cdef} \D{%a, %d %b %Y %T %z} ${SHELL} \V\n\
  478. ${__my_c4}:: ${__my_cdef}shlv:${SHLVL} cnum:\# jobs:\j last:\$? \$ "
  479. PS1=$_PS1
  480. __my_set_title(){
  481. title="$(echo $@)"
  482. case $TERM in
  483. (rxvt*|xterm*|aterm|screen*)
  484. test -t 1 &&
  485. test -n "$DISPLAY" &&
  486. test -z "$EMACS" &&
  487. echo -n -e "\033]0;${title}\007"
  488. ;;
  489. esac
  490. }
  491. export PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\ \${PWD};"
  492. # copied from https://wiki.archlinux.org/index.php/X_resources
  493. invader(){
  494. # ANSI color scheme script featuring Space Invaders
  495. #
  496. # Original: http://crunchbanglinux.org/forums/post/126921/#p126921
  497. # Modified by lolilolicon
  498. #
  499. f=3 b=4
  500. for j in f b; do
  501. for i in {0..7}; do
  502. printf -v $j$i %b "\e[${!j}${i}m"
  503. done
  504. done
  505. bld=$'\e[1m'
  506. rst=$'\e[0m'
  507. cat << EOF
  508. $f1 ▀▄ ▄▀ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4 ▀▄ ▄▀ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  509. $f1 ▄█▀███▀█▄ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4 ▄█▀███▀█▄ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  510. $f1█▀███████▀█ $f2▀▀███▀▀███▀▀ $f3▀█▀██▀█▀ $f4█▀███████▀█ $f5▀▀███▀▀███▀▀ $f6▀█▀██▀█▀$rst
  511. $f1▀ ▀▄▄ ▄▄▀ ▀ $f2 ▀█▄ ▀▀ ▄█▀ $f3▀▄ ▄▀ $f4▀ ▀▄▄ ▄▄▀ ▀ $f5 ▀█▄ ▀▀ ▄█▀ $f6▀▄ ▄▀$rst
  512. $bld$f1▄ ▀▄ ▄▀ ▄ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4▄ ▀▄ ▄▀ ▄ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  513. $bld$f1█▄█▀███▀█▄█ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4█▄█▀███▀█▄█ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  514. $bld$f1▀█████████▀ $f2▀▀▀██▀▀██▀▀▀ $f3▀▀█▀▀█▀▀ $f4▀█████████▀ $f5▀▀▀██▀▀██▀▀▀ $f6▀▀█▀▀█▀▀$rst
  515. $bld$f1 ▄▀ ▀▄ $f2▄▄▀▀ ▀▀ ▀▀▄▄ $f3▄▀▄▀▀▄▀▄ $f4 ▄▀ ▀▄ $f5▄▄▀▀ ▀▀ ▀▀▄▄ $f6▄▀▄▀▀▄▀▄$rst
  516. $f7▌$rst
  517. $f7▌$rst
  518. $f7 ▄█▄ $rst
  519. $f7▄█████████▄$rst
  520. $f7▀▀▀▀▀▀▀▀▀▀▀$rst
  521. EOF
  522. }
  523. #/etc/lsb-release