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.
 
 
 
 
 
 

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