From: James Bunton Date: Mon, 19 Nov 2018 06:59:15 +0000 (+1100) Subject: bash: consolidate config, move EMAIL into separate file X-Git-Url: https://code.delx.au/dotfiles/commitdiff_plain/034a0b02adf155c5ed008d51b716d60b84402d02 bash: consolidate config, move EMAIL into separate file --- diff --git a/.bash/.gitignore b/.bash/.gitignore deleted file mode 100644 index 19d93c0..0000000 --- a/.bash/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -environment_local -functions_local -interactive_local diff --git a/.bash/environment b/.bash/environment deleted file mode 100644 index 669f1f5..0000000 --- a/.bash/environment +++ /dev/null @@ -1,46 +0,0 @@ -#! bash - -# This file sets up the environment correctly. It gets run for every shell, -# so it must be fast. Also, starting a shell within a shell shouldn't change -# the environment. The path manipulation functions are useful for this. - -# Root gets the default umask. Files created by my user are only rwx by user/group -if [ "$(id -un)" = "root" ]; then - umask 0022 -else - umask 0007 -fi - -# General environment settings -export PAGER="less" -export LESS="RS" -export PYTHONSTARTUP="${HOME}/.pythonrc.py" -export EMAIL="James Bunton " - -# Editor settings -if emacsclient --version &> /dev/null; then - export ALTERNATE_EDITOR="vim" - export EDITOR="emacsclient --tty" - - if [[ "$TERM" == screen* ]]; then - export EDITORBG="$EDITOR" - else - export EDITORBG="emacsclient --create-frame --no-wait" - fi -else - export EDITOR="vim" - export EDITORBG="vim" -fi - -source "${HOME}/.bash/functions" - -pathprepend "${HOME}/bin" - -# Pick up SSH agent socket in case it isn't set automatically -if [ -z "$SSH_AUTH_SOCK" ]; then - export SSH_AUTH_SOCK="/run/user/$(id -u)/keyring/ssh" -fi - -# Load local environment settings -[ -r "${HOME}/.bash/environment_local" ] && source "${HOME}/.bash/environment_local" - diff --git a/.bash/functions b/.bash/functions deleted file mode 100644 index 7420d28..0000000 --- a/.bash/functions +++ /dev/null @@ -1,68 +0,0 @@ -#! bash - -# Useful bash functions. This is sourced by the environment file. -# These are available to scripts, but you shouldn't use them in scripts if you -# want them to be portable. - - -# Usage: pathremove /path/to/bin [PATH] -# Eg, to remove ~/bin from $PATH -# pathremove ~/bin PATH -function pathremove { - local IFS=':' - local NEWPATH - local DIR - local PATHVARIABLE=${2:-PATH} - for DIR in ${!PATHVARIABLE} ; do - if [ "${DIR}" != "${1}" ] ; then - NEWPATH="${NEWPATH:+${NEWPATH}:}${DIR}" - fi - done - export ${PATHVARIABLE}="${NEWPATH}" -} - -# Usage: pathprepend /path/to/bin [PATH] -# Eg, to prepend ~/bin to $PATH -# pathprepend ~/bin PATH -function pathprepend { - pathremove "${1}" "${2}" - [ -d "${1}" ] || return - local PATHVARIABLE="${2:-PATH}" - export ${PATHVARIABLE}="${1}${!PATHVARIABLE:+:${!PATHVARIABLE}}" -} - -# Usage: pathappend /path/to/bin [PATH] -# Eg, to append ~/bin to $PATH -# pathappend ~/bin PATH -function pathappend { - pathremove "${1}" "${2}" - [ -d "${1}" ] || return - local PATHVARIABLE=${2:-PATH} - export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}${1}" -} - -# Usage: is_function something -# Check if a function exists -function is_function { - declare -Ff "$1" > /dev/null -} - -# Usage: find_up_recurse somefile -# Check if a file named somefile exists in this directory or any parent -function find_up_recurse { - d="$1" - pushd . > /dev/null - while [ "$PWD" != "/" ]; do - if [ -e "$d" ]; then - popd > /dev/null - return 0 - fi - cd .. - done - popd > /dev/null - return 1 -} - -# Load local functions -[ -r "${HOME}/.bash/functions_local" ] && source "${HOME}/.bash/functions_local" - diff --git a/.bash/interactive b/.bash/interactive deleted file mode 100644 index ae9d8b9..0000000 --- a/.bash/interactive +++ /dev/null @@ -1,239 +0,0 @@ -#! bash - - - -################## -# Terminal setup # -################## - -# Check for unsupported TERM variable -if ! tput init &> /dev/null; then - echo "Warning! TERM=$TERM unsupported, using TERM=xterm" - export TERM=xterm -fi - -# Disable CTRL-s / CTRL-q -stty -ixon - -# Sets colour scheme in apps like Vim -export COLORFGBG="15;0" - - - -############# -# 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 { - 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)" -} - -# 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 - PS1="$PS1"'\n\[\033k\033\\\]\$> ' -fi - - -############### -# xterm title # -############### - -# hostname:workingdir -PROMPT_COMMAND='echo -ne "\033]0;$(hostname| cut -d. -f1):${PWD/$HOME/~}\007"' - - - -################################# -# 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 - - - -################ -# bash options # -################ - -# 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:ignorespace" -export HISTSIZE="100000" -export HISTTIMEFORMAT="%F %T " - - - -########################## -# ls aliases and colours # -########################## - -# GNU ls colours -eval $(TERM=xterm dircolors 2> /dev/null) - -# BSD ls colours -export LSCOLORS="ExFxCxDxBxEGEDABAGACAD" - -# 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' -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' - -if ! which pbcopy &> /dev/null; then - alias pbcopy='xsel --clipboard --input' - alias pbcopym='xsel --input' - alias pbpaste='xsel --clipboard --output' - alias pbpastem='xsel --output' -fi - -# Super man! -# Colourful headings -# Terminal title -function man { - echo -ne "\033]0;man $@\007" - - 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 --encoding ascii "$@" -} - -# Usage: mcd somedir -# Creates the directory if it doesn't exist, and changes into it -function mcd { - mkdir -p "${1}" && - cd "${1}" -} - -# Usage: editf somefile -# Does a recursive search of the current directory for somefile, then edits it -function editf { - find . -iname "${1}" -exec $EDITORBG '{}' + -} - -# Usage: edit somefile [otherfiles ...] -function edit { - $EDITORBG "$@" -} - -# 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 -o UserKnownHostsFile=/dev/null "$@" -} - - - -########### -# The end # -########### - -# Local customisations -[ -r "${HOME}/.bash/interactive_local" ] && source "${HOME}/.bash/interactive_local" - -# Load bash completion if available -[ -r "/etc/bash_completion" ] && source "/etc/bash_completion" - diff --git a/.bash_profile b/.bash_profile index 6cdf13a..86795d7 100644 --- a/.bash_profile +++ b/.bash_profile @@ -1,3 +1 @@ -[ -r /etc/profile ] && source /etc/profile - -source "${HOME}/.bashrc" +source ~/.bashrc diff --git a/.bashrc b/.bashrc index 99a5c53..bd58678 100644 --- a/.bashrc +++ b/.bashrc @@ -1,5 +1,270 @@ -source "${HOME}/.bash/environment" +########################### +# Settings for all shells # +########################### -if [ -n "${PS1}" ]; then - source "${HOME}/.bash/interactive" +# Set umask, depending on the user ID +if [ "$(id -un)" = "root" ]; then + umask 0022 +else + umask 0007 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 ~/bin -type d 2> /dev/null) + +# Set EMAIL from the freedesktop environment.d +if [ -r ~/.config/environment.d/10-email.conf ]; then + # shellcheck disable=SC1090 + source ~/.config/environment.d/10-email.conf + export EMAIL +fi + +# Pick up SSH agent socket in case it isn't set automatically +if [ -z "$SSH_AUTH_SOCK" ]; then + SSH_AUTH_SOCK="/run/user/$(id -u)/keyring/ssh" + export SSH_AUTH_SOCK +fi + + +########################### +# Interactive shells only # +########################### + +if [ -z "${PS1}" ]; then + return +fi + + +################ +# bash options # +################ + +# 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:ignorespace' +export HISTSIZE='100000' +export HISTTIMEFORMAT='%F %T ' + + +################## +# Terminal setup # +################## + +# Disable CTRL-s / CTRL-q +stty -ixon + +# hostname:workingdir +PROMPT_COMMAND='echo -ne "\\033]0;$(hostname| cut -d. -f1):${PWD/$HOME/~}\\007"' + + +############# +# Fancy PS1 # +############# + +# Revision control status for git, hg, svn + +function find_up_exists { + local d="$PWD" + while [ -n "$d" ]; do + if [ -e "$d/$1" ]; then + return 0 + fi + d="${d%/*}" + done + return 1 +} + +[ -r /usr/share/git/completion/git-prompt.sh ] && source /usr/share/git/completion/git-prompt.sh +function my_git_ps1 { + find_up_exists .git || return + GIT_PS1_SHOWDIRTYSTATE=1 \ + GIT_PS1_SHOWUNTRACKEDFILES=1 \ + __git_ps1 ' (%s)' 2> /dev/null +} + +function my_hg_ps1 { + find_up_exists .hg || return + local status + status="$(hg status | cut -c1 | sort -u | tr -d ' \n')" + echo -n " ($status)" +} + +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)" +} + +# 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 + + +################################# +# 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 + + +############### +# Pager setup # +############### + +export PAGER='less' +export LESS='RS' + + +################ +# Editor setup # +################ + +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 + + +########################## +# ls aliases and colours # +########################## + +# GNU ls colours +# shellcheck disable=SC2046 +eval $(TERM=xterm dircolors 2> /dev/null) + +# BSD ls colours +export LSCOLORS="ExFxCxDxBxEGEDABAGACAD" + +# 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 + +################# +# Other aliases # +################# + +alias f='find . -iname' +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 py='PYTHONSTARTUP=~/.pythonrc.py python' +alias webshare='python3 -mhttp.server' + +if ! command -v pbcopy &> /dev/null; then + alias pbcopy='xsel --clipboard --input' + alias pbcopym='xsel --input' + alias pbpaste='xsel --clipboard --output' + alias pbpastem='xsel --output' +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' \ + LESS_TERMCAP_us=$'\E[04;38;5;146m' \ + LESS_TERMCAP_ue=$'\E[0m' \ + man --encoding ascii "$@" +} + +# Creates the directory if it doesn't exist, and changes into it +function mcd { + # shellcheck disable=SC2164 + mkdir -p "$1" && cd "$1" +} + +# 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 + 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 "$@" +} + +########### +# The end # +########### diff --git a/.config/environment.d/10-email.conf b/.config/environment.d/10-email.conf new file mode 100644 index 0000000..528a28b --- /dev/null +++ b/.config/environment.d/10-email.conf @@ -0,0 +1 @@ +EMAIL=jamesbunton@delx.net.au diff --git a/.gitconfig b/.gitconfig index 2b1c967..caefde2 100644 --- a/.gitconfig +++ b/.gitconfig @@ -1,8 +1,4 @@ -[user] - name = James Bunton - email = jamesbunton@delx.net.au [core] - pager = less excludesfile = ~/.gitignore_global [pager] branch = less -F