]> code.delx.au - dotfiles/blobdiff - .bashrc
emacs: use console version
[dotfiles] / .bashrc
diff --git a/.bashrc b/.bashrc
index 9e0f0d91790433d26aa198117b71578c4e23c065..2ccae929d0ec187b6f57d0311ad93639bd001771 100644 (file)
--- a/.bashrc
+++ b/.bashrc
-# Run when a random bash is started. This is always run after the login
-# shell. It sets up the shell to have a nice prompt and xterm titlebar, etc.
-# It should NOT set any environment variables like the PATH.
-# Copy this around to all your machines and make machine specific
-# customisations to ~/.bashrc_local
-
-# Run .bash_profile if logging in from ssh
-[ ${RUN_BASH_PROFILE:-0} -eq 1 -a -f ~/.bash_profile ] && source ~/.bash_profile && return
-
-# If not running interactively do nothing
-[ -z "$PS1" ] && return
-
-# Terminals that we want coloured prompts in
-[ -n "$COLORTERM"           ] && PS1_COLOR=1
-[ "$TERM" = "linux"         ] && PS1_COLOR=1 && DARK=1
-[ "$TERM" = "screen"        ] && PS1_COLOR=1
-[ "$TERM" = "xterm"         ] && PS1_COLOR=1
-[ "$TERM" = "xterm-color"   ] && PS1_COLOR=1
-[ "$TERM" = "rxvt"          ] && PS1_COLOR=1
-[ "$TERM" = "rxvt-unicode"  ] && PS1_COLOR=1
-
-# Use colours appropriate to a light on dark terminal, we can autodetect
-# this for some terminals, for others we define it above.
-if [ -n "$COLORFGBG" ]; then
-       FGCOLOR=$(echo $COLORFGBG | cut -d ';' -f 1)
-       BGCOLOR=$(echo $COLORFGBG | cut -d ';' -f 3)
-       if [ "$FGCOLOR" -gt "$BGCOLOR" ]; then
-               DARK=1
-       fi
-       unset FGCOLOR
-       unset BGCOLOR
-elif [ ${DARK:-0} -eq 0 ]; then
-       export COLORFGBG="0;default;15"
-elif [ ${DARK:-0} -eq 1 ]; then
-       export COLORFGBG="15;default;0"
-fi
+# shellcheck disable=SC1090 disable=SC1091 # -- Can't follow non-constant source
+# shellcheck disable=SC1003 # -- Don't complain about '\e\\'
+
+###########################
+# Settings for all shells #
+###########################
 
-# Set the prompt colour, and the colors for the 'ls' command appropriately,
-# depending on the background of the terminal.
-if [ ${PS1_COLOR:-0} -eq 1 ]; then
-       if [ ${DARK:-0} -eq 0 ]; then
-               LS_COLORS="no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35"
-               PS1='\[\033[31m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[00m\]\$ '
-       else
-               LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:'
-               PS1='\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
-       fi
-
-       export LS_COLORS
-       unset PS1_COLOR
-       unset DARK
+# Set umask, depending on the user ID
+if [ "$(id -un)" = "root" ]; then
+    umask 0022
 else
-       PS1='\u@\h:\w\$ '
+    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 -depth -maxdepth 1 -type d 2> /dev/null)
+
+# Set environment variables, eg EMAIL, LANG
+eval "$(cat 2> /dev/null /etc/default/locale /etc/locale.conf ~/.config/environment.d/*.conf | awk -F= '/^[^# ]/ {print $0 ";export " $1}')"
+
+# 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
 
