]> code.delx.au - dotfiles/blob - .bash/interactive
bash: refactored colors and moved into interactive
[dotfiles] / .bash / interactive
1 #! bash
2
3 # Check for unsupported TERM variable
4 if ! tput init &> /dev/null; then
5 echo "Warning! TERM=$TERM unsupported, using TERM=xterm"
6 export TERM=xterm
7 fi
8
9
10
11 # Fancy PS1 with revision control status for git, hg, svn
12
13 [ -r /usr/share/git/completion/git-prompt.sh ] && source /usr/share/git/completion/git-prompt.sh
14 function my_git_ps1 {
15 find_up_recurse .git || return
16 GIT_PS1_SHOWDIRTYSTATE=1 \
17 GIT_PS1_SHOWUNTRACKEDFILES=1 \
18 __git_ps1 2> /dev/null
19 }
20
21 function my_hg_ps1 {
22 find_up_recurse .hg || return
23 b="$(hg branch 2>/dev/null)" || return
24 s="$(hg status | cut -c1 | sort -u | tr -d " \n")"
25 echo -n " ($b"
26 [ -n "$s" ] && echo -n " $s"
27 echo -n ")"
28 }
29
30 function my_svn_ps1 {
31 find_up_recurse .svn || return
32 s="$(svn status --ignore-externals 2>/dev/null | cut -c1 | sort -u | tr -d " \n")"
33 [ -z "$s" ] && return
34 echo -n " ($s)"
35 }
36
37 if [ -z "$debian_chroot" -a -r /etc/debian_chroot ]; then
38 debian_chroot=$(cat /etc/debian_chroot)
39 fi
40 PS1='${debian_chroot:+($debian_chroot)}'
41 PS1="$PS1"'\[\033[00;31m\]\u@\h\[\033[00m\]:\[\033[00;34m\]\w\[\033[00m\]'
42 PS1="$PS1"'\[\033[00;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\033[00m\]'
43 PS1="$PS1"'\n\$ '
44
45
46
47
48 # ls colours
49 eval $(TERM=xterm dircolors 2> /dev/null)
50
51 if [ "${DARKTERM:-1}" -eq 1 ]; then
52
53 # Sets colour scheme in apps like Vim
54 export COLORFGBG="15;0"
55
56 # Bold ls colours
57 LS_COLORS="$(echo "${LS_COLORS}" | sed 's/00;/01;/g')"
58 LSCOLORS="ExFxCxDxBxEGEDABAGACAD" # BSD ls
59 PS1="$(echo "${PS1}" | sed 's/00;/01;/g')"
60
61 else
62
63 # Sets colour scheme in apps like Vim
64 export COLORFGBG="0;15"
65
66 # Unbold ls colours
67 LS_COLORS="$(echo "${LS_COLORS}" | sed 's/01;/00;/g')"
68 LSCOLORS="exfxcxdxbxegedabagacad" # BSD ls
69 fi
70
71
72 # xterm titlebar displays 'hostname:workingdir'
73 if tput hs || tput tsl &> /dev/null; then
74 PROMPT_COMMAND='tput tsl; echo -n "$(hostname| cut -d. -f1):${PWD/$HOME/~}"; tput fsl;'
75 fi
76
77 # screen window displays current command
78 case "${TERM}" in
79 screen*)
80 PROMPT_COMMAND="$PROMPT_COMMAND echo -ne '\033k\033\\'"
81 ;;
82 esac
83
84 # Useful aliases
85 alias ls='ls --color=auto'
86 alias ll='ls -hlF'
87 alias la='ls -ha'
88 alias l='ls -halF'
89 alias f='find . -iname'
90 alias webshare='python -c "import SimpleHTTPServer; SimpleHTTPServer.test()"'
91 alias rm='rm -i'
92 alias less='less -R'
93 alias grep='grep --color=auto --exclude "*.svn-base"'
94 alias scp='scp -o ControlPath=none'
95 alias bc='bc -ql'
96 alias vv='vncviewer -encodings "tight hextile copyrect"'
97 alias watch='watch -n1'
98 alias sudo='sudo ' # ability to use aliases with sudo
99 alias sudosu='sudo su -l -s /bin/bash'
100 alias _psresources='ps -weo pid,user:16,%cpu,%mem,stat,start_time,bsdtime,args'
101 alias pscpu='_psresources --sort -%cpu|less -S'
102 alias psmem='_psresources --sort -%mem|less -S'
103 alias pstree='ps --forest -weo pid,user:16,args --sort start_time|less -S'
104 alias pstime='ps -weo pid,user:16,lstart,args --sort start_time|less -S'
105
106
107 # Colorful man pages
108 function man {
109 env \
110 LESS_TERMCAP_md=$'\E[01;38;5;74m' \
111 LESS_TERMCAP_me=$'\E[0m' \
112 LESS_TERMCAP_us=$'\E[04;38;5;146m' \
113 LESS_TERMCAP_ue=$'\E[0m' \
114 man "$@"
115 }
116
117 # Usage: mcd somedir
118 # Creates the directory if it doesn't exist, and changes into it
119 function mcd {
120 mkdir -p "${1}" &&
121 cd "${1}"
122 }
123
124 # Usage: vimf somefile
125 # Does a recursive search of the current directory for somefile, then edits it
126 function vimf {
127 find . -iname "${1}" -exec vim '{}' +
128 }
129
130 # Sets the nice and ionice priorities for the current shell to the lowest values
131 function slowshell {
132 ionice -c 3 -p $$
133 renice -n 19 -p $$
134 }
135
136
137 # Bash should check the terminal size after every command terminates
138 shopt -s checkwinsize
139
140 # Don't attempt to tab-complete an empty line
141 shopt -s no_empty_cmd_completion
142
143
144 # Display return codes on error
145 function print_exit_code {
146 _exit_msg="\033[01;33mexit code: $?\033[00m"
147 if [ -z "${BASH_SOURCE[1]}" ]; then
148 echo -e "$_exit_msg"
149 fi
150 unset _exit_msg
151 }
152 trap print_exit_code ERR
153
154
155
156 # Local customisations
157 [ -r "${HOME}/.bash/interactive_local" ] && source "${HOME}/.bash/interactive_local"
158
159 # Load bash completion if available
160 [ -r "/etc/bash_completion" ] && source "/etc/bash_completion"
161