]> code.delx.au - dotfiles/blobdiff - .bashrc
emacs: use console version
[dotfiles] / .bashrc
diff --git a/.bashrc b/.bashrc
index 8a8408861533eec7bc4aecec7fd241679aa5ca91..2ccae929d0ec187b6f57d0311ad93639bd001771 100644 (file)
--- a/.bashrc
+++ b/.bashrc
@@ -1,4 +1,5 @@
 # shellcheck disable=SC1090 disable=SC1091 # -- Can't follow non-constant source
+# shellcheck disable=SC1003 # -- Don't complain about '\e\\'
 
 ###########################
 # Settings for all shells #
@@ -14,12 +15,10 @@ 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)
+done < <(find -L ~/bin -depth -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
+# 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
@@ -41,6 +40,18 @@ if [ -z "${PS1}" ]; then
 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 #
 ################
@@ -61,7 +72,14 @@ export HISTCONTROL='erasedups:ignoredups:ignorespace'
 export HISTSIZE='100000'
 export HISTTIMEFORMAT='%F %T '
 
-[ -r /etc/bash_completion ] && source /etc/bash_completion
+
+###############
+# Pager setup #
+###############
+
+export PAGER='less'
+export LESS='RS'
+
 
 ##################
 # Terminal setup #
@@ -92,6 +110,7 @@ function find_up_exists {
 
 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
@@ -120,15 +139,9 @@ function my_svn_ps1 {
 
 # 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
+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\$ '
 
 
 #################################
@@ -136,51 +149,38 @@ fi
 #################################
 
 function print_exit_code {
-    _exit_msg="\\033[01;33mexit code: $?\\033[00m"
+    local exit_code="$?"
     if [ -z "${BASH_SOURCE[1]}" ]; then
-        echo -e "$_exit_msg"
+        printf '\e[01;33mexit code: %s\e[00m\n' \
+            "$exit_code"
     fi
-    unset _exit_msg
 }
 trap print_exit_code ERR
 
 
-###############
-# Pager setup #
-###############
 
-export PAGER='less'
-export LESS='RS'
-
-
-################
-# Editor setup #
-################
+####################
+# Function aliases #
+####################
 
-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
+# This prevents long/ugly command lines from showing up in xterm titles
+function aliasf {
+    eval "function $1 { $2 \$@; }"
+}
 
 
 ##########################
 # ls aliases and colours #
 ##########################
 
-# GNU ls colours
-eval "$(TERM=xterm dircolors 2> /dev/null)"
-
-# BSD ls colours
-export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
+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
@@ -196,20 +196,21 @@ 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'
+    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
-    alias pscpu='_psresources -r|less -S'
-    alias psmem='_psresources -m|less -S'
-    alias pstime='ps -wAo pid,user,lstart,args|less -S'
+    aliasf pscpu '_psresources -r|less -S'
+    aliasf psmem '_psresources -m|less -S'
+    aliasf pstime 'ps -wAo pid,user,lstart,args|less -S'
 fi
 
 #################
@@ -221,13 +222,12 @@ 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'
+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'
@@ -259,21 +259,25 @@ function slowshell {
     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 "$@"
 }
 
 
+##################
+# 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 #
 #########################
@@ -281,16 +285,48 @@ function ssh_unsafe {
 [ -r ~/.bashrc_local ] && source ~/.bashrc_local
 
 
-##################
-# xterm titlebar #
-##################
+########################
+# Terminal integration #
+########################
 
-# When at a prompt display `hostname:workingdir`
-PROMPT_COMMAND='echo -ne "\\033]0;$(hostname|cut -d. -f1):${PWD/$HOME/\~}\\007"'
+# When at a prompt display `workingdir (hostname)`
+function print_title_prompt {
+    printf '\e]0;bash:%s (%s)\a' \
+        "${PWD/$HOME/\~}" \
+        "$(hostname -s)"
 
-# 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
+    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
 
 
 ###########