-function DARKTERM {
-       unset COLORFGBG
-       DARK=1 && source ~/.bashrc
+
+###########################
+# Interactive shells only #
+###########################
+
+if [ -z "${PS1}" ]; then
+    return
+fi
+
+
+################
+# Source files #
+################
+
+# Debian/Ubuntu don't source this from /etc/bash.bashrc
+[ -z "$BASH_COMPLETION_VERSINFO" ] && [ -r /usr/share/bash-completion/bash_completion ] && source /usr/share/bash-completion/bash_completion
+
+# This isn't sourced for interactive shells on Debian/Ubuntu/Arch
+# https://bugzilla.gnome.org/show_bug.cgi?id=697475
+[ -r /etc/profile.d/vte.sh ] && source /etc/profile.d/vte.sh
+
+
+################
+# 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 '
+
+
+###############
+# Pager setup #
+###############
+
+export PAGER='less'
+export LESS='RS'
+
+
+##################
+# 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/share/git-core/contrib/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"'\[\e[01;31m\]\u@\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[00m\]'
+PS1="$PS1"'\[\e[01;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\e[00m\]'
+PS1="$PS1"'\n\$ '
+
+
+#################################
+# Display return codes on error #
+#################################
+
+function print_exit_code {
+    local exit_code="$?"
+    if [ -z "${BASH_SOURCE[1]}" ]; then
+        printf '\e[01;33mexit code: %s\e[00m\n' \
+            "$exit_code"
+    fi
+}
+trap print_exit_code ERR
+
+
+
+####################
+# Function aliases #
+####################
+
+# This prevents long/ugly command lines from showing up in xterm titles
+function aliasf {
+    eval "function $1 { $2 \$@; }"
 }
 
-# xterm titlebar displays current command
-case "$TERM" in
-xterm*|rxvt*)
-       HOSTNAME="$(hostname | cut -d '.' -f 1)"
-       CWD_WITHHOME='"$(echo "$PWD" | sed "s|^$HOME|~|")"'
-       PROMPT_COMMAND="echo -ne \"\033]0;${HOSTNAME}: ${CWD_WITHHOME}\007\""
-       ;;
-*)
-       ;;
-esac
-
-# Useful aliases
-alias ls='ls --color=auto'
+
+##########################
+# ls aliases and colours #
+##########################
+
+if command -v dircolors &> /dev/null; then
+    # GNU ls colours
+    eval "$(dircolors)"
+    LS_COLORS+=':ow=30;42'
+else
+    # BSD ls colours
+    export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
+fi
+
+# 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'
-alias rm='rm -i'
-alias less='less -R'
-alias screen='TERM=screen screen'
 
-shopt -s checkwinsize
 
-# Local customisations
+##############
+# ps aliases #
+##############
+
+alias _psresources='ps -wAo pid,user,%cpu,%mem,stat,start,time,args'
+if [ "$(uname)" = "Linux" ]; then
+    aliasf pscpu '_psresources --sort -%cpu|less -S'
+    aliasf psmem '_psresources --sort -%mem|less -S'
+    aliasf pstree 'ps --forest -weo pid,user:16,args --sort start_time|less -S'
+    aliasf pstime 'ps -wAo pid,user,lstart,args --sort start_time|less -S'
+else
+    aliasf pscpu '_psresources -r|less -S'
+    aliasf psmem '_psresources -m|less -S'
+    aliasf 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 bc='bc -ql'
+alias watch='watch -n1'
+alias sudo='sudo ' # ability to use aliases with sudo
+aliasf sudosu 'sudo su -l -s /bin/bash'
+aliasf python 'PYTHONSTARTUP=~/.pythonrc.py python3'
+aliasf 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 without verifying host key
+function ssh_unsafe {
+    ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"
+}
+
+
+##################
+# Editor aliases #
+##################
+
+if emacsclient --version &> /dev/null; then
+    export ALTERNATE_EDITOR='vim'
+    export EDITOR='emacsclient --tty'
+else
+    export EDITOR='vim'
+fi
+aliasf edit "$EDITOR"
+
+
+#########################
+# Optional local config #
+#########################
+
 [ -r ~/.bashrc_local ] && source ~/.bashrc_local
 
+
+########################
+# Terminal integration #
+########################
+
+# When at a prompt display `workingdir (hostname)`
+function print_title_prompt {
+    printf '\e]0;bash:%s (%s)\a' \
+        "${PWD/$HOME/\~}" \
+        "$(hostname -s)"
+
+    if [[ "$TERM" == screen* ]]; then
+        printf '\ekbash\e\\'
+    fi
+}
+
+# Preserve working directory when opening new terminals
+# This depends on /etc/profile/vte.sh
+function record_terminal_cwd {
+    [ "$(type -t __vte_osc7)" = "function" ] && __vte_osc7 || true
+}
+
+function prompt_command {
+    print_title_prompt
+    record_terminal_cwd
+}
+PROMPT_COMMAND=prompt_command
+
+# Display the command about to be executed. This must go at the end of the
+# bashrc to avoid running the trap on commands in the bashrc
+function print_title_exec {
+    [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return
+
+    printf '\e]0;%s (%s)\a' \
+        "$(tr -cd '[:graph:] ' <<< "$BASH_COMMAND")" \
+        "$(hostname -s)"
+
+    if [[ "$TERM" == screen* ]]; then
+        printf '\ek%s\e\\' \
+            "$(sed -n -e 's/sudo //' -e 's/ .*//' -e 1p <<< "$BASH_COMMAND")"
+    fi
+}
+trap print_title_exec DEBUG
+
+
+###########
+# The end #
+###########