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.
 
 
 
 
 
 

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