1 # shellcheck disable=SC1090 # -- 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 [ -r ~/.config/environment.d/10-email.conf ]; then
21 source ~/.config/environment.d/10-email.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 '
70 # Disable CTRL-s / CTRL-q
74 PROMPT_COMMAND='echo -ne "\\033]0;$(hostname| cut -d. -f1):${PWD/$HOME/~}\\007"'
81 # Revision control status for git, hg, svn
83 function find_up_exists {
86 if [ -e "$d/$1" ]; then
94 [ -r /usr/share/git/completion/git-prompt.sh ] && source /usr/share/git/completion/git-prompt.sh
96 find_up_exists .git || return
97 GIT_PS1_SHOWDIRTYSTATE=1 \
98 GIT_PS1_SHOWUNTRACKEDFILES=1 \
99 __git_ps1 ' (%s)' 2> /dev/null
103 find_up_exists .hg || return
105 status="$(hg status | cut -c1 | sort -u | tr -d ' \n')"
109 function my_svn_ps1 {
110 find_up_exists .svn || return
112 status="$(svn status --ignore-externals 2> /dev/null | cut -c1 | sort -u | tr -d ' \n')"
113 [ -n "$status" ] && echo -n " ($s)"
118 PS1="$PS1"'\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]'
119 PS1="$PS1"'\[\033[01;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\033[00m\]'
121 if ! [[ "$TERM" =~ ^screen ]]; then
124 # matches with shelltitle in .screenrc
125 PS1="$PS1"'\n\[\033k\033\\\]\$> '
129 #################################
130 # Display return codes on error #
131 #################################
133 function print_exit_code {
134 _exit_msg="\\033[01;33mexit code: $?\\033[00m"
135 if [ -z "${BASH_SOURCE[1]}" ]; then
140 trap print_exit_code ERR
155 if emacsclient --version &> /dev/null; then
156 export ALTERNATE_EDITOR='vim'
157 export EDITOR='emacsclient --tty'
159 if [[ "$TERM" == screen* ]]; then
160 alias edit='emacsclient --tty'
162 alias edit='emacsclient --create-frame --no-wait'
170 ##########################
171 # ls aliases and colours #
172 ##########################
175 # shellcheck disable=SC2046
176 eval $(TERM=xterm dircolors 2> /dev/null)
179 export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
182 if ls --color=auto -v &> /dev/null; then
183 alias ls='ls --color=auto -v'
184 elif gls --color=auto -v &> /dev/null; then
185 alias ls='gls --color=auto -v'
186 elif ls -G &> /dev/null; then
199 alias _psresources='ps -wAo pid,user,%cpu,%mem,stat,start,time,args'
200 if [ "$(uname)" = "Linux" ]; then
201 alias pscpu='_psresources --sort -%cpu|less -S'
202 alias psmem='_psresources --sort -%mem|less -S'
203 alias pstree='ps --forest -weo pid,user:16,args --sort start_time|less -S'
204 alias pstime='ps -wAo pid,user,lstart,args --sort start_time|less -S'
206 alias pscpu='_psresources -r|less -S'
207 alias psmem='_psresources -m|less -S'
208 alias pstime='ps -wAo pid,user,lstart,args|less -S'
215 alias f='find . -iname'
216 if echo x | grep -q --color=auto x &> /dev/null; then
217 alias grep='grep --color=auto'
220 alias scp='scp -o ControlPath=none'
222 alias watch='watch -n1'
223 alias sudo='sudo ' # ability to use aliases with sudo
224 alias sudosu='sudo su -l -s /bin/bash'
225 alias py='PYTHONSTARTUP=~/.pythonrc.py python'
226 alias webshare='python3 -mhttp.server'
228 if ! command -v pbcopy &> /dev/null; then
229 alias pbcopy='xsel --clipboard --input'
230 alias pbcopym='xsel --input'
231 alias pbpaste='xsel --clipboard --output'
232 alias pbpastem='xsel --output'
235 # man with coloured headings and a terminal title
237 echo -ne "\\033]0;man $*\\007"
240 LESS_TERMCAP_md=$'\E[01;38;5;74m' \
241 LESS_TERMCAP_me=$'\E[0m' \
242 LESS_TERMCAP_us=$'\E[04;38;5;146m' \
243 LESS_TERMCAP_ue=$'\E[0m' \
244 man --encoding ascii "$@"
247 # Creates the directory if it doesn't exist, and changes into it
249 # shellcheck disable=SC2164
250 mkdir -p "$1" && cd "$1"
253 # Sets the nice and ionice priorities for the current shell to the lowest values
259 # SSH to an unknown host and print the new known_hosts entry
261 local new_known_hosts_file
262 new_known_hosts_file="$(mktemp)"
263 ssh -o UserKnownHostsFile="$new_known_hosts_file" "$@" echo 'Connection ok'
264 cat "$new_known_hosts_file"
265 rm -f "$new_known_hosts_file"
268 # SSH without verifying host key
269 function ssh_unsafe {
270 ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"