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.
 
 
 
 
 
 

531 lines
14 KiB

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