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 environment variables, eg EMAIL, LANG
21 eval "$(cat 2> /dev/null /etc/default/locale /etc/locale.conf ~/.config/environment.d/*.conf | awk -F= '/^[^# ]/ {print $0 ";export " $1}')"
23 # Pick up SSH agent socket
24 if [ -z "$SSH_AUTH_SOCK" ]; then
25 if [ -r ~/.ssh-agent.env ]; then
26 source ~/.ssh-agent.env
28 SSH_AUTH_SOCK="/run/user/$(id -u)/keyring/ssh"
34 ###########################
35 # Interactive shells only #
36 ###########################
38 if [ -z "${PS1}" ]; then
47 # Debian/Ubuntu don't source this from /etc/bash.bashrc
48 [ -z "$BASH_COMPLETION_VERSINFO" ] && [ -r /usr/share/bash-completion/bash_completion ] && source /usr/share/bash-completion/bash_completion
50 # This isn't sourced for interactive shells on Debian/Ubuntu/Arch
51 # https://bugzilla.gnome.org/show_bug.cgi?id=697475
52 [ -r /etc/profile.d/vte.sh ] && source /etc/profile.d/vte.sh
59 # Bash should check the terminal size after every command terminates
62 # Don't attempt to tab-complete an empty line
63 shopt -s no_empty_cmd_completion
65 # Prevent overwriting existing files on stdout redirection
71 export HISTCONTROL='erasedups:ignoredups:ignorespace'
72 export HISTSIZE='100000'
73 export HISTTIMEFORMAT='%F %T '
88 # Disable CTRL-s / CTRL-q
91 # Use dark background colors in apps like vim
92 export COLORFGBG='15;0'
98 # Revision control status for git, hg, svn
100 function find_up_exists {
102 while [ -n "$d" ]; do
103 if [ -e "$d/$1" ]; then
111 for p in /usr/lib/git-core/git-sh-prompt \
112 /usr/share/git/completion/git-prompt.sh \
113 /usr/share/git-core/contrib/completion/git-prompt.sh \
114 /usr/local/share/git-core/contrib/completion/git-prompt.sh
116 [ -r "$p" ] && source "$p" && break
119 function my_git_ps1 {
120 find_up_exists .git || return
121 GIT_PS1_SHOWDIRTYSTATE=1 \
122 GIT_PS1_SHOWUNTRACKEDFILES=1 \
123 __git_ps1 ' (%s)' 2> /dev/null
127 find_up_exists .hg || return
129 status="$(hg status | cut -c1 | sort -u | tr -d ' \n')"
133 function my_svn_ps1 {
134 find_up_exists .svn || return
136 status="$(svn status --ignore-externals 2> /dev/null | cut -c1 | sort -u | tr -d ' \n')"
137 [ -n "$status" ] && echo -n " ($status)"
142 PS1="$PS1"'\[\e[01;31m\]\u@\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[00m\]'
143 PS1="$PS1"'\[\e[01;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\e[00m\]'
147 #################################
148 # Display return codes on error #
149 #################################
151 function print_exit_code {
153 if [ -z "${BASH_SOURCE[1]}" ]; then
154 printf '\e[01;33mexit code: %s\e[00m\n' \
158 trap print_exit_code ERR
166 # This prevents long/ugly command lines from showing up in xterm titles
168 eval "function $1 { $2 \$@; }"
172 ##########################
173 # ls aliases and colours #
174 ##########################
176 if command -v dircolors &> /dev/null; then
179 LS_COLORS+=':ow=30;42'
182 export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
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
204 alias _psresources='ps -wAo pid,user,%cpu,%mem,stat,start,time,args'
205 if [ "$(uname)" = "Linux" ]; then
206 aliasf pscpu '_psresources --sort -%cpu|less -S'
207 aliasf psmem '_psresources --sort -%mem|less -S'
208 aliasf pstree 'ps --forest -weo pid,user:16,args --sort start_time|less -S'
209 aliasf pstime 'ps -wAo pid,user,lstart,args --sort start_time|less -S'
211 aliasf pscpu '_psresources -r|less -S'
212 aliasf psmem '_psresources -m|less -S'
213 aliasf pstime 'ps -wAo pid,user,lstart,args|less -S'
220 alias f='find . -iname'
221 if echo x | grep -q --color=auto x &> /dev/null; then
222 alias grep='grep --color=auto'
226 alias watch='watch -n1'
227 alias sudo='sudo ' # ability to use aliases with sudo
228 aliasf sudosu 'sudo su -l -s /bin/bash'
229 aliasf python 'PYTHONSTARTUP=~/.pythonrc.py python3'
230 aliasf webshare 'python3 -mhttp.server'
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'
239 # man with coloured headings and a terminal title
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 without verifying host key
263 function ssh_unsafe {
264 ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"
272 if emacsclient --version &> /dev/null; then
273 export ALTERNATE_EDITOR='vim'
274 export EDITOR='emacsclient --tty'
276 if [[ "$TERM" == screen* ]]; then
277 aliasf edit 'emacsclient --tty'
279 aliasf edit 'emacsclient --create-frame --no-wait'
287 #########################
288 # Optional local config #
289 #########################
291 [ -r ~/.bashrc_local ] && source ~/.bashrc_local
294 ########################
295 # Terminal integration #
296 ########################
298 # When at a prompt display `workingdir (hostname)`
299 function print_title_prompt {
300 printf '\e]0;bash:%s (%s)\a' \
304 if [[ "$TERM" == screen* ]]; then
309 # Preserve working directory when opening new terminals
310 # This depends on /etc/profile/vte.sh
311 function record_terminal_cwd {
312 [ "$(type -t __vte_osc7)" = "function" ] && __vte_osc7 || true
315 function prompt_command {
319 PROMPT_COMMAND=prompt_command
321 # Display the command about to be executed. This must go at the end of the
322 # bashrc to avoid running the trap on commands in the bashrc
323 function print_title_exec {
324 [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return
326 printf '\e]0;%s (%s)\a' \
327 "$(tr -cd '[:graph:] ' <<< "$BASH_COMMAND")" \
330 if [[ "$TERM" == screen* ]]; then
332 "$(sed -n -e 's/sudo //' -e 's/ .*//' -e 1p <<< "$BASH_COMMAND")"
335 trap print_title_exec DEBUG