1 # shellcheck disable=SC1090 disable=SC1091 # -- Can't follow non-constant source
3 ###########################
4 # Settings for all shells #
5 ###########################
7 # Set umask, depending on the user ID
8 if [ "$(id -un)" = "root" ]; then
14 # Add ~/bin and subdirs to PATH if needed
16 echo "$PATH" | tr ':' '\n' | grep -qxF "$p" || PATH="${p}:$PATH"
17 done < <(find -L ~/bin -maxdepth 1 -type d 2> /dev/null)
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)"
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
29 SSH_AUTH_SOCK="/run/user/$(id -u)/keyring/ssh"
35 ###########################
36 # Interactive shells only #
37 ###########################
39 if [ -z "${PS1}" ]; then
48 # Bash should check the terminal size after every command terminates
51 # Don't attempt to tab-complete an empty line
52 shopt -s no_empty_cmd_completion
54 # Prevent overwriting existing files on stdout redirection
60 export HISTCONTROL='erasedups:ignoredups:ignorespace'
61 export HISTSIZE='100000'
62 export HISTTIMEFORMAT='%F %T '
64 [ -r /etc/bash_completion ] && source /etc/bash_completion
70 # Disable CTRL-s / CTRL-q
74 PROMPT_COMMAND='echo -ne "\\033]0;$(hostname|cut -d. -f1):${PWD/$HOME/~}\\007"'
76 # Use dark background colors in apps like vim
77 export COLORFGBG='15;0'
83 # Revision control status for git, hg, svn
85 function find_up_exists {
88 if [ -e "$d/$1" ]; then
96 [ -r /usr/lib/git-core/git-sh-prompt ] && source /usr/lib/git-core/git-sh-prompt
97 [ -r /usr/share/git/completion/git-prompt.sh ] && source /usr/share/git/completion/git-prompt.sh
99 find_up_exists .git || return
100 GIT_PS1_SHOWDIRTYSTATE=1 \
101 GIT_PS1_SHOWUNTRACKEDFILES=1 \
102 __git_ps1 ' (%s)' 2> /dev/null
106 find_up_exists .hg || return
108 status="$(hg status | cut -c1 | sort -u | tr -d ' \n')"
112 function my_svn_ps1 {
113 find_up_exists .svn || return
115 status="$(svn status --ignore-externals 2> /dev/null | cut -c1 | sort -u | tr -d ' \n')"
116 [ -n "$status" ] && echo -n " ($s)"
121 PS1="$PS1"'\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]'
122 PS1="$PS1"'\[\033[01;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\033[00m\]'
124 if ! [[ "$TERM" =~ ^screen ]]; then
127 # matches with shelltitle in .screenrc
128 PS1="$PS1"'\n\[\033k\033\\\]\$> '
132 #################################
133 # Display return codes on error #
134 #################################
136 function print_exit_code {
137 _exit_msg="\\033[01;33mexit code: $?\\033[00m"
138 if [ -z "${BASH_SOURCE[1]}" ]; then
143 trap print_exit_code ERR
158 if emacsclient --version &> /dev/null; then
159 export ALTERNATE_EDITOR='vim'
160 export EDITOR='emacsclient --tty'
162 if [[ "$TERM" == screen* ]]; then
163 alias edit='emacsclient --tty'
165 alias edit='emacsclient --create-frame --no-wait'
173 ##########################
174 # ls aliases and colours #
175 ##########################
178 eval "$(TERM=xterm dircolors 2> /dev/null)"
181 export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
184 if ls --color=auto -v &> /dev/null; then
185 alias ls='ls --color=auto -v'
186 elif gls --color=auto -v &> /dev/null; then
187 alias ls='gls --color=auto -v'
188 elif ls -G &> /dev/null; then
201 alias _psresources='ps -wAo pid,user,%cpu,%mem,stat,start,time,args'
202 if [ "$(uname)" = "Linux" ]; then
203 alias pscpu='_psresources --sort -%cpu|less -S'
204 alias psmem='_psresources --sort -%mem|less -S'
205 alias pstree='ps --forest -weo pid,user:16,args --sort start_time|less -S'
206 alias pstime='ps -wAo pid,user,lstart,args --sort start_time|less -S'
208 alias pscpu='_psresources -r|less -S'
209 alias psmem='_psresources -m|less -S'
210 alias pstime='ps -wAo pid,user,lstart,args|less -S'
217 alias f='find . -iname'
218 if echo x | grep -q --color=auto x &> /dev/null; then
219 alias grep='grep --color=auto'
222 alias scp='scp -o ControlPath=none'
224 alias watch='watch -n1'
225 alias sudo='sudo ' # ability to use aliases with sudo
226 alias sudosu='sudo su -l -s /bin/bash'
227 alias python='PYTHONSTARTUP=~/.pythonrc.py python3'
228 alias webshare='python3 -mhttp.server'
230 if ! command -v pbcopy &> /dev/null; then
231 alias pbcopy='xsel --clipboard --input'
232 alias pbcopym='xsel --input'
233 alias pbpaste='xsel --clipboard --output'
234 alias pbpastem='xsel --output'
237 # man with coloured headings and a terminal title
239 echo -ne "\\033]0;man $*\\007"
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' \
250 # Creates the directory if it doesn't exist, and changes into it
252 # shellcheck disable=SC2164
253 mkdir -p "$1" && cd "$1"
256 # Sets the nice and ionice priorities for the current shell to the lowest values
262 # SSH to an unknown host and print the new known_hosts entry
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"
271 # SSH without verifying host key
272 function ssh_unsafe {
273 ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"
280 [ -r ~/.bashrc_local ] && source ~/.bashrc_local