From: James Bunton Date: Thu, 27 Oct 2016 07:41:31 +0000 (+1100) Subject: bash: BSD friendly X-Git-Url: https://code.delx.au/dotfiles/commitdiff_plain/79e1077d8fea6a7d39793507c0752b19aa1dca62 bash: BSD friendly --- diff --git a/.bash/interactive b/.bash/interactive index 3d1810f..6ca1cac 100644 --- a/.bash/interactive +++ b/.bash/interactive @@ -1,5 +1,11 @@ #! bash + + +################## +# Terminal setup # +################## + # Check for unsupported TERM variable if ! tput init &> /dev/null; then echo "Warning! TERM=$TERM unsupported, using TERM=xterm" @@ -9,8 +15,16 @@ fi # Disable CTRL-s / CTRL-q stty -ixon +# Sets colour scheme in apps like Vim +export COLORFGBG="15;0" + + -# Fancy PS1 with revision control status for git, hg, svn +############# +# Fancy PS1 # +############# + +# 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 { @@ -36,17 +50,36 @@ function my_svn_ps1 { 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\]' +# 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\]' PS1="$PS1"'\n\$ ' -# Display return codes on error +################ +# xterm titles # +################ + +# hostname:workingdir +DISPLAY_TITLE_COMMAND='echo -ne "\033]0;$(hostname| cut -d. -f1):${PWD/$HOME/~}\007"' + +if [[ "$TERM" =~ ^xterm ]]; then + PROMPT_COMMAND="$DISPLAY_TITLE_COMMAND" +fi + +if [[ "$TERM" =~ ^screen ]]; then + PROMPT_COMMAND="${DISPLAY_TITLE_COMMAND}; echo -ne '\033k\033\\'" +fi + + + +################################# +# Display return codes on error # +################################# + function print_exit_code { _exit_msg="\033[01;33mexit code: $?\033[00m" if [ -z "${BASH_SOURCE[1]}" ]; then @@ -58,69 +91,92 @@ trap print_exit_code ERR -# ls colours -eval $(TERM=xterm dircolors 2> /dev/null) +################ +# bash options # +################ -if [ "${DARKTERM:-1}" -eq 1 ]; then +# Bash should check the terminal size after every command terminates +shopt -s checkwinsize - # Sets colour scheme in apps like Vim - export COLORFGBG="15;0" +# Don't attempt to tab-complete an empty line +shopt -s no_empty_cmd_completion - # 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')" +# Prevent overwriting existing files on stdout redirection +set -o noclobber -else +# Better history +shopt -s histappend +shopt -s cmdhist +export HISTCONTROL="erasedups:ignoredups" +export HISTSIZE="100000" +export HISTTIMEFORMAT="%F %T " - # 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 -# Display 'hostname:workingdir' -DISPLAY_TITLE_COMMAND='echo -ne "\033]0;$(hostname| cut -d. -f1):${PWD/$HOME/~}\007"' +########################## +# ls aliases and colours # +########################## -# xterm title -if [[ "$TERM" =~ ^xterm ]]; then - PROMPT_COMMAND="$DISPLAY_TITLE_COMMAND" -fi +# GNU ls colours +eval $(TERM=xterm dircolors 2> /dev/null) -# screen window displays current command -if [[ "$TERM" =~ ^screen ]]; then - PROMPT_COMMAND="${DISPLAY_TITLE_COMMAND}; echo -ne '\033k\033\\'" -fi +# BSD ls colours +export LSCOLORS="ExFxCxDxBxEGEDABAGACAD" -# Useful aliases -alias ls='ls --color=auto -v' +# 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' + + + +############## +# 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' +else + alias pscpu='_psresources -r|less -S' + alias psmem='_psresources -m|less -S' + alias pstime='ps -wAo pid,user,lstart,args|less -S' +fi + + +################## +# Useful aliases # +################## + 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"' +if echo x | grep -q --color=auto x &> /dev/null; then + alias grep='grep --color=auto' +fi 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' alias pbcopy='xsel --clipboard --input' alias pbcopym='xsel --input' alias pbpaste='xsel --clipboard --output' alias pbpastem='xsel --output' - - # Super man! # Colourful headings # Terminal title @@ -173,21 +229,10 @@ function ssh_unsafe { } -# 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" -export HISTSIZE="100000" -export HISTTIMEFORMAT="%F %T " +########### +# The end # +########### # Local customisations [ -r "${HOME}/.bash/interactive_local" ] && source "${HOME}/.bash/interactive_local"