]> code.delx.au - dotfiles/blobdiff - .bashrc
emacs: use console version
[dotfiles] / .bashrc
diff --git a/.bashrc b/.bashrc
index 06b9d258bab6c90dd6108f3fe217142b3732b0cf..2ccae929d0ec187b6f57d0311ad93639bd001771 100644 (file)
--- a/.bashrc
+++ b/.bashrc
@@ -15,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
@@ -42,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 #
 ################
@@ -62,8 +72,6 @@ export HISTCONTROL='erasedups:ignoredups:ignorespace'
 export HISTSIZE='100000'
 export HISTTIMEFORMAT='%F %T '
 
-[ -r /etc/bash_completion ] && source /etc/bash_completion
-
 
 ###############
 # Pager setup #
@@ -102,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
@@ -140,9 +149,10 @@ PS1="$PS1"'\n\$ '
 #################################
 
 function print_exit_code {
-    local exit_msg="\e[01;33mexit code: $?\e[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
 }
 trap print_exit_code ERR
@@ -163,11 +173,14 @@ function aliasf {
 # 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
@@ -183,6 +196,7 @@ alias ll='ls -hlF'
 alias la='ls -ha'
 alias  l='ls -halF'
 
+
 ##############
 # ps aliases #
 ##############
@@ -245,15 +259,6 @@ 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 "$@"
@@ -267,16 +272,10 @@ function ssh_unsafe {
 if emacsclient --version &> /dev/null; then
     export ALTERNATE_EDITOR='vim'
     export EDITOR='emacsclient --tty'
-
-    if [[ "$TERM" == screen* ]]; then
-        aliasf edit 'emacsclient --tty'
-    else
-        aliasf edit 'emacsclient --create-frame --no-wait'
-    fi
 else
     export EDITOR='vim'
-    aliasf edit 'vim'
 fi
+aliasf edit "$EDITOR"
 
 
 #########################
@@ -286,37 +285,45 @@ fi
 [ -r ~/.bashrc_local ] && source ~/.bashrc_local
 
 
-##################
-# xterm titlebar #
-##################
+########################
+# Terminal integration #
+########################
 
 # When at a prompt display `workingdir (hostname)`
 function print_title_prompt {
-    echo -ne "\e]0;bash:${PWD/$HOME/\~} ($(hostname -s))\a"
+    printf '\e]0;bash:%s (%s)\a' \
+        "${PWD/$HOME/\~}" \
+        "$(hostname -s)"
 
     if [[ "$TERM" == screen* ]]; then
-        echo -ne '\ekbash\e\\'
+        printf '\ekbash\e\\'
     fi
 }
-PROMPT_COMMAND=print_title_prompt
 
-# 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
+# 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
 
-    echo -ne '\e]0;'
-    echo -n "$BASH_COMMAND" | tr -cd '[:graph:] '
-    echo -n " ($(hostname -s))"
-    echo -ne '\a'
+    printf '\e]0;%s (%s)\a' \
+        "$(tr -cd '[:graph:] ' <<< "$BASH_COMMAND")" \
+        "$(hostname -s)"
 
     if [[ "$TERM" == screen* ]]; then
-        echo -ne '\ek'
-        echo -n "$BASH_COMMAND" | \
-            sed 's/^sudo //' | \
-            cut -d' ' -f1 | \
-            tr -cd '[:graph:]'
-        echo -ne '\e\\'
+        printf '\ek%s\e\\' \
+            "$(sed -n -e 's/sudo //' -e 's/ .*//' -e 1p <<< "$BASH_COMMAND")"
     fi
 }
 trap print_title_exec DEBUG