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.
 
 
 
 
 
 

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