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.
 
 
 
 
 
 

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