]> code.delx.au - dotfiles/blob - .bashrc
bash: pre-exec xterm titlebar
[dotfiles] / .bashrc
1 # shellcheck disable=SC1090 disable=SC1091 # -- Can't follow non-constant source
2
3 ###########################
4 # Settings for all shells #
5 ###########################
6
7 # Set umask, depending on the user ID
8 if [ "$(id -un)" = "root" ]; then
9 umask 0022
10 else
11 umask 0007
12 fi
13
14 # Add ~/bin and subdirs to PATH if needed
15 while read -r p; do
16 echo "$PATH" | tr ':' '\n' | grep -qxF "$p" || PATH="${p}:$PATH"
17 done < <(find -L ~/bin -maxdepth 1 -type d 2> /dev/null)
18
19 # Set EMAIL from the freedesktop environment.d
20 if ls ~/.config/environment.d/*.conf &> /dev/null; then
21 eval "$(awk -F= '{print $0 ";export " $1}' ~/.config/environment.d/*.conf)"
22 fi
23
24 # Pick up SSH agent socket
25 if [ -z "$SSH_AUTH_SOCK" ]; then
26 if [ -r ~/.ssh-agent.env ]; then
27 source ~/.ssh-agent.env
28 else
29 SSH_AUTH_SOCK="/run/user/$(id -u)/keyring/ssh"
30 export SSH_AUTH_SOCK
31 fi
32 fi
33
34
35 ###########################
36 # Interactive shells only #
37 ###########################
38
39 if [ -z "${PS1}" ]; then
40 return
41 fi
42
43
44 ################
45 # bash options #
46 ################
47
48 # Bash should check the terminal size after every command terminates
49 shopt -s checkwinsize
50
51 # Don't attempt to tab-complete an empty line
52 shopt -s no_empty_cmd_completion
53
54 # Prevent overwriting existing files on stdout redirection
55 set -o noclobber
56
57 # Better history
58 shopt -s histappend
59 shopt -s cmdhist
60 export HISTCONTROL='erasedups:ignoredups:ignorespace'
61 export HISTSIZE='100000'
62 export HISTTIMEFORMAT='%F %T '
63
64 [ -r /etc/bash_completion ] && source /etc/bash_completion
65
66 ##################
67 # Terminal setup #
68 ##################
69
70 # Disable CTRL-s / CTRL-q
71 stty -ixon
72
73 # Use dark background colors in apps like vim
74 export COLORFGBG='15;0'
75
76 #############
77 # Fancy PS1 #
78 #############
79
80 # Revision control status for git, hg, svn
81
82 function find_up_exists {
83 local d="$PWD"
84 while [ -n "$d" ]; do
85 if [ -e "$d/$1" ]; then
86 return 0
87 fi
88 d="${d%/*}"
89 done
90 return 1
91 }
92
93 for p in /usr/lib/git-core/git-sh-prompt \
94 /usr/share/git/completion/git-prompt.sh \
95 /usr/local/share/git-core/contrib/completion/git-prompt.sh
96 do
97 [ -r "$p" ] && source "$p" && break
98 done
99
100 function my_git_ps1 {
101 find_up_exists .git || return
102 GIT_PS1_SHOWDIRTYSTATE=1 \
103 GIT_PS1_SHOWUNTRACKEDFILES=1 \
104 __git_ps1 ' (%s)' 2> /dev/null
105 }
106
107 function my_hg_ps1 {
108 find_up_exists .hg || return
109 local status
110 status="$(hg status | cut -c1 | sort -u | tr -d ' \n')"
111 echo -n " ($status)"
112 }
113
114 function my_svn_ps1 {
115 find_up_exists .svn || return
116 local status
117 status="$(svn status --ignore-externals 2> /dev/null | cut -c1 | sort -u | tr -d ' \n')"
118 [ -n "$status" ] && echo -n " ($status)"
119 }
120
121 # Two line prompt
122 PS1=''
123 PS1="$PS1"'\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]'
124 PS1="$PS1"'\[\033[01;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\033[00m\]'
125
126 if ! [[ "$TERM" =~ ^screen ]]; then
127 PS1="$PS1"'\n\$ '
128 else
129 # matches with shelltitle in .screenrc
130 PS1="$PS1"'\n\[\033k\033\\\]\$> '
131 fi
132
133
134 #################################
135 # Display return codes on error #
136 #################################
137
138 function print_exit_code {
139 _exit_msg="\\033[01;33mexit code: $?\\033[00m"
140 if [ -z "${BASH_SOURCE[1]}" ]; then
141 echo -e "$_exit_msg"
142 fi
143 unset _exit_msg
144 }
145 trap print_exit_code ERR
146
147
148 ###############
149 # Pager setup #
150 ###############
151
152 export PAGER='less'
153 export LESS='RS'
154
155
156 ################
157 # Editor setup #
158 ################
159
160 if emacsclient --version &> /dev/null; then
161 export ALTERNATE_EDITOR='vim'
162 export EDITOR='emacsclient --tty'
163
164 if [[ "$TERM" == screen* ]]; then
165 alias edit='emacsclient --tty'
166 else
167 alias edit='emacsclient --create-frame --no-wait'
168 fi
169 else
170 export EDITOR='vim'
171 alias edit='vim'
172 fi
173
174
175 ##########################
176 # ls aliases and colours #
177 ##########################
178
179 # GNU ls colours
180 eval "$(TERM=xterm dircolors 2> /dev/null)"
181
182 # BSD ls colours
183 export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
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 # ps aliases #
201 ##############
202
203 alias _psresources='ps -wAo pid,user,%cpu,%mem,stat,start,time,args'
204 if [ "$(uname)" = "Linux" ]; then
205 alias pscpu='_psresources --sort -%cpu|less -S'
206 alias psmem='_psresources --sort -%mem|less -S'
207 alias pstree='ps --forest -weo pid,user:16,args --sort start_time|less -S'
208 alias pstime='ps -wAo pid,user,lstart,args --sort start_time|less -S'
209 else
210 alias pscpu='_psresources -r|less -S'
211 alias psmem='_psresources -m|less -S'
212 alias pstime='ps -wAo pid,user,lstart,args|less -S'
213 fi
214
215 #################
216 # Other aliases #
217 #################
218
219 alias f='find . -iname'
220 if echo x | grep -q --color=auto x &> /dev/null; then
221 alias grep='grep --color=auto'
222 fi
223 alias rg='rg -p'
224 alias scp='scp -o ControlPath=none'
225 alias bc='bc -ql'
226 alias watch='watch -n1'
227 alias sudo='sudo ' # ability to use aliases with sudo
228 alias sudosu='sudo su -l -s /bin/bash'
229 alias python='PYTHONSTARTUP=~/.pythonrc.py python3'
230 alias 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 to an unknown host and print the new known_hosts entry
263 function ssh_new {
264 local new_known_hosts_file
265 new_known_hosts_file="$(mktemp)"
266 ssh -o UserKnownHostsFile="$new_known_hosts_file" "$@" echo 'Connection ok'
267 cat "$new_known_hosts_file"
268 rm -f "$new_known_hosts_file"
269 }
270
271 # SSH without verifying host key
272 function ssh_unsafe {
273 ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"
274 }
275
276
277 #########################
278 # Optional local config #
279 #########################
280
281 [ -r ~/.bashrc_local ] && source ~/.bashrc_local
282
283
284 ##################
285 # xterm titlebar #
286 ##################
287
288 # When at a prompt display `hostname:workingdir`
289 PROMPT_COMMAND='echo -ne "\\033]0;$(hostname|cut -d. -f1):${PWD/$HOME/\~}\\007"'
290
291 # Display the command about to be executed
292 # This must go at the end of bashrc to avoid running the trap on commands in the bashrc
293 trap 'echo -ne "\\033]0;$BASH_COMMAND\007"' DEBUG
294
295
296 ###########
297 # The end #
298 ###########