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.
 
 
 
 
 
 

628 rivejä
16 KiB

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