]> code.delx.au - dotfiles/blob - .bash/interactive
bash: BSD friendly
[dotfiles] / .bash / interactive
1 #! bash
2
3
4
5 ##################
6 # Terminal setup #
7 ##################
8
9 # Check for unsupported TERM variable
10 if ! tput init &> /dev/null; then
11 echo "Warning! TERM=$TERM unsupported, using TERM=xterm"
12 export TERM=xterm
13 fi
14
15 # Disable CTRL-s / CTRL-q
16 stty -ixon
17
18 # Sets colour scheme in apps like Vim
19 export COLORFGBG="15;0"
20
21
22
23 #############
24 # Fancy PS1 #
25 #############
26
27 # Revision control status for git, hg, svn
28
29 [ -r /usr/share/git/completion/git-prompt.sh ] && source /usr/share/git/completion/git-prompt.sh
30 function my_git_ps1 {
31 find_up_recurse .git || return
32 GIT_PS1_SHOWDIRTYSTATE=1 \
33 GIT_PS1_SHOWUNTRACKEDFILES=1 \
34 __git_ps1 2> /dev/null
35 }
36
37 function my_hg_ps1 {
38 find_up_recurse .hg || return
39 b="$(hg branch 2>/dev/null)" || return
40 s="$(hg status | cut -c1 | sort -u | tr -d " \n")"
41 echo -n " ($b"
42 [ -n "$s" ] && echo -n " $s"
43 echo -n ")"
44 }
45
46 function my_svn_ps1 {
47 find_up_recurse .svn || return
48 s="$(svn status --ignore-externals 2>/dev/null | cut -c1 | sort -u | tr -d " \n")"
49 [ -z "$s" ] && return
50 echo -n " ($s)"
51 }
52
53 # Two line prompt
54
55 PS1=''
56 PS1="$PS1"'\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]'
57 PS1="$PS1"'\[\033[01;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\033[00m\]'
58 PS1="$PS1"'\n\$ '
59
60
61
62 ################
63 # xterm titles #
64 ################
65
66 # hostname:workingdir
67 DISPLAY_TITLE_COMMAND='echo -ne "\033]0;$(hostname| cut -d. -f1):${PWD/$HOME/~}\007"'
68
69 if [[ "$TERM" =~ ^xterm ]]; then
70 PROMPT_COMMAND="$DISPLAY_TITLE_COMMAND"
71 fi
72
73 if [[ "$TERM" =~ ^screen ]]; then
74 PROMPT_COMMAND="${DISPLAY_TITLE_COMMAND}; echo -ne '\033k\033\\'"
75 fi
76
77
78
79 #################################
80 # Display return codes on error #
81 #################################
82
83 function print_exit_code {
84 _exit_msg="\033[01;33mexit code: $?\033[00m"
85 if [ -z "${BASH_SOURCE[1]}" ]; then
86 echo -e "$_exit_msg"
87 fi
88 unset _exit_msg
89 }
90 trap print_exit_code ERR
91
92
93
94 ################
95 # bash options #
96 ################
97
98 # Bash should check the terminal size after every command terminates
99 shopt -s checkwinsize
100
101 # Don't attempt to tab-complete an empty line
102 shopt -s no_empty_cmd_completion
103
104 # Prevent overwriting existing files on stdout redirection
105 set -o noclobber
106
107 # Better history
108 shopt -s histappend
109 shopt -s cmdhist
110 export HISTCONTROL="erasedups:ignoredups"
111 export HISTSIZE="100000"
112 export HISTTIMEFORMAT="%F %T "
113
114
115
116 ##########################
117 # ls aliases and colours #
118 ##########################
119
120 # GNU ls colours
121 eval $(TERM=xterm dircolors 2> /dev/null)
122
123 # BSD ls colours
124 export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
125
126 # Lets find the ls
127 if ls --color=auto -v &> /dev/null; then
128 alias ls='ls --color=auto -v'
129 elif gls --color=auto -v &> /dev/null; then
130 alias ls='gls --color=auto -v'
131 elif ls -G &> /dev/null; then
132 alias ls='ls -G'
133 else
134 alias ls='ls -F'
135 fi
136 alias ll='ls -hlF'
137 alias la='ls -ha'
138 alias l='ls -halF'
139
140
141
142 ##############
143 # ps aliases #
144 ##############
145
146 alias _psresources='ps -wAo pid,user,%cpu,%mem,stat,start,time,args'
147 if [ "$(uname)" = "Linux" ]; then
148 alias pscpu='_psresources --sort -%cpu|less -S'
149 alias psmem='_psresources --sort -%mem|less -S'
150 alias pstree='ps --forest -weo pid,user:16,args --sort start_time|less -S'
151 alias pstime='ps -wAo pid,user,lstart,args --sort start_time|less -S'
152 else
153 alias pscpu='_psresources -r|less -S'
154 alias psmem='_psresources -m|less -S'
155 alias pstime='ps -wAo pid,user,lstart,args|less -S'
156 fi
157
158
159 ##################
160 # Useful aliases #
161 ##################
162
163 alias f='find . -iname'
164 alias webshare='python3 -mhttp.server'
165 alias rm='rm -i'
166 alias less='less -R'
167 if echo x | grep -q --color=auto x &> /dev/null; then
168 alias grep='grep --color=auto'
169 fi
170 alias scp='scp -o ControlPath=none'
171 alias bc='bc -ql'
172 alias watch='watch -n1'
173 alias sudo='sudo ' # ability to use aliases with sudo
174 alias sudosu='sudo su -l -s /bin/bash'
175 alias pbcopy='xsel --clipboard --input'
176 alias pbcopym='xsel --input'
177 alias pbpaste='xsel --clipboard --output'
178 alias pbpastem='xsel --output'
179
180 # Super man!
181 # Colourful headings
182 # Terminal title
183 function man {
184 echo -ne "\033]0;man $@\007"
185
186 env \
187 LESS_TERMCAP_md=$'\E[01;38;5;74m' \
188 LESS_TERMCAP_me=$'\E[0m' \
189 LESS_TERMCAP_us=$'\E[04;38;5;146m' \
190 LESS_TERMCAP_ue=$'\E[0m' \
191 man "$@"
192 }
193
194 # Usage: mcd somedir
195 # Creates the directory if it doesn't exist, and changes into it
196 function mcd {
197 mkdir -p "${1}" &&
198 cd "${1}"
199 }
200
201 # Usage: editf somefile
202 # Does a recursive search of the current directory for somefile, then edits it
203 function editf {
204 find . -iname "${1}" -exec $EDITORBG '{}' +
205 }
206
207 # Usage: edit somefile [otherfiles ...]
208 function edit {
209 $EDITORBG "$@"
210 }
211
212 # Sets the nice and ionice priorities for the current shell to the lowest values
213 function slowshell {
214 ionice -c 3 -p $$
215 renice -n 19 -p $$
216 }
217
218 # SSH to an unknown host and print the new known_hosts entry
219 function ssh_new {
220 local new_known_hosts_file="$(mktemp)"
221 ssh -o UserKnownHostsFile="$new_known_hosts_file" "$@" echo Connection ok
222 cat "$new_known_hosts_file"
223 rm -f "$new_known_hosts_file"
224 }
225
226 # SSH without verifying host key
227 function ssh_unsafe {
228 ssh -o StrictHostKeyChecking=no "$@"
229 }
230
231
232
233 ###########
234 # The end #
235 ###########
236
237 # Local customisations
238 [ -r "${HOME}/.bash/interactive_local" ] && source "${HOME}/.bash/interactive_local"
239
240 # Load bash completion if available
241 [ -r "/etc/bash_completion" ] && source "/etc/bash_completion"
242