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.
 
 
 
 
 
 

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