aboutsummaryrefslogtreecommitdiff
path: root/bash
diff options
context:
space:
mode:
Diffstat (limited to 'bash')
-rw-r--r--bash/bash_completion.d/man.bash4
-rw-r--r--bash/bash_profile16
-rw-r--r--bash/bashrc38
-rw-r--r--bash/bashrc.d/bc.bash7
-rw-r--r--bash/bashrc.d/cd.bash32
-rw-r--r--bash/bashrc.d/diff.bash4
-rw-r--r--bash/bashrc.d/ed.bash26
-rw-r--r--bash/bashrc.d/gdb.bash4
-rw-r--r--bash/bashrc.d/gpg.bash10
-rw-r--r--bash/bashrc.d/grep.bash33
-rw-r--r--bash/bashrc.d/hgrep.bash16
-rw-r--r--bash/bashrc.d/keychain.bash4
-rw-r--r--bash/bashrc.d/lhn.bash7
-rw-r--r--bash/bashrc.d/ls.bash20
-rw-r--r--bash/bashrc.d/mkcd.bash4
-rw-r--r--bash/bashrc.d/mysql.bash21
-rw-r--r--bash/bashrc.d/prompt.bash14
-rw-r--r--bash/bashrc.d/pwgen.bash8
-rw-r--r--bash/bashrc.d/rcsdiff.bash4
-rw-r--r--bash/bashrc.d/scp.bash10
-rw-r--r--bash/bashrc.d/scr.bash12
-rw-r--r--bash/bashrc.d/sudo.bash7
-rw-r--r--bash/bashrc.d/tmux.bash19
-rw-r--r--bash/bashrc.d/vim.bash15
24 files changed, 43 insertions, 292 deletions
diff --git a/bash/bash_completion.d/man.bash b/bash/bash_completion.d/man.bash
index f1762a01..d2499c0a 100644
--- a/bash/bash_completion.d/man.bash
+++ b/bash/bash_completion.d/man.bash
@@ -35,8 +35,8 @@ _man() {
# Iterate through the manual page paths and add every manual page we find
for manpath in "${manpaths[@]}" ; do
- [[ $manpath ]] || continue
- if [[ $section ]] ; then
+ [[ -n $manpath ]] || continue
+ if [[ -n $section ]] ; then
for page in "$manpath"/"$subdir"/"$word"*."$section"?(.[glx]z|.bz2|.lzma|.Z) ; do
pages[${#pages[@]}]=$page
done
diff --git a/bash/bash_profile b/bash/bash_profile
index ee735720..db1d9bc8 100644
--- a/bash/bash_profile
+++ b/bash/bash_profile
@@ -14,16 +14,12 @@ elif ((BASH_VERSINFO[0] == 2)) &&
fi
# Load any supplementary scripts
-if [[ -d $HOME/.bash_profile.d ]] ; then
- for bash_profile in "$HOME"/.bash_profile.d/*.bash ; do
- if [[ -e $bash_profile ]] ; then
- source "$bash_profile"
- fi
- done
- unset -v bash_profile
-fi
+for sh in "$HOME"/.bash_profile.d/*.bash ; do
+ [[ -e $sh ]] && source "$sh"
+done
+unset -v sh
-# Source interactive Bash config if it exists
-if [[ -e $HOME/.bashrc ]] ; then
+# If ~/.bashrc exists, source that too; the test for interactivity is in there
+if [[ -f $HOME/.bashrc ]] ; then
source "$HOME"/.bashrc
fi
diff --git a/bash/bashrc b/bash/bashrc
index b551819d..7defd85d 100644
--- a/bash/bashrc
+++ b/bash/bashrc
@@ -8,21 +8,22 @@ elif ((BASH_VERSINFO[0] == 2)) &&
return
fi
-# Don't do anything if not running interactively
-if [[ $- != *i* ]] ; then
- return
-fi
+# Make sure the shell is interactive
+case $- in
+ *i*) ;;
+ *) return ;;
+esac
# Don't do anything if running a restricted shell
if shopt -q restricted_shell ; then
return
fi
-# Keep around sixteen million lines of history in file
-HISTFILESIZE=$((1 << 24))
+# Keep around four thousand lines of history in file
+HISTFILESIZE=$((1 << 12))
-# Keep around four thousand lines of history in memory
-HISTSIZE=$((1 << 12))
+# Keep around one thousand lines of history in memory
+HISTSIZE=$((1 << 10))
# Ignore duplicate commands and whitespace in history
HISTCONTROL=ignoreboth
@@ -101,20 +102,15 @@ fi
# If COMP_WORDBREAKS has a value, strip all colons from it; this allows
# completing filenames correctly, since an unquoted colon is not a syntactic
# character: <http://tiswww.case.edu/php/chet/bash/FAQ> (E13)
-if [[ $COMP_WORDBREAKS ]] ; then
+if [[ -n $COMP_WORDBREAKS ]] ; then
COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
fi
-# Load any supplementary scripts
-for bashrc in "$HOME"/.bashrc.d/*.bash ; do
- [[ -e $bashrc ]] || continue
- source "$bashrc"
-done
-unset -v bashrc
-
-# Load any completion files
-for bashcmp in "$HOME"/.bash_completion.d/*.bash ; do
- [[ -e $bashcmp ]] || continue
- source "$bashcmp"
+# Load POSIX shell functions, Bash-specific scripts, and Bash completion files,
+# in that order
+for sh in "$ENV" \
+ "$HOME"/.bashrc.d/*.bash \
+ "$HOME"/.bash_completion.d/*.bash ; do
+ [[ -e $sh ]] && source "$sh"
done
-unset -v bashcmp
+unset -v sh
diff --git a/bash/bashrc.d/bc.bash b/bash/bashrc.d/bc.bash
deleted file mode 100644
index 643678ac..00000000
--- a/bash/bashrc.d/bc.bash
+++ /dev/null
@@ -1,7 +0,0 @@
-# This function is only applicable if bc(1) has the non-POSIX -q option
-command bc -q </dev/null >&0 2>&0 || return
-
-# Don't print the bc(1) welcome message
-bc() {
- command bc -q "$@"
-}
diff --git a/bash/bashrc.d/cd.bash b/bash/bashrc.d/cd.bash
deleted file mode 100644
index 04de96d5..00000000
--- a/bash/bashrc.d/cd.bash
+++ /dev/null
@@ -1,32 +0,0 @@
-# If given two arguments to cd, replace the first with the second in $PWD,
-# emulating a Zsh function that I often find useful; preserves options too
-cd() {
- local arg
- local -a opts
- for arg ; do
- case $arg in
- --)
- shift
- break
- ;;
- -*)
- shift
- opts[${#opts[@]}]=$arg
- ;;
- *)
- break
- ;;
- esac
- done
- if (($# == 2)) ; then
- if [[ $PWD == *"$1"* ]] ; then
- builtin cd "${opts[@]}" -- "${PWD/"$1"/"$2"}"
- else
- printf 'bash: %s: could not replace substring\n' \
- "$FUNCNAME" >&2
- return 2
- fi
- else
- builtin cd "${opts[@]}" -- "$@"
- fi
-}
diff --git a/bash/bashrc.d/diff.bash b/bash/bashrc.d/diff.bash
deleted file mode 100644
index 2c752c8d..00000000
--- a/bash/bashrc.d/diff.bash
+++ /dev/null
@@ -1,4 +0,0 @@
-# Use a unified format for diff(1) by default
-diff() {
- command diff -u "$@"
-}
diff --git a/bash/bashrc.d/ed.bash b/bash/bashrc.d/ed.bash
deleted file mode 100644
index b87894df..00000000
--- a/bash/bashrc.d/ed.bash
+++ /dev/null
@@ -1,26 +0,0 @@
-# Add a colon prompt to ed when a command is expected rather than text; makes
-# it feel a lot more like using ex. Only do this when stdin is a terminal,
-# however. Also try and use -v for more verbose error output, and rlwrap(1) if
-# it's available.
-ed() {
-
- # We're only adding options if input is from a terminal
- if [[ -t 0 ]] ; then
-
- # Colon prompt (POSIX)
- set -- -p : "$@"
-
- # Verbose if available (not POSIX)
- if ed -sv - </dev/null >&0 2>&0 ; then
- set -- -v "$@"
- fi
- fi
-
- # Execute the ed(1) call, in a wrapper if appropriate and with the
- # concluded options
- if [[ -t 0 ]] && hash rlwrap 2>/dev/null ; then
- command rlwrap ed "$@"
- else
- command ed "$@"
- fi
-}
diff --git a/bash/bashrc.d/gdb.bash b/bash/bashrc.d/gdb.bash
deleted file mode 100644
index ec9d4137..00000000
--- a/bash/bashrc.d/gdb.bash
+++ /dev/null
@@ -1,4 +0,0 @@
-# Don't print the GDB copyright message on every invocation
-gdb() {
- command gdb -q "$@"
-}
diff --git a/bash/bashrc.d/gpg.bash b/bash/bashrc.d/gpg.bash
deleted file mode 100644
index 62d123ea..00000000
--- a/bash/bashrc.d/gpg.bash
+++ /dev/null
@@ -1,10 +0,0 @@
-# Wrapper around gpg(1) to stop ``--batch'' breaking things
-gpg() {
- # shellcheck disable=SC2048
- case $* in
- *--ed*|*--gen-k*|*--sign-k*)
- set -- --no-batch "$@"
- ;;
- esac
- command gpg "$@"
-}
diff --git a/bash/bashrc.d/grep.bash b/bash/bashrc.d/grep.bash
deleted file mode 100644
index 11eeb5b0..00000000
--- a/bash/bashrc.d/grep.bash
+++ /dev/null
@@ -1,33 +0,0 @@
-# Store whether we have colors in a variable
-declare -i colors
-colors=$( {
- tput Co || tput colors
-} 2>/dev/null )
-
-# Store grep(1)'s --help output in a variable
-grep_help=$(grep --help 2>/dev/null)
-
-# Use GREPOPTS to add some useful options to grep(1) calls if applicable; we
-# use a function wrapper to do this, rather than GREP_OPTIONS as we don't want
-# to change grep(1)'s actual behaviour inside scripts
-declare -a GREPOPTS
-if [[ -n $GREP_COLORS ]] && ((colors >= 8)) ; then
- GREPOPTS[${#GREPOPTS[@]}]='--color=auto'
-fi
-if [[ $grep_help == *--binary-files* ]] ; then
- GREPOPTS[${#GREPOPTS[@]}]='--binary-files=without-match'
-fi
-if [[ $grep_help == *--exclude* ]] ; then
- GREPOPTS[${#GREPOPTS[@]}]='--exclude={.gitignore,.gitmodules}'
-fi
-if [[ $grep_help == *--exclude-dir* ]] ; then
- GREPOPTS[${#GREPOPTS[@]}]='--exclude-dir={.cvs,.git,.hg,.svn}'
-fi
-
-# Done, unset helper vars
-unset -v grep_help colors
-
-# Define function proper
-grep() {
- command grep "${GREPOPTS[@]}" "$@"
-}
diff --git a/bash/bashrc.d/hgrep.bash b/bash/bashrc.d/hgrep.bash
deleted file mode 100644
index 2f7f8d54..00000000
--- a/bash/bashrc.d/hgrep.bash
+++ /dev/null
@@ -1,16 +0,0 @@
-# Search shell history file for a pattern. If you put your whole HISTFILE
-# contents into memory, then you probably don't need this, as you can just do:
-#
-# $ history | grep PATTERN
-#
-hgrep() {
- if ! (($#)) ; then
- printf >&2 '%s: Need a pattern\n' "$FUNCNAME"
- exit 2
- fi
- if ! [[ -n $HISTFILE ]] ; then
- printf >&2 '%s: No HISTFILE\n' "$FUNCNAME"
- exit 2
- fi
- grep "$@" "$HISTFILE"
-}
diff --git a/bash/bashrc.d/keychain.bash b/bash/bashrc.d/keychain.bash
deleted file mode 100644
index 40fe5d71..00000000
--- a/bash/bashrc.d/keychain.bash
+++ /dev/null
@@ -1,4 +0,0 @@
-# If GPG_TTY is set, update it
-if [[ -n $GPG_TTY ]] ; then
- GPG_TTY=$(tty)
-fi
diff --git a/bash/bashrc.d/lhn.bash b/bash/bashrc.d/lhn.bash
new file mode 100644
index 00000000..89c6f5da
--- /dev/null
+++ b/bash/bashrc.d/lhn.bash
@@ -0,0 +1,7 @@
+# Print the history number of the last command
+lhn () {
+ local last
+ last=$(fc -l -1) || return
+ [[ -n $last ]] || return
+ printf '%u\n' "${last%%[^0-9]*}"
+}
diff --git a/bash/bashrc.d/ls.bash b/bash/bashrc.d/ls.bash
deleted file mode 100644
index 4b647163..00000000
--- a/bash/bashrc.d/ls.bash
+++ /dev/null
@@ -1,20 +0,0 @@
-# Store whether we have colors in a variable
-declare -i colors
-colors=$( {
- tput Co || tput colors
-} 2>/dev/null )
-
-# Use LSOPTS to add some useful options to ls(1) calls if applicable; we use a
-# function wrapper to do this
-declare -a LSOPTS
-if [[ -n $LS_COLORS ]] && ((colors >= 8)) ; then
- LSOPTS[${#LSOPTS[@]}]='--color=auto'
-fi
-
-# Done, unset helper var
-unset -v colors
-
-# Define function proper
-ls() {
- command ls "${LSOPTS[@]}" "$@"
-}
diff --git a/bash/bashrc.d/mkcd.bash b/bash/bashrc.d/mkcd.bash
deleted file mode 100644
index 6342d4a6..00000000
--- a/bash/bashrc.d/mkcd.bash
+++ /dev/null
@@ -1,4 +0,0 @@
-# Create a directory and change into it
-mkcd() {
- mkdir -p -- "$1" && builtin cd -- "$1"
-}
diff --git a/bash/bashrc.d/mysql.bash b/bash/bashrc.d/mysql.bash
deleted file mode 100644
index 0d5ddb86..00000000
--- a/bash/bashrc.d/mysql.bash
+++ /dev/null
@@ -1,21 +0,0 @@
-# If a file ~/.mysql/$1.cnf exists, call mysql(1) using that file. Otherwise
-# just run MySQL with given args. Use restrictive permissions on these files.
-# Examples:
-#
-# [client]
-# host=dbhost.example.com
-# user=foo
-# password=SsJ2pICe226jM
-#
-# [mysql]
-# database=bar
-#
-mysql() {
- local config
- config=$HOME/.mysql/$1.cnf
- if [[ -r $config ]] ; then
- shift
- set -- --defaults-extra-file="$config" "$@"
- fi
- command mysql "$@"
-}
diff --git a/bash/bashrc.d/prompt.bash b/bash/bashrc.d/prompt.bash
index b5ac74b1..081cff69 100644
--- a/bash/bashrc.d/prompt.bash
+++ b/bash/bashrc.d/prompt.bash
@@ -24,7 +24,7 @@ prompt() {
fi
# Basic prompt shape
- PS1='[\u@\h:\w]'
+ PS1='\u@\h:\w'
# Add sub-commands; VCS, job, and return status checks
PS1=$PS1'$(prompt vcs)$(prompt job)$(prompt ret)'
@@ -104,6 +104,13 @@ prompt() {
return 1
fi
+ # Bail if we're not in a work tree
+ local iswt
+ iswt=$(git rev-parse --is-inside-work-tree 2>/dev/null)
+ if [[ $iswt != true ]] ; then
+ return 1
+ fi
+
# Attempt to determine git branch, bail if we can't
local branch
branch=$( {
@@ -130,8 +137,9 @@ prompt() {
state=${state}^
fi
- # Print the status in brackets with a git: prefix
- printf '(git:%s%s)' "${branch:-unknown}" "$state"
+ # Print the status in brackets; add a git: prefix only if there
+ # might be another VCS prompt (because PROMPT_VCS is set)
+ printf '(%s%s%s)' "${PROMPT_VCS:+git:}" "${branch:-unknown}" "$state"
;;
# Subversion prompt function
diff --git a/bash/bashrc.d/pwgen.bash b/bash/bashrc.d/pwgen.bash
deleted file mode 100644
index 7ba056e5..00000000
--- a/bash/bashrc.d/pwgen.bash
+++ /dev/null
@@ -1,8 +0,0 @@
-# Set some defaults for pwgen(1), because its defaults are to give me a long
-# list of relatively short passwords, when I generally want only one good one
-pwgen() {
- if ! (($#)) ; then
- set -- --secure -- "${PWGEN_LENGTH:-15}" "${PWGEN_COUNT:-1}"
- fi
- command pwgen "$@"
-}
diff --git a/bash/bashrc.d/rcsdiff.bash b/bash/bashrc.d/rcsdiff.bash
deleted file mode 100644
index 18b1d324..00000000
--- a/bash/bashrc.d/rcsdiff.bash
+++ /dev/null
@@ -1,4 +0,0 @@
-# Use a unified format for rcsdiff(1) by default
-rcsdiff() {
- command rcsdiff -u "$@"
-}
diff --git a/bash/bashrc.d/scp.bash b/bash/bashrc.d/scp.bash
deleted file mode 100644
index cd2f8521..00000000
--- a/bash/bashrc.d/scp.bash
+++ /dev/null
@@ -1,10 +0,0 @@
-# Wrap scp(1) to check for missing colons
-scp() {
- # shellcheck disable=SC2048
- if (($# >= 2)) && [[ $* != *:* ]] ; then
- printf 'bash: %s: Missing colon, probably an error\n' \
- "$FUNCNAME" >&2
- return 2
- fi
- command scp "$@"
-}
diff --git a/bash/bashrc.d/scr.bash b/bash/bashrc.d/scr.bash
deleted file mode 100644
index bf2990fd..00000000
--- a/bash/bashrc.d/scr.bash
+++ /dev/null
@@ -1,12 +0,0 @@
-# Create a temporary directory and change into it, to stop me putting stray
-# files into $HOME, and making the system do cleanup for me. Single optional
-# argument is the string to use for naming the directory; defaults to "scr".
-scr() {
- if (($# <= 1)) ; then
- pushd -- "$(mktd "${1:-"$FUNCNAME"}")"
- else
- printf 'bash: %s: too many arguments\n' \
- "$FUNCNAME" >&2
- return 2
- fi
-}
diff --git a/bash/bashrc.d/sudo.bash b/bash/bashrc.d/sudo.bash
deleted file mode 100644
index d6d91d12..00000000
--- a/bash/bashrc.d/sudo.bash
+++ /dev/null
@@ -1,7 +0,0 @@
-# Add the -H parameter to sudo(8) calls, always use the target user's $HOME
-sudo() {
- if [[ $1 != -v ]] ; then
- set -- -H "$@"
- fi
- command sudo "$@"
-}
diff --git a/bash/bashrc.d/tmux.bash b/bash/bashrc.d/tmux.bash
deleted file mode 100644
index f0d0e36a..00000000
--- a/bash/bashrc.d/tmux.bash
+++ /dev/null
@@ -1,19 +0,0 @@
-# Attach to existing tmux session rather than create a new one if possible
-tmux() {
-
- # If given any arguments, just use them as they are
- if (($#)) ; then
- :
-
- # If a session exists, just attach to it
- elif command tmux has-session 2>/dev/null ; then
- set -- attach-session -d
-
- # Create a new session with an appropriate name
- else
- set -- new-session -s "${TMUX_SESSION:-default}"
- fi
-
- # Execute with concluded arguments
- command tmux "$@"
-}
diff --git a/bash/bashrc.d/vim.bash b/bash/bashrc.d/vim.bash
deleted file mode 100644
index 37fc1871..00000000
--- a/bash/bashrc.d/vim.bash
+++ /dev/null
@@ -1,15 +0,0 @@
-# If Vim exists on the system, use it instead of ex, vi, and view
-if ! hash vim 2>/dev/null ; then
- return
-fi
-
-# Define functions proper
-ex() {
- vim -e "$@"
-}
-vi() {
- vim "$@"
-}
-view() {
- vim -R "$@"
-}