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.
 
 
 
 
 
 

546 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=\$\ PROMPT_COMMAND="" 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 zcd="cd \`zenity --file-selection --directory\`"
  110. alias pad=notepad
  111. null type gedit && alias pad=gedit
  112. null type leafpad && alias pad=leafpad
  113. alias wic=wicd-curses
  114. alias wil="wicd-cli -y -l | head"
  115. #alias wicn="wicd-cli -y -c -n"
  116. wicn(){
  117. if test $# -eq 0
  118. then
  119. local num
  120. wicd-cli -y -l | head
  121. echo -n "input num: "
  122. read num
  123. test -n "$num" && wicd-cli -y -c -n $num
  124. else
  125. wicd-cli -y -c -n $1
  126. fi
  127. }
  128. alias aptin="apt-get install"
  129. alias aptsearch="apt-cache search"
  130. alias aptshow="apt-cache show"
  131. for f in /usr/share/vim/vimcurrent/macros/less.sh \
  132. /usr/share/vim/vim73/macros/less.sh
  133. do
  134. test -f $f && alias vl=$f && break
  135. done
  136. alias yt=yaourt
  137. null type pacman-color && {
  138. alias pacman=pacman-color
  139. export pacman_program=pacman-color # used by pacmatic
  140. export PACMAN=pacman-color # used by yaourt
  141. }
  142. null type pacmatic && {
  143. alias pacman="pacmatic"
  144. export PACMAN="pacmatic"
  145. }
  146. alias ubuntu-upgrade="sudo apt-get autoremove --yes && sudo apt-get update --yes && sudo apt-get upgrade --yes"
  147. alias arch-upgrade="sudo pacman -Syu"
  148. alias port-upgrade="port selfupdate && port sync && port upgrade installed"
  149. if iscygwin; then
  150. null type windate || alias windate="/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%'"
  151. alias cygsu="cygstart /cygwinsetup.exe"
  152. alias emacs="CYGWIN=tty emacs -nw"
  153. alias ls="ls -CFG $(iswindows || test "$TERM" == dumb || echo --color=auto)"
  154. fi
  155. alias g=git
  156. if null type _git # enable programmable completion for g
  157. then
  158. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  159. || complete -o default -o nospace -F _git g
  160. fi
  161. tmux(){
  162. if test $# -eq 0
  163. then
  164. if command tmux has -t main
  165. then
  166. command tmux attach -t main
  167. else
  168. command tmux new -s main
  169. fi
  170. else
  171. command tmux "$@"
  172. fi
  173. }
  174. __my_moc_state(){
  175. type mocp >/dev/null 2>&1 || return
  176. test "`mocp -Q %state 2>/dev/null`" == PLAY || return
  177. printf "$1" "`mocp -Q %title 2>/dev/null`"
  178. }
  179. mcrypt-stream(){
  180. test $# -eq 2 || return 1
  181. case $1 in
  182. en)
  183. mcrypt --key $2 | base64 ;;
  184. de)
  185. base64 -d | mcrypt -d --key $2 ;;
  186. esac
  187. }
  188. gpg-stream(){
  189. test $# -eq 2 || return 1
  190. case $1 in
  191. en)
  192. gpg --passphrase $2 -c --batch |base64 ;;
  193. de)
  194. base64 -d|gpg --passphrase $2 -d --batch ;;
  195. esac
  196. }
  197. alias enst="gpg-stream en"
  198. alias dest="gpg-stream de"
  199. showinfo(){
  200. echo "Japanese letters are 表示可能"
  201. __try_exec diskinfo
  202. ! isdarwin && test -n "${DISPLAY}" && {
  203. __try_exec xrandr | \grep --color=never ^Screen
  204. }
  205. iswindows || __try_exec finger $USER
  206. LANG=C __try_exec id
  207. __try_exec xset q
  208. }
  209. x(){
  210. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  211. #mkdir -p ~/.my/log
  212. # nohup startx >~/.my/log/xorg.log 2>&1 &
  213. # exit
  214. exec startx
  215. else
  216. echo "X cant be started! Maybe another X is already running or something." 1>&2
  217. fi
  218. }
  219. bak(){
  220. for file in "$@"
  221. do
  222. cp -v ${file} ${file}.bak
  223. done
  224. }
  225. di(){
  226. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  227. then
  228. local diffcmd=colordiff
  229. else
  230. local diffcmd=diff
  231. fi
  232. ${diffcmd} -u "$@" | ${PAGER}
  233. }
  234. tb(){
  235. local tb=~/.my/tb
  236. mkdir -p $tb
  237. for file in "$@"
  238. do
  239. mv $file $tb
  240. done
  241. }
  242. mkcd(){
  243. mkdir -p $1
  244. cd $1
  245. }
  246. catclip(){
  247. if iswindows
  248. then
  249. cat /dev/clipboard | tr -d \\r
  250. else
  251. xclip -o -selection "clipboard"
  252. fi
  253. }
  254. setclip(){
  255. if iswindows
  256. then
  257. if test $# -eq 0
  258. then
  259. sed -e 's/$/\r/' | tee /dev/clipboard
  260. else
  261. cat "$@" | sed -e 's/$/\r/' | tee /dev/clipboard
  262. fi
  263. else
  264. if test $# -eq 0
  265. then
  266. xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  267. else
  268. cat "$@" | xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  269. fi
  270. fi
  271. }
  272. if iswindows; then
  273. alias _open_file='cmd.exe //c start ""'
  274. elif isdarwin; then
  275. alias _open_file=open
  276. elif islinux; then
  277. if null type pcmanfm; then
  278. alias _open_file="LC_MESSAGES= pcmanfm"
  279. else
  280. alias _open_file="LC_MESSAGES= xdg-open"
  281. fi
  282. else
  283. alias _open_file=cat
  284. fi
  285. o(){
  286. if test $# -eq 0
  287. then
  288. _open_file . >/dev/null 2>&1 &
  289. else
  290. for f in "$@"
  291. do
  292. if test -d $f
  293. then
  294. _open_file $f >/dev/null 2>&1 &
  295. else
  296. _open_file $f >/dev/null 2>&1 &
  297. fi
  298. done
  299. fi
  300. }
  301. convmv-sjis2utf8-test(){
  302. convmv -r -f sjis -t utf8 *
  303. }
  304. convmv-sjis2utf8-notest(){
  305. convmv -r -f sjis -t utf8 * --notest
  306. }
  307. __my_parse_svn_branch() {
  308. local LANG=C
  309. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  310. local svn_repository_root=$(svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p')
  311. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | awk '{print $1}'
  312. }
  313. __my_svn_ps1(){
  314. if svn status >/dev/null 2>&1
  315. then
  316. local svn_branch=$(__my_parse_svn_branch)
  317. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  318. fi
  319. }
  320. #Change ANSI Colors
  321. _chengecolors(){
  322. echo -e \
  323. "\e]P0000000" \
  324. "\e]P1cd0000" \
  325. "\e]P200cd00" \
  326. "\e]P3cdcd00" \
  327. "\e]P41e90ff" \
  328. "\e]P5cd00cd" \
  329. "\e]P600cdcd" \
  330. "\e]P7353535" \
  331. "\e]P8666666" \
  332. "\e]P9ff9999" \
  333. "\e]Pa99ff99" \
  334. "\e]Pbffff99" \
  335. "\e]Pc9999ff" \
  336. "\e]Pdff99ff" \
  337. "\e]Pe99ffff" \
  338. "\e]Pfffffff"
  339. }
  340. # printf "\e]P7353535" \
  341. _colors(){
  342. echo -e \
  343. "\e[30mBlack" \
  344. "\e[31mRed" \
  345. "\e[32mGreen" \
  346. "\e[33mYellow" \
  347. "\e[34mBlue" \
  348. "\e[35mMagenta" \
  349. "\e[36mCyan" \
  350. "\e[37mWhite"
  351. echo -e \
  352. "\e[30;1mBright Black" \
  353. "\e[31;1mBright Red" \
  354. "\e[32;1mBright Green" \
  355. "\e[33;1mBright Yellow" \
  356. "\e[34;1mBright Blue" \
  357. "\e[35;1mBright Magenta" \
  358. "\e[36;1mBright Cyan" \
  359. "\e[37;1mBright White\n" \
  360. "\e[0m"
  361. }
  362. winln(){
  363. # for windose make link (actually junction)
  364. if test $# -eq 0
  365. then
  366. {
  367. echo "usage: winln TARGET LINK_NAME"
  368. echo "Create a link to TARGET with the name LINK_NAME (that is, TARGET must already exist)."
  369. echo "About other features run 'junction'."
  370. } 1>&2
  371. return 1
  372. else
  373. junction "$2" "$1"
  374. fi
  375. }
  376. __my_battery_status(){
  377. local dir=/sys/class/power_supply/BAT0
  378. if test -d $dir
  379. then
  380. local st=$(cat $dir/status)
  381. local full=$(cat $dir/charge_full)
  382. local now=$(cat $dir/charge_now)
  383. local rate=$(expr $now \* 100 / $full)
  384. printf "$1" "${st}:${rate}%"
  385. fi
  386. }
  387. alias bat='__my_battery_status %s\\n'
  388. ip-address(){
  389. test type ifconfig >/dev/null 2>&1 || return 1
  390. local ip=$(LANG=C ifconfig | \grep --color=never "inet " | \grep --color=never -v "127.0.0.1" | awk '{print $2}')
  391. test -n "$ip" && printf $1 $ip
  392. }
  393. __my_ps1_moc(){
  394. local last=$?
  395. __my_moc_state "[MOC:%s]"
  396. return $last
  397. }
  398. __my_ps1_git(){
  399. local last=$?
  400. __try_exec __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  401. return $last
  402. }
  403. __my_ps1_ipaddr(){
  404. local last=$?
  405. test -z "$DISPLAY" && ! iswindows && ip-address [Addr:%s]
  406. return $last
  407. }
  408. __my_ps1_bttry(){
  409. local last=$?
  410. local bst="${TMP}/batterystatus"
  411. if test -z "$DISPLAY" && ! iswindows
  412. then
  413. test -f $bst && local bstr="$(cat $bst)"
  414. test -n "$bstr" && echo "[Battery:$bstr]"
  415. __my_battery_status %s >$bst &
  416. fi
  417. return $last
  418. }
  419. __my_ps1_dirs(){
  420. dirs | wc -l
  421. }
  422. __my_ps1_jobs(){
  423. jobs | wc -l
  424. }
  425. if test "$TERM" != dumb
  426. then
  427. __my_c1="\[\e[1;31m\]" # color for PWD
  428. __my_c2="\[\e[0;36m\]" # color for user
  429. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  430. __my_c4="\[\e[1;32m\]" # color for ::
  431. __my_cdef="\[\e[0m\]"
  432. fi
  433. _PS1="\
  434. ${__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\
  435. ${__my_c4}:: ${__my_c2}\u@\H${__my_cdef} \D{%a, %d %b %Y %T %z} ${SHELL} \V\n\
  436. ${__my_c4}:: ${__my_cdef}shlv:${SHLVL} cnum:\# jobs:\j last:\$? \$ "
  437. PS1=$_PS1
  438. __my_set_title(){
  439. title="$(echo $@)"
  440. case $TERM in
  441. (rxvt*|xterm*|aterm|screen*)
  442. test -t 1 &&
  443. test -n "$DISPLAY" &&
  444. test -z "$EMACS" &&
  445. echo -n -e "\033]0;${title}\007"
  446. ;;
  447. esac
  448. }
  449. export PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\ \${PWD};"
  450. # copied from https://wiki.archlinux.org/index.php/X_resources
  451. invader(){
  452. # ANSI color scheme script featuring Space Invaders
  453. #
  454. # Original: http://crunchbanglinux.org/forums/post/126921/#p126921
  455. # Modified by lolilolicon
  456. #
  457. f=3 b=4
  458. for j in f b; do
  459. for i in {0..7}; do
  460. printf -v $j$i %b "\e[${!j}${i}m"
  461. done
  462. done
  463. bld=$'\e[1m'
  464. rst=$'\e[0m'
  465. cat << EOF
  466. $f1 ▀▄ ▄▀ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4 ▀▄ ▄▀ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  467. $f1 ▄█▀███▀█▄ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4 ▄█▀███▀█▄ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  468. $f1█▀███████▀█ $f2▀▀███▀▀███▀▀ $f3▀█▀██▀█▀ $f4█▀███████▀█ $f5▀▀███▀▀███▀▀ $f6▀█▀██▀█▀$rst
  469. $f1▀ ▀▄▄ ▄▄▀ ▀ $f2 ▀█▄ ▀▀ ▄█▀ $f3▀▄ ▄▀ $f4▀ ▀▄▄ ▄▄▀ ▀ $f5 ▀█▄ ▀▀ ▄█▀ $f6▀▄ ▄▀$rst
  470. $bld$f1▄ ▀▄ ▄▀ ▄ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4▄ ▀▄ ▄▀ ▄ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  471. $bld$f1█▄█▀███▀█▄█ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4█▄█▀███▀█▄█ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  472. $bld$f1▀█████████▀ $f2▀▀▀██▀▀██▀▀▀ $f3▀▀█▀▀█▀▀ $f4▀█████████▀ $f5▀▀▀██▀▀██▀▀▀ $f6▀▀█▀▀█▀▀$rst
  473. $bld$f1 ▄▀ ▀▄ $f2▄▄▀▀ ▀▀ ▀▀▄▄ $f3▄▀▄▀▀▄▀▄ $f4 ▄▀ ▀▄ $f5▄▄▀▀ ▀▀ ▀▀▄▄ $f6▄▀▄▀▀▄▀▄$rst
  474. $f7▌$rst
  475. $f7▌$rst
  476. $f7 ▄█▄ $rst
  477. $f7▄█████████▄$rst
  478. $f7▀▀▀▀▀▀▀▀▀▀▀$rst
  479. EOF
  480. }
  481. #/etc/lsb-release