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