選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

442 行
11 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 VISUAL="$EDITOR"
  37. export LESS="-iRMX"
  38. export LC_MESSAGES=C
  39. export CDPATH=".:~"
  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. if [ "${EMACS}" = "t" ]; then # for emacs shell
  56. true export PS1="\u@\H \d \t \w\nemacs shell\$ "
  57. elif echo "$EMACS" | grep term >/dev/null 2>&1; then # for emacs term
  58. echo "Emacs Term"
  59. fi
  60. # if test -f /etc/issue
  61. # then
  62. # cat /etc/issue
  63. # fi
  64. ###################################
  65. # some aliases and functions
  66. alias ls="ls -hCF $(test "$TERM" == dumb || echo --color=auto\ )--time-style=long-iso"
  67. # alias ll="ls -l"
  68. # alias la="ls -A"
  69. # alias lla="ls -Al"
  70. # alias less=""
  71. alias vl=/usr/share/vim/vimcurrent/macros/less.sh
  72. alias em="emacs -nw"
  73. alias pstree="LANG=C pstree"
  74. # alias apt-get="sudo apt-get"
  75. alias ut="ssh 6365454829@un001.ecc.u-tokyo.ac.jp"
  76. alias rand="echo \$RANDOM"
  77. alias xunp="file-roller -h"
  78. alias pacome="sudo \paco -D"
  79. alias psall="ps auxww"
  80. alias q=exit
  81. alias p="$PAGER"
  82. alias c=cat
  83. alias pcalc="python -i -c 'from math import *' "
  84. alias py3=python3
  85. alias _reloadrc="test -f ~/.bashrc && source ~/.bashrc"
  86. alias sudo="sudo " # use aliases through sudo
  87. alias e3=e3em
  88. alias mytime="date +%Y%m%d-%H%M%S"
  89. alias sh="ENV=$HOME/.shrc PS1=\$\ sh"
  90. alias halt="sudo halt"
  91. alias reboot="sudo reboot"
  92. # type trash >/dev/null 2>&1 && alias rm=trash
  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="yaourt -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. else
  135. echo "X cant be started! Maybe another X is already running!" 1>&2
  136. fi
  137. }
  138. export __MYGITBAREREP="${HOME}/dbx/.git-bare"
  139. git-make-local-rep(){
  140. test $# -eq 0 && {
  141. echo "specify repository name." 1>&2
  142. return 1
  143. }
  144. dir="${__MYGITBAREREP}/$1.git"
  145. cdir=$PWD
  146. if test -d "$dir"
  147. then
  148. echo "dir $dir already exist!" 1>&2
  149. else
  150. mkdir -p "$dir" && {
  151. cd "$dir" &&
  152. git init --bare --shared=all
  153. }
  154. fi
  155. cd ${cdir}
  156. }
  157. bak(){
  158. for file in "$@"
  159. do
  160. mv -v ${file} ${file}.bak
  161. done
  162. }
  163. di(){
  164. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  165. then
  166. local diffcmd=colordiff
  167. else
  168. local diffcmd=diff
  169. fi
  170. ${diffcmd} -u "$@" | ${PAGER}
  171. }
  172. throw-away(){
  173. mkdir -p ~/.backup/tb
  174. for file in "$@"
  175. do
  176. mv $file ~/.backup/tb
  177. done
  178. }
  179. mkcd(){
  180. mkdir -p $1
  181. cd $1
  182. }
  183. catclip(){
  184. if iswindows
  185. then
  186. cat /dev/clipboard | tr -d \\r
  187. else
  188. xclip -o -selection "clipboard"
  189. fi
  190. }
  191. setclip(){
  192. if iswindows
  193. then
  194. if test $# -eq 0
  195. then
  196. sed -e 's/$/\r/' | tee /dev/clipboard
  197. else
  198. cat "$@" | sed -e 's/$/\r/' | tee /dev/clipboard
  199. fi
  200. else
  201. if test $# -eq 0
  202. then
  203. xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  204. else
  205. cat "$@" | xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  206. fi
  207. fi
  208. }
  209. o(){
  210. if [ $# -eq 0 ]
  211. then
  212. local f=.
  213. else
  214. local f="$1"
  215. fi
  216. if iswindows
  217. then
  218. cmd.exe //c start "" "$f"
  219. elif isdarwin
  220. then
  221. open "$f"
  222. elif type pcmanfm >/dev/null 2>&1
  223. then
  224. pcmanfm "$f"
  225. else
  226. xdg-open "$f"
  227. fi
  228. }
  229. convmv-sjis2utf8-test(){
  230. convmv -r -f sjis -t utf8 *
  231. }
  232. convmv-sjis2utf8-notest(){
  233. convmv -r -f sjis -t utf8 * --notest
  234. }
  235. _mygitconfig(){
  236. git config --global user.name '10sr'
  237. git config --global user.email '8slashes+git@gmail.com'
  238. git config --global core.autocrlf false
  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=format:\"<%h> %ad [%an] %Cgreen%d%Creset %s\" --all --date=short"
  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. # http://www.frexx.de/xterm-256-notes/data/colortable16.sh
  315. _install_script(){
  316. mkdir -p $HOMO/bin/
  317. for f in "$@"
  318. do
  319. bn=$(basename "$f")
  320. type ${bn} >/dev/null 2>&1 || wget "$f" -P "$HOME/bin/"
  321. chmod u+x "$HOME/bin/${bn}"
  322. done
  323. }
  324. winln(){
  325. # for windose make link (actually junction)
  326. if test $# -eq 0
  327. then
  328. {
  329. echo "usage: winln TARGET LINK_NAME"
  330. echo "Create a link to TARGET with the name LINK_NAME (that is, TARGET must already exist)."
  331. echo "About other features run 'junction'."
  332. } 1>&2
  333. return 1
  334. else
  335. junction "$2" "$1"
  336. fi
  337. }
  338. battery-status(){
  339. local dir=/sys/class/power_supply/BAT0
  340. if test -d $dir
  341. then
  342. local st=$(cat $dir/status)
  343. local full=$(cat $dir/charge_full)
  344. local now=$(cat $dir/charge_now)
  345. local rate=$(expr $now \* 100 / $full)
  346. printf "$1" "${st}:${rate}%"
  347. fi
  348. }
  349. alias bat='battery-status %s\\n'
  350. battery-status2(){
  351. local dir=/sys/class/power_supply/BAT0
  352. . $dir/uevent
  353. local rate=$(expr $POWER_SUPPLY_CHARGE_NOW \* 100 / $POWER_SUPPLY_CHARGE_FULL)
  354. echo ${POWER_SUPPLY_STATUS}:${rate}%
  355. }
  356. ip-address(){
  357. local ip=$(LANG=C ifconfig | grep "inet " | grep -v "127.0.0.1" | awk '{print $2}')
  358. test -n "$ip" && printf $1 $ip
  359. }
  360. __my_prompt_function(){ # used by PS1
  361. local lastreturn=$?
  362. if test "${TERM}" == dumb
  363. then
  364. local c1=
  365. local c2=
  366. local c3=
  367. local cdef=
  368. else
  369. local c1="\e[33m"
  370. local c2="\e[36m"
  371. local c3="\e[37m"
  372. local cdef="\e[0m"
  373. fi
  374. if iswindows
  375. then
  376. local pwd=$PWD
  377. local oldpwd=$OLDPWD
  378. git branch >/dev/null 2>&1 && local git="[GIT]"
  379. local date=$(/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%')
  380. else
  381. local pwd=$(echo "${PWD}/" | sed -e "s#${HOME}#~#")
  382. local oldpwd=$(echo "${OLDPWD}/" | sed -e "s#${HOME}#~#")
  383. local jobnum=$(jobs | wc -l)
  384. local git=$(__try_exec __git_ps1 [GIT:%s])
  385. local date=$(LANG=C __try_exec date +"%a, %d %b %Y %T %z")
  386. fi
  387. # local svn=$(type svn >/dev/null 2>&1 && __try_exec __my_svn_ps1 [SVN:%s])
  388. if test -z "$DISPLAY"
  389. then
  390. local ip=$(ip-address [Addr:%s])
  391. test -f /tmp/batterystatus && local battery="[Battery:$(sed -e 's`%`%%`g' /tmp/batterystatus)]"
  392. battery-status %s >/tmp/batterystatus &
  393. fi
  394. local tty=$(__try_exec tty | sed -e 's:/dev/::')
  395. # local battery=$(battery-state [%s] | sed -e 's`%`%%`g') # very slow
  396. printf " [${c1}${pwd}${cdef}<${c3}${oldpwd}${cdef}]${git}${svn}${battery}${ip}\n"
  397. printf "${c2}${USER}@${HOSTNAME}${cdef} ${tty} ${date} ${BASH} ${BASH_VERSION}\n"
  398. printf "shlv:${SHLVL} jobs:${jobnum} last:${lastreturn} "
  399. }