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.
 
 
 
 
 
 

499 regels
14 KiB

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