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