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.
 
 
 
 
 
 

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