]> code.delx.au - dotfiles/blobdiff - .bashrc
bashrc: git-prompt for Fedora
[dotfiles] / .bashrc
diff --git a/.bashrc b/.bashrc
index 4c7226f6b23d4864e6bfb6c41604264aa182eb5f..5d2f8b203b112da43f56619898298518ab135034 100644 (file)
--- a/.bashrc
+++ b/.bashrc
@@ -1,4 +1,5 @@
-# shellcheck disable=SC1090 # -- Can't follow non-constant source
+# shellcheck disable=SC1090 disable=SC1091 # -- Can't follow non-constant source
+# shellcheck disable=SC1003 # -- Don't complain about '\e\\'
 
 ###########################
 # Settings for all shells #
@@ -61,6 +62,16 @@ 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 #
@@ -69,9 +80,8 @@ export HISTTIMEFORMAT='%F %T '
 # Disable CTRL-s / CTRL-q
 stty -ixon
 
-# hostname:workingdir
-PROMPT_COMMAND='echo -ne "\\033]0;$(hostname|cut -d. -f1):${PWD/$HOME/~}\\007"'
-
+# Use dark background colors in apps like vim
+export COLORFGBG='15;0'
 
 #############
 # Fancy PS1 #
@@ -90,7 +100,14 @@ function find_up_exists {
     return 1
 }
 
-[ -r /usr/share/git/completion/git-prompt.sh ] && source /usr/share/git/completion/git-prompt.sh
+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 \
@@ -109,20 +126,14 @@ 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 " ($s)"
+    [ -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
+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\$ '
 
 
 #################################
@@ -130,40 +141,24 @@ 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 #
-################
-
-if emacsclient --version &> /dev/null; then
-    export ALTERNATE_EDITOR='vim'
-    export EDITOR='emacsclient --tty'
+####################
+# Function aliases #
+####################
 
-    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 \$@; }"
+}
 
 
 ##########################
@@ -196,14 +191,14 @@ alias  l='ls -halF'
 
 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
 
 #################
@@ -215,13 +210,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'
@@ -232,8 +226,6 @@ fi
 
 # man with coloured headings and a terminal title
 function man {
-    echo -ne "\\033]0;man $*\\007"
-
     env \
         LESS_TERMCAP_md=$'\E[01;38;5;74m' \
         LESS_TERMCAP_me=$'\E[0m' \
@@ -269,8 +261,66 @@ 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'
+
+    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
+
+
+#########################
+# Optional local config #
+#########################
+
+[ -r ~/.bashrc_local ] && source ~/.bashrc_local
+
+
+##################
+# xterm titlebar #
+##################
+
+# 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
+}
+PROMPT_COMMAND=print_title_prompt
+
+# 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 #
 ###########
-
-[ -r ~/.bashrc_local ] && source ~/.bashrc_local