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.
 
 
 
 
 
 

515 line
14 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. iswindows || test "$TERM" == dumb || _coloroption=" --color=always"
  61. alias ls="ls -hCF --time-style=long-iso${_coloroption}"
  62. iswindows && alias ls="ls -hCF"
  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. encrypt-stream(){
  133. test $# -eq 1 &&
  134. mcrypt --key $1 2>/dev/null | base64
  135. }
  136. decrypt-stream(){
  137. test $# -eq 1 &&
  138. base64 -d | mcrypt -d --key $1 2>/dev/null
  139. }
  140. showinfo(){
  141. echo "Japanese letters are 表示可能"
  142. __try_exec diskinfo
  143. ! isdarwin && test -n "${DISPLAY}" && {
  144. __try_exec xrandr | \grep --color=never ^Screen
  145. }
  146. iswindows || __try_exec finger $USER
  147. LANG=C __try_exec id
  148. __try_exec xset q
  149. }
  150. x(){
  151. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  152. #mkdir -p ~/.backup/log
  153. # nohup startx >~/.backup/log/xorg.log 2>&1 &
  154. # exit
  155. exec startx
  156. else
  157. echo "X cant be started! Maybe another X is already running or something." 1>&2
  158. fi
  159. }
  160. bak(){
  161. for file in "$@"
  162. do
  163. cp -v ${file} ${file}.bak
  164. done
  165. }
  166. di(){
  167. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  168. then
  169. local diffcmd=colordiff
  170. else
  171. local diffcmd=diff
  172. fi
  173. ${diffcmd} -u "$@" | ${PAGER}
  174. }
  175. throw-away(){
  176. mkdir -p ~/.backup/tb
  177. for file in "$@"
  178. do
  179. mv $file ~/.backup/tb
  180. done
  181. }
  182. mkcd(){
  183. mkdir -p $1
  184. cd $1
  185. }
  186. catclip(){
  187. if iswindows
  188. then
  189. cat /dev/clipboard | tr -d \\r
  190. else
  191. xclip -o -selection "clipboard"
  192. fi
  193. }
  194. setclip(){
  195. if iswindows
  196. then
  197. if test $# -eq 0
  198. then
  199. sed -e 's/$/\r/' | tee /dev/clipboard
  200. else
  201. cat "$@" | sed -e 's/$/\r/' | tee /dev/clipboard
  202. fi
  203. else
  204. if test $# -eq 0
  205. then
  206. xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  207. else
  208. cat "$@" | xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  209. fi
  210. fi
  211. }
  212. if iswindows; then
  213. alias _open_file='cmd.exe //c start ""'
  214. elif isdarwin; then
  215. alias _open_file=open
  216. elif islinux; then
  217. if null type pcmanfm; then
  218. alias _open_file="LC_MESSAGES= pcmanfm"
  219. else
  220. alias _open_file="LC_MESSAGES= xdg-open"
  221. fi
  222. else
  223. alias _open_file=cat
  224. fi
  225. o(){
  226. if test $# -eq 0
  227. then
  228. _open_file . >/dev/null 2>&1 &
  229. else
  230. for f in "$@"
  231. do
  232. if test -d $f
  233. then
  234. _open_file $f >/dev/null 2>&1 &
  235. else
  236. _open_file $f >/dev/null 2>&1 &
  237. fi
  238. done
  239. fi
  240. }
  241. convmv-sjis2utf8-test(){
  242. convmv -r -f sjis -t utf8 *
  243. }
  244. convmv-sjis2utf8-notest(){
  245. convmv -r -f sjis -t utf8 * --notest
  246. }
  247. _my_git_config(){
  248. git config --global user.name '10sr'
  249. git config --global user.email '8slashes+git@gmail.com'
  250. git config --global core.autocrlf false
  251. git config --global core.excludesfile '~/.gitignore'
  252. git config --global color.ui auto
  253. git config --global status.relativePaths false
  254. git config --global status.showUntrackedFiles normal
  255. git config --global alias.graph "log --graph --date-order -C -M --pretty=tformat:\"<%h> %ad [%an] %Cgreen%d%Creset %s\" --all --date=iso"
  256. git config --global alias.st "status -s -b"
  257. git config --global alias.b "branch"
  258. git config --global alias.ci "commit --verbose"
  259. git config --global alias.co "checkout"
  260. git config --global alias.cim "commit --verbose -m"
  261. git config --global alias.di "diff --color"
  262. git config --global alias.me "merge --no-ff --stat -v"
  263. git config --global alias.ls "ls-files -v --full-name"
  264. git config --global alias.sl "!sl"
  265. # git config --global alias.my-ls "ls-files | xargs ls"
  266. # git config --global alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  267. git config --global alias.addi "add -i"
  268. if iswindows; then
  269. git config --global core.fileMode false
  270. fi
  271. }
  272. __my_parse_svn_branch() {
  273. local LANG=C
  274. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  275. local svn_repository_root=$(svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p')
  276. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | awk '{print $1}'
  277. }
  278. __my_svn_ps1(){
  279. if svn status >/dev/null 2>&1
  280. then
  281. local svn_branch=$(__my_parse_svn_branch)
  282. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  283. fi
  284. }
  285. #Change ANSI Colors
  286. _chengecolors(){
  287. echo -e \
  288. "\e]P0000000" \
  289. "\e]P1cd0000" \
  290. "\e]P200cd00" \
  291. "\e]P3cdcd00" \
  292. "\e]P41e90ff" \
  293. "\e]P5cd00cd" \
  294. "\e]P600cdcd" \
  295. "\e]P7353535" \
  296. "\e]P8666666" \
  297. "\e]P9ff9999" \
  298. "\e]Pa99ff99" \
  299. "\e]Pbffff99" \
  300. "\e]Pc9999ff" \
  301. "\e]Pdff99ff" \
  302. "\e]Pe99ffff" \
  303. "\e]Pfffffff"
  304. }
  305. # printf "\e]P7353535" \
  306. _colors(){
  307. echo -e \
  308. "\e[30mBlack" \
  309. "\e[31mRed" \
  310. "\e[32mGreen" \
  311. "\e[33mYellow" \
  312. "\e[34mBlue" \
  313. "\e[35mMagenta" \
  314. "\e[36mCyan" \
  315. "\e[37mWhite"
  316. echo -e \
  317. "\e[30;1mBright Black" \
  318. "\e[31;1mBright Red" \
  319. "\e[32;1mBright Green" \
  320. "\e[33;1mBright Yellow" \
  321. "\e[34;1mBright Blue" \
  322. "\e[35;1mBright Magenta" \
  323. "\e[36;1mBright Cyan" \
  324. "\e[37;1mBright White\n" \
  325. "\e[0m"
  326. }
  327. _my_install_script(){
  328. mkdir -p $HOMO/bin/
  329. for f in "$@"
  330. do
  331. bn=$(basename "$f")
  332. type $bn >/dev/null 2>&1 || {
  333. wget "$f" -P "$HOME/bin/" &&
  334. chmod u+x "$HOME/bin/${bn}"
  335. }
  336. done
  337. }
  338. _my_install_script http://www.frexx.de/xterm-256-notes/data/colortable16.sh
  339. winln(){
  340. # for windose make link (actually junction)
  341. if test $# -eq 0
  342. then
  343. {
  344. echo "usage: winln TARGET LINK_NAME"
  345. echo "Create a link to TARGET with the name LINK_NAME (that is, TARGET must already exist)."
  346. echo "About other features run 'junction'."
  347. } 1>&2
  348. return 1
  349. else
  350. junction "$2" "$1"
  351. fi
  352. }
  353. __my_battery_status(){
  354. local dir=/sys/class/power_supply/BAT0
  355. if test -d $dir
  356. then
  357. local st=$(cat $dir/status)
  358. local full=$(cat $dir/charge_full)
  359. local now=$(cat $dir/charge_now)
  360. local rate=$(expr $now \* 100 / $full)
  361. printf "$1" "${st}:${rate}%"
  362. fi
  363. }
  364. alias bat='__my_battery_status %s\\n'
  365. ip-address(){
  366. local ip=$(LANG=C ifconfig | \grep --color=never "inet " | \grep --color=never -v "127.0.0.1" | awk '{print $2}')
  367. test -n "$ip" && printf $1 $ip
  368. }
  369. __my_prompt_function(){ # used by PS1
  370. # remove __try_exec from function
  371. local lastreturn=$?
  372. if test "${TERM}" == dumb
  373. then
  374. local c1=
  375. local c2=
  376. local c3=
  377. local cdef=
  378. else
  379. local c1="\e[33m"
  380. local c2="\e[36m"
  381. local c3="\e[37m"
  382. local cdef="\e[0m"
  383. fi
  384. if iswindows
  385. then
  386. local pwd=$PWD
  387. local oldpwd=$OLDPWD
  388. git branch >/dev/null 2>&1 && local git="[GIT]"
  389. local date=$(/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%')
  390. else
  391. local pwd=$(echo "${PWD}/" | sed -e "s#${HOME}#~#")
  392. local oldpwd=$(echo "${OLDPWD}/" | sed -e "s#${HOME}#~#")
  393. local jobnum=$(jobs | wc -l) # a strange job exists...
  394. local git=$(__try_exec __git_ps1 [GIT:%s])
  395. local date=$(LANG=C __try_exec date +"%a, %d %b %Y %T %z")
  396. fi
  397. local dirs=$(dirs | wc -l)
  398. # local svn=$(type svn >/dev/null 2>&1 && __try_exec __my_svn_ps1 [SVN:%s])
  399. if test -z "$DISPLAY" && ! iswindows
  400. then
  401. local ip=$(ip-address [Addr:%s])
  402. local bst="/tmp/${USER}-tmp/batterystatus"
  403. test -f $bst && local battery="[Battery:$(sed -e 's`%`%%`g' $bst)]"
  404. __my_battery_status %s >$bst &
  405. fi
  406. # local battery=$(battery-state [%s] | sed -e 's`%`%%`g') # very slow
  407. printf " [${c1}${pwd}${cdef}<${c3}${oldpwd}${cdef}]${git}${svn}${battery}${ip}\n"
  408. printf "${c2}${USER}@${HOSTNAME}${cdef} ${date}\n"
  409. printf "shlv:${SHLVL} dirs:${dirs} last:${lastreturn} "
  410. }
  411. __my_set_title(){
  412. title="$(echo $@)"
  413. case $TERM in
  414. (rxvt*|xterm*|aterm)
  415. test -t 1 &&
  416. test -n "$DISPLAY" &&
  417. test -z "$EMACS" &&
  418. echo -n -e "\033]0;${1}\007"
  419. ;;
  420. esac
  421. }
  422. # export PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\ \${PWD};${PROMPT_COMMAND}"
  423. # copied from https://wiki.archlinux.org/index.php/X_resources
  424. invader(){
  425. # ANSI color scheme script featuring Space Invaders
  426. #
  427. # Original: http://crunchbanglinux.org/forums/post/126921/#p126921
  428. # Modified by lolilolicon
  429. #
  430. f=3 b=4
  431. for j in f b; do
  432. for i in {0..7}; do
  433. printf -v $j$i %b "\e[${!j}${i}m"
  434. done
  435. done
  436. bld=$'\e[1m'
  437. rst=$'\e[0m'
  438. cat << EOF
  439. $f1 ▀▄ ▄▀ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4 ▀▄ ▄▀ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  440. $f1 ▄█▀███▀█▄ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4 ▄█▀███▀█▄ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  441. $f1█▀███████▀█ $f2▀▀███▀▀███▀▀ $f3▀█▀██▀█▀ $f4█▀███████▀█ $f5▀▀███▀▀███▀▀ $f6▀█▀██▀█▀$rst
  442. $f1▀ ▀▄▄ ▄▄▀ ▀ $f2 ▀█▄ ▀▀ ▄█▀ $f3▀▄ ▄▀ $f4▀ ▀▄▄ ▄▄▀ ▀ $f5 ▀█▄ ▀▀ ▄█▀ $f6▀▄ ▄▀$rst
  443. $bld$f1▄ ▀▄ ▄▀ ▄ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4▄ ▀▄ ▄▀ ▄ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  444. $bld$f1█▄█▀███▀█▄█ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4█▄█▀███▀█▄█ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  445. $bld$f1▀█████████▀ $f2▀▀▀██▀▀██▀▀▀ $f3▀▀█▀▀█▀▀ $f4▀█████████▀ $f5▀▀▀██▀▀██▀▀▀ $f6▀▀█▀▀█▀▀$rst
  446. $bld$f1 ▄▀ ▀▄ $f2▄▄▀▀ ▀▀ ▀▀▄▄ $f3▄▀▄▀▀▄▀▄ $f4 ▄▀ ▀▄ $f5▄▄▀▀ ▀▀ ▀▀▄▄ $f6▄▀▄▀▀▄▀▄$rst
  447. $f7▌$rst
  448. $f7▌$rst
  449. $f7 ▄█▄ $rst
  450. $f7▄█████████▄$rst
  451. $f7▀▀▀▀▀▀▀▀▀▀▀$rst
  452. EOF
  453. }
  454. #/etc/lsb-release