]> code.delx.au - dotfiles/blobdiff - .bash/interactive
bash: Don't allow stdout redirection to overwrite existing files
[dotfiles] / .bash / interactive
index 74a4c3cf82a5ba41a0e66e402286dfcd0347fb55..cc4ca57eb9e4565a211d989710aba8ad6f23236c 100644 (file)
 #! bash
 
-# Sets up the shell for interactive commands
+# Check for unsupported TERM variable
+if ! tput init &> /dev/null; then
+    echo "Warning! TERM=$TERM unsupported, using TERM=xterm"
+    export TERM=xterm
+fi
 
-# Prompt: user@host:directory$
-PS1='\u@\h:\w\$ '
 
-# The all important colours!
-ssource "${HOME}/.bash/colors"
 
-# xterm titlebar displays current command
+# Fancy PS1 with revision control status for git, hg, svn
+
+[ -r /usr/share/git/completion/git-prompt.sh ] && source /usr/share/git/completion/git-prompt.sh
+function my_git_ps1 {
+    find_up_recurse .git || return
+    GIT_PS1_SHOWDIRTYSTATE=1 \
+    GIT_PS1_SHOWUNTRACKEDFILES=1 \
+    __git_ps1 2> /dev/null
+}
+
+function my_hg_ps1 {
+    find_up_recurse .hg || return
+    b="$(hg branch 2>/dev/null)" || return
+    s="$(hg status | cut -c1 | sort -u | tr -d " \n")"
+    echo -n " ($b"
+    [ -n "$s" ] && echo -n " $s"
+    echo -n ")"
+}
+
+function my_svn_ps1 {
+    find_up_recurse .svn || return
+    s="$(svn status --ignore-externals 2>/dev/null | cut -c1 | sort -u | tr -d " \n")"
+    [ -z "$s" ] && return
+    echo -n " ($s)"
+}
+
+if [ -z "$debian_chroot" -a -r /etc/debian_chroot ]; then
+    debian_chroot=$(cat /etc/debian_chroot)
+fi
+PS1='${debian_chroot:+($debian_chroot)}'
+PS1="$PS1"'\[\033[00;31m\]\u@\h\[\033[00m\]:\[\033[00;34m\]\w\[\033[00m\]'
+PS1="$PS1"'\[\033[00;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\033[00m\]'
+PS1="$PS1"'\n\$ '
+
+
+
+# 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
+
+
+
+# ls colours
+eval $(TERM=xterm dircolors 2> /dev/null)
+
+if [ "${DARKTERM:-1}" -eq 1 ]; then
+
+    # Sets colour scheme in apps like Vim
+    export COLORFGBG="15;0"
+
+    # Bold ls colours
+    LS_COLORS="$(echo "${LS_COLORS}" | sed 's/00;/01;/g')"
+    LSCOLORS="ExFxCxDxBxEGEDABAGACAD" # BSD ls
+    PS1="$(echo "${PS1}" | sed 's/00;/01;/g')"
+
+else
+
+    # Sets colour scheme in apps like Vim
+    export COLORFGBG="0;15"
+
+    # Unbold ls colours
+    LS_COLORS="$(echo "${LS_COLORS}" | sed 's/01;/00;/g')"
+    LSCOLORS="exfxcxdxbxegedabagacad" # BSD ls
+fi
+
+
+# xterm titlebar displays 'hostname:workingdir'
+if tput hs || tput tsl &> /dev/null; then
+    PROMPT_COMMAND='tput tsl; echo -n "$(hostname| cut -d. -f1):${PWD/$HOME/~}"; tput fsl;'
+fi
+
+# screen window 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\""
-       unset HOSTNAME
-       ;;
 screen*)
-       PS1="\[\033k\033\\\\\]${PS1}"
-       ;;
-*)
-       ;;
+    PROMPT_COMMAND="$PROMPT_COMMAND echo -ne '\033k\033\\'"
+    ;;
 esac
 
 # Useful aliases
-alias ls='ls --color=auto'
+alias ls='ls --color=auto -v'
 alias ll='ls -hlF'
 alias la='ls -ha'
 alias  l='ls -halF'
+alias f='find . -iname'
+alias webshare='python3 -mhttp.server'
 alias rm='rm -i'
 alias less='less -R'
+alias grep='grep --color=auto --exclude "*.svn-base"'
+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 _psresources='ps -weo pid,user:16,%cpu,%mem,stat,start_time,bsdtime,args'
+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 -weo pid,user:16,lstart,args --sort start_time|less -S'
+
+
+# Super man!
+#  Colourful headings
+#  Terminal title
+function man {
+    tput tsl; echo "man $@"; tput fsl
+
+    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' \
+    man "$@"
+}
+
+# Usage: mcd somedir
+# Creates the directory if it doesn't exist, and changes into it
+function mcd {
+    mkdir -p "${1}" &&
+    cd "${1}"
+}
+
+# Usage: vimf somefile
+# Does a recursive search of the current directory for somefile, then edits it
+function vimf {
+    find . -iname "${1}" -exec vim '{}' +
+}
+
+# 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="$(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 "$@"
+}
+
 
 # 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
+
 # Local customisations
-ssource "${HOME}/.bash/interactive_local"
+[ -r "${HOME}/.bash/interactive_local" ] && source "${HOME}/.bash/interactive_local"
+
+# Load bash completion if available
+[ -r "/etc/bash_completion" ] && source "/etc/bash_completion"