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