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

bashrc 13 KiB

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