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.
 
 
 
 
 
 

557 lines
14 KiB

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