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.
 
 
 
 
 
 

493 lines
14 KiB

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