]> code.delx.au - dotfiles/blob - .bashrc
vim: enable modeline
[dotfiles] / .bashrc
1 # shellcheck disable=SC1090 disable=SC1091 # -- Can't follow non-constant source
2 # shellcheck disable=SC1003 # -- Don't complain about '\e\\'
3
4 ###########################
5 # Settings for all shells #
6 ###########################
7
8 # Set umask, depending on the user ID
9 if [ "$(id -un)" = "root" ]; then
10 umask 0022
11 else
12 umask 0007
13 fi
14
15 # Add ~/bin and subdirs to PATH if needed
16 while read -r p; do
17 echo "$PATH" | tr ':' '\n' | grep -qxF "$p" || PATH="${p}:$PATH"
18 done < <(find -L ~/bin -depth -maxdepth 1 -type d 2> /dev/null)
19
20 # Set environment variables, eg EMAIL, LANG
21 eval "$(cat 2> /dev/null /etc/default/locale /etc/locale.conf ~/.config/environment.d/*.conf | awk -F= '/^[^# ]/ {print $0 ";export " $1}')"
22
23 # Pick up SSH agent socket
24 if [ -z "$SSH_AUTH_SOCK" ]; then
25 if [ -r ~/.ssh-agent.env ]; then
26 source ~/.ssh-agent.env
27 else
28 SSH_AUTH_SOCK="/run/user/$(id -u)/keyring/ssh"
29 export SSH_AUTH_SOCK
30 fi
31 fi
32
33 # Look for XDG_RUNTIME_DIR
34 if [ -z "$XDG_RUNTIME_DIR" ] && [ -d "/run/user/$(id -u)" ]; then
35 XDG_RUNTIME_DIR="/run/user/$(id -u)"
36 export XDG_RUNTIME_DIR
37 fi
38
39 # Look for DBUS
40 if [ -z "$DBUS_SESSION_BUS_ADDRESS" ] && [ -S "/run/user/$(id -u)/bus" ]; then
41 DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus"
42 export DBUS_SESSION_BUS_ADDRESS
43 fi
44
45
46 ###########################
47 # Interactive shells only #
48 ###########################
49
50 if [ -z "${PS1}" ]; then
51 return
52 fi
53
54
55 ################
56 # Source files #
57 ################
58
59 # Debian/Ubuntu don't source this from /etc/bash.bashrc
60 [ -z "$BASH_COMPLETION_VERSINFO" ] && [ -r /usr/share/bash-completion/bash_completion ] && source /usr/share/bash-completion/bash_completion
61
62 # This isn't sourced for interactive shells on Debian/Ubuntu/Arch
63 # https://bugzilla.gnome.org/show_bug.cgi?id=697475
64 [ -r /etc/profile.d/vte.sh ] && source /etc/profile.d/vte.sh
65
66
67 ################
68 # bash options #
69 ################
70
71 # Bash should check the terminal size after every command terminates
72 shopt -s checkwinsize
73
74 # Don't attempt to tab-complete an empty line
75 shopt -s no_empty_cmd_completion
76
77 # Prevent overwriting existing files on stdout redirection
78 set -o noclobber
79
80 # Better history
81 shopt -s histappend
82 shopt -s cmdhist
83 export HISTCONTROL='erasedups:ignoredups:ignorespace'
84 export HISTSIZE='100000'
85 export HISTTIMEFORMAT='%F %T '
86
87
88 ###############
89 # Pager setup #
90 ###############
91
92 export PAGER='less'
93 export LESS='RS'
94
95
96 ##################
97 # Terminal setup #
98 ##################
99
100 # Disable CTRL-s / CTRL-q
101 stty -ixon
102
103 # Use dark background colors in apps like vim
104 export COLORFGBG='15;0'
105
106 #############
107 # Fancy PS1 #
108 #############
109
110 # Revision control status for git, hg, svn
111
112 function find_up_exists {
113 local d="$PWD"
114 while [ -n "$d" ]; do
115 if [ -e "$d/$1" ]; then
116 return 0
117 fi
118 d="${d%/*}"
119 done
120 return 1
121 }
122
123 for p in /usr/lib/git-core/git-sh-prompt \
124 /usr/share/git/completion/git-prompt.sh \
125 /usr/share/git-core/contrib/completion/git-prompt.sh \
126 /usr/local/share/git-core/contrib/completion/git-prompt.sh
127 do
128 [ -r "$p" ] && source "$p" && break
129 done
130
131 function my_git_ps1 {
132 find_up_exists .git || return
133 GIT_PS1_SHOWDIRTYSTATE=1 \
134 GIT_PS1_SHOWUNTRACKEDFILES=1 \
135 __git_ps1 ' (%s)' 2> /dev/null
136 }
137
138 function my_hg_ps1 {
139 find_up_exists .hg || return
140 local status
141 status="$(hg status | cut -c1 | sort -u | tr -d ' \n')"
142 echo -n " ($status)"
143 }
144
145 function my_svn_ps1 {
146 find_up_exists .svn || return
147 local status
148 status="$(svn status --ignore-externals 2> /dev/null | cut -c1 | sort -u | tr -d ' \n')"
149 [ -n "$status" ] && echo -n " ($status)"
150 }
151
152 # Two line prompt
153 PS1=''
154 PS1="$PS1"'\[\e[01;31m\]\u@\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[00m\]'
155 PS1="$PS1"'\[\e[01;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\e[00m\]'
156 PS1="$PS1"'\n\$ '
157
158
159 #################################
160 # Display return codes on error #
161 #################################
162
163 function print_exit_code {
164 local exit_code="$?"
165 if [ -z "${BASH_SOURCE[1]}" ]; then
166 printf '\e[01;33mexit code: %s\e[00m\n' \
167 "$exit_code"
168 fi
169 }
170 trap print_exit_code ERR
171
172
173
174 ####################
175 # Function aliases #
176 ####################
177
178 # This prevents long/ugly command lines from showing up in xterm titles
179 function aliasf {
180 eval "function $1 { $2 \"\$@\"; }"
181 }
182
183
184 ##########################
185 # ls aliases and colours #
186 ##########################
187
188 if command -v dircolors &> /dev/null; then
189 # GNU ls colours
190 eval "$(dircolors)"
191 LS_COLORS+=':ow=30;42'
192 else
193 # BSD ls colours
194 export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
195 fi
196
197 # Lets find the ls
198 if ls --color=auto -v &> /dev/null; then
199 alias ls='ls --color=auto -v'
200 elif gls --color=auto -v &> /dev/null; then
201 alias ls='gls --color=auto -v'
202 elif ls -G &> /dev/null; then
203 alias ls='ls -G'
204 else
205 alias ls='ls -F'
206 fi
207 alias ll='ls -hlF'
208 alias la='ls -ha'
209 alias l='ls -halF'
210
211
212 ##############
213 # ps aliases #
214 ##############
215
216 alias _psresources='ps -wAo pid,user,%cpu,%mem,stat,start,time,args'
217 if [ "$(uname)" = "Linux" ]; then
218 aliasf pscpu '_psresources --sort -%cpu|less -S'
219 aliasf psmem '_psresources --sort -%mem|less -S'
220 aliasf pstree 'ps --forest -weo pid,user:16,args --sort start_time|less -S'
221 aliasf pstime 'ps -wAo pid,user,lstart,args --sort start_time|less -S'
222 else
223 aliasf pscpu '_psresources -r|less -S'
224 aliasf psmem '_psresources -m|less -S'
225 aliasf pstime 'ps -wAo pid,user,lstart,args|less -S'
226 fi
227
228 #################
229 # Other aliases #
230 #################
231
232 alias f='find . -iname'
233 if echo x | grep -q --color=auto x &> /dev/null; then
234 alias grep='grep --color=auto'
235 fi
236 alias rg='rg -p'
237 alias bc='bc -ql'
238 alias watch='watch -n1'
239 alias sudo='sudo ' # ability to use aliases with sudo
240 aliasf sudosu 'sudo su -l -s /bin/bash'
241 aliasf python 'PYTHONSTARTUP=~/.pythonrc.py python3'
242 aliasf webshare 'python3 -mhttp.server'
243
244 export EDITOR='vim'
245 aliasf edit "$EDITOR"
246
247 if ! command -v pbcopy &> /dev/null; then
248 alias pbcopy='xsel --clipboard --input'
249 alias pbcopym='xsel --input'
250 alias pbpaste='xsel --clipboard --output'
251 alias pbpastem='xsel --output'
252 fi
253
254 # man with coloured headings and a terminal title
255 function man {
256 env \
257 LESS_TERMCAP_md=$'\E[01;38;5;74m' \
258 LESS_TERMCAP_me=$'\E[0m' \
259 LESS_TERMCAP_us=$'\E[04;38;5;146m' \
260 LESS_TERMCAP_ue=$'\E[0m' \
261 LC_CTYPE=C \
262 man "$@"
263 }
264
265 # Creates the directory if it doesn't exist, and changes into it
266 function mcd {
267 # shellcheck disable=SC2164
268 mkdir -p "$1" && cd "$1"
269 }
270
271 # Sets the nice and ionice priorities for the current shell to the lowest values
272 function slowshell {
273 ionice -c 3 -p $$
274 renice -n 19 -p $$
275 }
276
277 # SSH without verifying host key
278 function ssh_unsafe {
279 ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"
280 }
281
282
283 #########################
284 # Optional local config #
285 #########################
286
287 [ -r ~/.bashrc_local ] && source ~/.bashrc_local
288
289
290 ########################
291 # Terminal integration #
292 ########################
293
294 # When at a prompt display `workingdir (hostname)`
295 function print_title_prompt {
296 printf '\e]0;bash:%s (%s)\a' \
297 "${PWD/$HOME/\~}" \
298 "$(cat /etc/hostname)"
299
300 if [[ "$TERM" == screen* ]]; then
301 printf '\ekbash\e\\'
302 fi
303 }
304
305 # Preserve working directory when opening new terminals
306 # This depends on /etc/profile/vte.sh
307 function record_terminal_cwd {
308 [ "$(type -t __vte_osc7)" = "function" ] && __vte_osc7 || true
309 }
310
311 function prompt_command {
312 print_title_prompt
313 record_terminal_cwd
314 }
315 PROMPT_COMMAND=prompt_command
316
317 # Display the command about to be executed. This must go at the end of the
318 # bashrc to avoid running the trap on commands in the bashrc
319 function print_title_exec {
320 [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return
321
322 printf '\e]0;%s (%s)\a' \
323 "$(tr -cd '[:graph:] ' <<< "$BASH_COMMAND")" \
324 "$(cat /etc/hostname)"
325
326 if [[ "$TERM" == screen* ]]; then
327 printf '\ek%s\e\\' \
328 "$(sed -n -e 's/sudo //' -e 's/ .*//' -e 1p <<< "$BASH_COMMAND")"
329 fi
330 }
331 trap print_title_exec DEBUG
332
333
334 ###########
335 # The end #
336 ###########