]> code.delx.au - dotfiles/blobdiff - .bashrc
bash: pre-exec xterm titlebar
[dotfiles] / .bashrc
diff --git a/.bashrc b/.bashrc
index 79f465cfb07a0b4675c7362a7345e83e4efe4206..8a8408861533eec7bc4aecec7fd241679aa5ca91 100644 (file)
--- a/.bashrc
+++ b/.bashrc
@@ -1,8 +1,298 @@
-# We run the environment settings for all shells to ensure it's always set up
-source "${HOME}/.bash/environment"
+# shellcheck disable=SC1090 disable=SC1091 # -- Can't follow non-constant source
 
-# An interactive shell starting bashrc is not a login shell, just run
-# interactive setup
-if [ -n "${PS1}" ]; then
-    source "${HOME}/.bash/interactive"
+###########################
+# Settings for all shells #
+###########################
+
+# Set umask, depending on the user ID
+if [ "$(id -un)" = "root" ]; then
+    umask 0022
+else
+    umask 0007
+fi
+
+# Add ~/bin and subdirs to PATH if needed
+while read -r p; do
+    echo "$PATH" | tr ':' '\n' | grep -qxF "$p" || PATH="${p}:$PATH"
+done < <(find -L ~/bin -maxdepth 1 -type d 2> /dev/null)
+
+# Set EMAIL from the freedesktop environment.d
+if ls ~/.config/environment.d/*.conf &> /dev/null; then
+    eval "$(awk -F= '{print $0 ";export " $1}' ~/.config/environment.d/*.conf)"
+fi
+
+# Pick up SSH agent socket
+if [ -z "$SSH_AUTH_SOCK" ]; then
+    if [ -r ~/.ssh-agent.env ]; then
+        source ~/.ssh-agent.env
+    else
+        SSH_AUTH_SOCK="/run/user/$(id -u)/keyring/ssh"
+        export SSH_AUTH_SOCK
+    fi
+fi
+
+
+###########################
+# Interactive shells only #
+###########################
+
+if [ -z "${PS1}" ]; then
+    return
 fi
+
+
+################
+# bash options #
+################
+
+# Bash should check the terminal size after every command terminates
+shopt -s checkwinsize
+
+# Don't attempt to tab-complete an empty line
+shopt -s no_empty_cmd_completion
+
+# Prevent overwriting existing files on stdout redirection
+set -o noclobber
+
+# Better history
+shopt -s histappend
+shopt -s cmdhist
+export HISTCONTROL='erasedups:ignoredups:ignorespace'
+export HISTSIZE='100000'
+export HISTTIMEFORMAT='%F %T '
+
+[ -r /etc/bash_completion ] && source /etc/bash_completion
+
+##################
+# Terminal setup #
+##################
+
+# Disable CTRL-s / CTRL-q
+stty -ixon
+
+# Use dark background colors in apps like vim
+export COLORFGBG='15;0'
+
+#############
+# Fancy PS1 #
+#############
+
+# Revision control status for git, hg, svn
+
+function find_up_exists {
+    local d="$PWD"
+    while [ -n "$d" ]; do
+        if [ -e "$d/$1" ]; then
+            return 0
+        fi
+        d="${d%/*}"
+    done
+    return 1
+}
+
+for p in /usr/lib/git-core/git-sh-prompt \
+         /usr/share/git/completion/git-prompt.sh \
+         /usr/local/share/git-core/contrib/completion/git-prompt.sh
+do
+    [ -r "$p" ] && source "$p" && break
+done
+
+function my_git_ps1 {
+    find_up_exists .git || return
+    GIT_PS1_SHOWDIRTYSTATE=1 \
+    GIT_PS1_SHOWUNTRACKEDFILES=1 \
+    __git_ps1 ' (%s)' 2> /dev/null
+}
+
+function my_hg_ps1 {
+    find_up_exists .hg || return
+    local status
+    status="$(hg status | cut -c1 | sort -u | tr -d ' \n')"
+    echo -n " ($status)"
+}
+
+function my_svn_ps1 {
+    find_up_exists .svn || return
+    local status
+    status="$(svn status --ignore-externals 2> /dev/null | cut -c1 | sort -u | tr -d ' \n')"
+    [ -n "$status" ] && echo -n " ($status)"
+}
+
+# Two line prompt
+PS1=''
+PS1="$PS1"'\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]'
+PS1="$PS1"'\[\033[01;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\033[00m\]'
+
+if ! [[ "$TERM" =~ ^screen ]]; then
+    PS1="$PS1"'\n\$ '
+else
+    # matches with shelltitle in .screenrc
+    PS1="$PS1"'\n\[\033k\033\\\]\$> '
+fi
+
+
+#################################
+# Display return codes on error #
+#################################
+
+function print_exit_code {
+    _exit_msg="\\033[01;33mexit code: $?\\033[00m"
+    if [ -z "${BASH_SOURCE[1]}" ]; then
+        echo -e "$_exit_msg"
+    fi
+    unset _exit_msg
+}
+trap print_exit_code ERR
+
+
+###############
+# Pager setup #
+###############
+
+export PAGER='less'
+export LESS='RS'
+
+
+################
+# Editor setup #
+################
+
+if emacsclient --version &> /dev/null; then
+    export ALTERNATE_EDITOR='vim'
+    export EDITOR='emacsclient --tty'
+
+    if [[ "$TERM" == screen* ]]; then
+        alias edit='emacsclient --tty'
+    else
+        alias edit='emacsclient --create-frame --no-wait'
+    fi
+else
+    export EDITOR='vim'
+    alias edit='vim'
+fi
+
+
+##########################
+# ls aliases and colours #
+##########################
+
+# GNU ls colours
+eval "$(TERM=xterm dircolors 2> /dev/null)"
+
+# BSD ls colours
+export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
+
+# Lets find the ls
+if ls --color=auto -v &> /dev/null; then
+    alias ls='ls --color=auto -v'
+elif gls --color=auto -v &> /dev/null; then
+    alias ls='gls --color=auto -v'
+elif ls -G &> /dev/null; then
+    alias ls='ls -G'
+else
+    alias ls='ls -F'
+fi
+alias ll='ls -hlF'
+alias la='ls -ha'
+alias  l='ls -halF'
+
+##############
+# ps aliases #
+##############
+
+alias _psresources='ps -wAo pid,user,%cpu,%mem,stat,start,time,args'
+if [ "$(uname)" = "Linux" ]; then
+    alias pscpu='_psresources --sort -%cpu|less -S'
+    alias psmem='_psresources --sort -%mem|less -S'
+    alias pstree='ps --forest -weo pid,user:16,args --sort start_time|less -S'
+    alias pstime='ps -wAo pid,user,lstart,args --sort start_time|less -S'
+else
+    alias pscpu='_psresources -r|less -S'
+    alias psmem='_psresources -m|less -S'
+    alias pstime='ps -wAo pid,user,lstart,args|less -S'
+fi
+
+#################
+# Other aliases #
+#################
+
+alias f='find . -iname'
+if echo x | grep -q --color=auto x &> /dev/null; then
+    alias grep='grep --color=auto'
+fi
+alias rg='rg -p'
+alias scp='scp -o ControlPath=none'
+alias bc='bc -ql'
+alias watch='watch -n1'
+alias sudo='sudo ' # ability to use aliases with sudo
+alias sudosu='sudo su -l -s /bin/bash'
+alias python='PYTHONSTARTUP=~/.pythonrc.py python3'
+alias webshare='python3 -mhttp.server'
+
+if ! command -v pbcopy &> /dev/null; then
+    alias pbcopy='xsel --clipboard --input'
+    alias pbcopym='xsel --input'
+    alias pbpaste='xsel --clipboard --output'
+    alias pbpastem='xsel --output'
+fi
+
+# man with coloured headings and a terminal title
+function man {
+    env \
+        LESS_TERMCAP_md=$'\E[01;38;5;74m' \
+        LESS_TERMCAP_me=$'\E[0m' \
+        LESS_TERMCAP_us=$'\E[04;38;5;146m' \
+        LESS_TERMCAP_ue=$'\E[0m' \
+        LC_CTYPE=C \
+    man "$@"
+}
+
+# Creates the directory if it doesn't exist, and changes into it
+function mcd {
+    # shellcheck disable=SC2164
+    mkdir -p "$1" && cd "$1"
+}
+
+# Sets the nice and ionice priorities for the current shell to the lowest values
+function slowshell {
+    ionice -c 3 -p $$
+    renice -n 19 -p $$
+}
+
+# SSH to an unknown host and print the new known_hosts entry
+function ssh_new {
+    local new_known_hosts_file
+    new_known_hosts_file="$(mktemp)"
+    ssh -o UserKnownHostsFile="$new_known_hosts_file" "$@" echo 'Connection ok'
+    cat "$new_known_hosts_file"
+    rm -f "$new_known_hosts_file"
+}
+
+# SSH without verifying host key
+function ssh_unsafe {
+    ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"
+}
+
+
+#########################
+# Optional local config #
+#########################
+
+[ -r ~/.bashrc_local ] && source ~/.bashrc_local
+
+
+##################
+# xterm titlebar #
+##################
+
+# When at a prompt display `hostname:workingdir`
+PROMPT_COMMAND='echo -ne "\\033]0;$(hostname|cut -d. -f1):${PWD/$HOME/\~}\\007"'
+
+# Display the command about to be executed
+# This must go at the end of bashrc to avoid running the trap on commands in the bashrc
+trap 'echo -ne "\\033]0;$BASH_COMMAND\007"' DEBUG
+
+
+###########
+# The end #
+###########