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.
 
 
 
 
 
 

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