1 # shellcheck disable=SC1090 disable=SC1091 # -- Can't follow non-constant source
2 # shellcheck disable=SC1003 # -- Don't complain about '\e\\'
4 ###########################
5 # Settings for all shells #
6 ###########################
8 # Set umask, depending on the user ID
9 if [ "$(id -un)" = "root" ]; then
15 # Add ~/bin and subdirs to PATH if needed
17 echo "$PATH" | tr ':' '\n' | grep -qxF "$p" || PATH="${p}:$PATH"
18 done < <(find -L ~/bin -maxdepth 1 -type d 2> /dev/null)
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)"
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
30 SSH_AUTH_SOCK="/run/user/$(id -u)/keyring/ssh"
36 ###########################
37 # Interactive shells only #
38 ###########################
40 if [ -z "${PS1}" ]; then
49 # Bash should check the terminal size after every command terminates
52 # Don't attempt to tab-complete an empty line
53 shopt -s no_empty_cmd_completion
55 # Prevent overwriting existing files on stdout redirection
61 export HISTCONTROL='erasedups:ignoredups:ignorespace'
62 export HISTSIZE='100000'
63 export HISTTIMEFORMAT='%F %T '
65 [ -r /etc/bash_completion ] && source /etc/bash_completion
80 # Disable CTRL-s / CTRL-q
83 # Use dark background colors in apps like vim
84 export COLORFGBG='15;0'
90 # Revision control status for git, hg, svn
92 function find_up_exists {
95 if [ -e "$d/$1" ]; then
103 for p in /usr/lib/git-core/git-sh-prompt \
104 /usr/share/git/completion/git-prompt.sh \
105 /usr/share/git-core/contrib/completion/git-prompt.sh \
106 /usr/local/share/git-core/contrib/completion/git-prompt.sh
108 [ -r "$p" ] && source "$p" && break
111 function my_git_ps1 {
112 find_up_exists .git || return
113 GIT_PS1_SHOWDIRTYSTATE=1 \
114 GIT_PS1_SHOWUNTRACKEDFILES=1 \
115 __git_ps1 ' (%s)' 2> /dev/null
119 find_up_exists .hg || return
121 status="$(hg status | cut -c1 | sort -u | tr -d ' \n')"
125 function my_svn_ps1 {
126 find_up_exists .svn || return
128 status="$(svn status --ignore-externals 2> /dev/null | cut -c1 | sort -u | tr -d ' \n')"
129 [ -n "$status" ] && echo -n " ($status)"
134 PS1="$PS1"'\[\e[01;31m\]\u@\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[00m\]'
135 PS1="$PS1"'\[\e[01;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\e[00m\]'
139 #################################
140 # Display return codes on error #
141 #################################
143 function print_exit_code {
145 if [ -z "${BASH_SOURCE[1]}" ]; then
146 printf '\e[01;33mexit code: %s\e[00m\n' \
150 trap print_exit_code ERR
158 # This prevents long/ugly command lines from showing up in xterm titles
160 eval "function $1 { $2 \$@; }"
164 ##########################
165 # ls aliases and colours #
166 ##########################
169 eval "$(TERM=xterm dircolors 2> /dev/null)"
172 export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
175 if ls --color=auto -v &> /dev/null; then
176 alias ls='ls --color=auto -v'
177 elif gls --color=auto -v &> /dev/null; then
178 alias ls='gls --color=auto -v'
179 elif ls -G &> /dev/null; then
192 alias _psresources='ps -wAo pid,user,%cpu,%mem,stat,start,time,args'
193 if [ "$(uname)" = "Linux" ]; then
194 aliasf pscpu '_psresources --sort -%cpu|less -S'
195 aliasf psmem '_psresources --sort -%mem|less -S'
196 aliasf pstree 'ps --forest -weo pid,user:16,args --sort start_time|less -S'
197 aliasf pstime 'ps -wAo pid,user,lstart,args --sort start_time|less -S'
199 aliasf pscpu '_psresources -r|less -S'
200 aliasf psmem '_psresources -m|less -S'
201 aliasf pstime 'ps -wAo pid,user,lstart,args|less -S'
208 alias f='find . -iname'
209 if echo x | grep -q --color=auto x &> /dev/null; then
210 alias grep='grep --color=auto'
214 alias watch='watch -n1'
215 alias sudo='sudo ' # ability to use aliases with sudo
216 aliasf sudosu 'sudo su -l -s /bin/bash'
217 aliasf python 'PYTHONSTARTUP=~/.pythonrc.py python3'
218 aliasf webshare 'python3 -mhttp.server'
220 if ! command -v pbcopy &> /dev/null; then
221 alias pbcopy='xsel --clipboard --input'
222 alias pbcopym='xsel --input'
223 alias pbpaste='xsel --clipboard --output'
224 alias pbpastem='xsel --output'
227 # man with coloured headings and a terminal title
230 LESS_TERMCAP_md=$'\E[01;38;5;74m' \
231 LESS_TERMCAP_me=$'\E[0m' \
232 LESS_TERMCAP_us=$'\E[04;38;5;146m' \
233 LESS_TERMCAP_ue=$'\E[0m' \
238 # Creates the directory if it doesn't exist, and changes into it
240 # shellcheck disable=SC2164
241 mkdir -p "$1" && cd "$1"
244 # Sets the nice and ionice priorities for the current shell to the lowest values
250 # SSH to an unknown host and print the new known_hosts entry
252 local new_known_hosts_file
253 new_known_hosts_file="$(mktemp)"
254 ssh -o UserKnownHostsFile="$new_known_hosts_file" "$@" echo 'Connection ok'
255 cat "$new_known_hosts_file"
256 rm -f "$new_known_hosts_file"
259 # SSH without verifying host key
260 function ssh_unsafe {
261 ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"
269 if emacsclient --version &> /dev/null; then
270 export ALTERNATE_EDITOR='vim'
271 export EDITOR='emacsclient --tty'
273 if [[ "$TERM" == screen* ]]; then
274 aliasf edit 'emacsclient --tty'
276 aliasf edit 'emacsclient --create-frame --no-wait'
284 #########################
285 # Optional local config #
286 #########################
288 [ -r ~/.bashrc_local ] && source ~/.bashrc_local
295 # When at a prompt display `workingdir (hostname)`
296 function print_title_prompt {
297 printf '\e]0;bash:%s (%s)\a' \
301 if [[ "$TERM" == screen* ]]; then
305 PROMPT_COMMAND=print_title_prompt
307 # Display the command about to be executed. This must go at the end of the
308 # bashrc to avoid running the trap on commands in the bashrc
309 function print_title_exec {
310 [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return
312 printf '\e]0;%s (%s)\a' \
313 "$(tr -cd '[:graph:] ' <<< "$BASH_COMMAND")" \
316 if [[ "$TERM" == screen* ]]; then
318 "$(sed -n -e 's/sudo //' -e 's/ .*//' -e 1p <<< "$BASH_COMMAND")"
321 trap print_title_exec DEBUG