workstation/roles/home-cli/files/bashrc

345 lines
7.2 KiB
Bash
Raw Normal View History

2021-03-11 22:49:39 +00:00
#!/bin/bash
2021-02-20 22:59:54 +00:00
export ACP_BASHRC=1
2022-08-15 10:35:34 +00:00
if [ -d /var/acp ]
then
export ACP=/var/acp
else
export ACP="$HOME/data"
fi
2022-08-18 07:44:25 +00:00
if [ -n "$ZSH_VERSION" ]
then
# These commands do not exist in zsh.
alias bind=false
alias shopt=false
fi
2021-05-02 21:17:58 +00:00
if [ -f /etc/bashrc ]
then
. /etc/bashrc
fi
2021-02-20 22:59:54 +00:00
if [ -f /usr/share/bash-completion/bash_completion ]
then
2021-05-02 21:17:58 +00:00
. /usr/share/bash-completion/bash_completion
2021-02-20 22:59:54 +00:00
elif [ -f /etc/bash_completion ]
then
2021-05-02 21:17:58 +00:00
. /etc/bash_completion
2021-02-20 22:59:54 +00:00
fi
2021-05-02 21:12:59 +00:00
if [ -f /usr/share/bash-completion/completions/quilt ]
then
2021-05-02 21:17:58 +00:00
. /usr/share/bash-completion/completions/quilt
complete -F _quilt_completion -o filenames dquilt
2021-05-02 21:12:59 +00:00
fi
2021-02-20 22:59:54 +00:00
_acp_add_path () {
2021-06-18 20:36:41 +00:00
if ! [[ ":$PATH:" =~ ":$1:" ]] && test -d "$1"
2021-05-02 21:17:58 +00:00
then
PATH="$1:$PATH"
fi
export PATH
2021-02-20 22:59:54 +00:00
}
2022-08-09 11:23:02 +00:00
# Last in the list means first in the path.
2022-03-06 16:10:55 +00:00
for dir in "$HOME/.local/bin" \
2021-05-02 21:17:58 +00:00
"$HOME/.cargo/bin" \
2021-11-10 16:33:19 +00:00
"${XDG_DATA_HOME:-$HOME/.local/share}/gem/ruby/bin" \
2022-07-17 21:53:12 +00:00
"${XDG_DATA_HOME:-$HOME/.local/share}/flatpak/exports/bin" \
2022-08-16 08:38:18 +00:00
/opt/local/bin \
2022-08-16 08:40:22 +00:00
/opt/local/sbin \
2022-07-17 21:56:28 +00:00
/var/lib/flatpak/exports/bin \
2022-08-15 10:35:34 +00:00
$ACP/bin \
2021-11-14 16:34:02 +00:00
/usr/local/bin \
2021-05-02 21:17:58 +00:00
/usr/local/sbin \
2021-11-14 16:34:02 +00:00
/usr/bin \
/usr/sbin
2021-02-20 22:59:54 +00:00
do
2021-05-02 21:17:58 +00:00
_acp_add_path "$dir"
2021-02-20 22:59:54 +00:00
done
umask 0022
if [ -z "$ZSH_VERSION" ]
2022-08-18 07:49:32 +00:00
then
set -o emacs
set -o noclobber
fi
2021-02-20 22:59:54 +00:00
# Locale and language.
## Clear the existing locale settings and set options I always want.
export TIME_STYLE=long-iso # Used by GNU 'ls'.
2022-07-01 16:01:26 +00:00
export TZ=Europe/London # Can override this elsewhere if needed.
2021-05-13 12:44:41 +00:00
if [ -f /etc/locale.conf ]
then
. /etc/locale.conf
fi
if [ -f "${XDG_CONFIG_DIR:-$HOME/.config}/locale.conf" ]
2021-05-13 12:44:41 +00:00
then
. "${XDG_CONFIG_DIR:-$HOME/.config}/locale.conf"
2021-05-13 12:44:41 +00:00
fi
2021-02-20 22:59:54 +00:00
# Other exports.
export ANSIBLE_NOCOWS=1
export EDITOR=vi
export HISTCONTROL=ignorespace:ignoredups
2021-02-21 15:06:35 +00:00
export PAPERSIZE=a4
2021-02-20 22:59:54 +00:00
export POWERSHELL_TELEMETRY_OPTOUT=1
export PYTHONWARNINGS=ignore::UserWarning
2022-08-18 07:44:25 +00:00
if [ "x$(uname -s)" = "xDarwin" ]
then
export BASH_SILENCE_DEPRECATION_WARNING=1
fi
2022-02-10 12:47:48 +00:00
# Override this in ~/.config/bashrc or ~/.ssh/config.
export LIBVIRT_DEFAULT_URI="qemu+ssh://vmhost/system"
2021-03-16 08:26:44 +00:00
if test -x /usr/bin/tty
then
2021-05-02 21:17:58 +00:00
export GPG_TTY=$(tty)
2021-02-20 22:59:54 +00:00
fi
2022-07-17 21:53:12 +00:00
# Set up Vi/Vim.
if command -v vim > /dev/null
2022-02-20 19:32:49 +00:00
then
2022-07-17 21:53:12 +00:00
export EDITOR="vim -f"
alias vi=vim
2022-07-17 21:07:29 +00:00
else
export EDITOR=vi
fi
2022-02-20 19:32:49 +00:00
2022-08-18 07:44:25 +00:00
if [ ${XDG_CURRENT_DESKTOP:-unknown} = GNOME ]
2022-03-04 19:27:20 +00:00
then
export QT_FONT_DPI=72
fi
2021-02-20 22:59:54 +00:00
shopt -s histappend
bind '"\e[1;5C": forward-word'
bind '"\e[1;5D": backward-word'
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
bind 'set bell-style none'
bind 'set completion-ignore-case on'
2022-05-11 11:58:37 +00:00
bind 'set enable-bracketed-paste off'
2021-02-20 22:59:54 +00:00
2022-08-18 07:54:37 +00:00
if [ -n "$ZSH_VERSION" ]
then
bindkey "^[[A" history-beginning-search-backward
bindkey "^[[B" history-beginning-search-forward
fi
2022-01-25 14:18:39 +00:00
export PS1="\[\033[01m\]\u@\h>\[\033[00m\] "
2022-08-18 07:44:25 +00:00
if [ -n "$ZSH_VERSION" ]
then
export PS1="%B%n@%m>%b "
fi
2021-11-19 12:23:51 +00:00
if test $(id -u) -ne 0 && test -x /usr/bin/dnf
2021-03-16 08:26:44 +00:00
then
2022-01-11 21:55:41 +00:00
alias dnf="dnf --cacheonly --nogpgcheck"
2021-02-20 22:59:54 +00:00
fi
alias 7zencrypt="7z a -t7z -p -mhe"
alias alu="apt list --upgradable"
alias asdo="apt source --download-only"
alias aurmake="makepkg -irs"
2021-03-16 08:26:44 +00:00
alias dquilt="quilt --quiltrc=\"${HOME}/.quiltrc-dpkg\""
2021-02-20 22:59:54 +00:00
alias f=fossil
alias ip="ip -c"
2022-03-01 10:10:54 +00:00
alias isocal="cal --iso --reform=iso --monday --week"
2022-01-11 21:47:19 +00:00
alias la="ls -a"
alias ll="ls -l"
2022-01-11 21:42:43 +00:00
alias ls="command ls -F"
2021-02-20 22:59:54 +00:00
alias now="date +%Y%m%dT%H%M%S%z"
alias nowu="date -u +%Y%m%dT%H%M%SZ"
alias streamenc="openssl aes-256-cbc -pbkdf2 -in - -out - -e"
alias streamdec="openssl aes-256-cbc -pbkdf2 -in - -out - -d"
alias wgr=wordgrinder
alias ytmp3="youtube-dl -q -x --audio-format=mp3"
alias zlu="zypper list-updates"
2022-07-13 20:30:07 +00:00
# Language aliases.
alias en="LANG=en_GB.UTF-8"
alias fr="LANG=fr_FR.UTF-8"
2022-02-23 10:19:18 +00:00
# Alias dig to drill if dig is not installed.
if [ -x /usr/bin/drill ] && ! [ -x /usr/bin/dig ]
then
alias dig=drill
fi
2021-07-02 21:23:10 +00:00
# Set up Go.
2022-08-15 10:35:34 +00:00
if [ -x "$ACP/opt/go/bin/go" ] || [ -x /usr/bin/go ] || [ -x /usr/local/bin/go ]
2021-02-20 22:59:54 +00:00
then
2021-07-02 22:12:25 +00:00
export GOPROXY=https://proxy.golang.org
export GOPATH="${XDG_DATA_HOME:-$HOME/.local/share}/go"
2021-05-02 21:17:58 +00:00
if [ ! -d "$GOPATH" ]
then
mkdir -p "$GOPATH/bin"
mkdir -p "$GOPATH/pkg"
mkdir -p "$GOPATH/src"
fi
2022-08-15 10:35:34 +00:00
if [ -x "$ACP/opt/go/bin/go" ]
2021-05-02 21:17:58 +00:00
then
2022-08-15 10:35:34 +00:00
export GOROOT="$ACP/opt/go"
2021-05-02 21:17:58 +00:00
_acp_add_path "$GOROOT/bin"
fi
2021-07-02 22:12:25 +00:00
_acp_add_path "$GOPATH/bin"
2021-02-20 22:59:54 +00:00
fi
2022-01-11 21:55:41 +00:00
# Useful aliases for Podman.
2022-07-29 11:08:10 +00:00
_podman_run_args=(
--rm
--interactive
--tty
--security-opt label=disable
2022-07-29 13:21:30 +00:00
--volume $HOME:$HOME
--volume $XDG_RUNTIME_DIR:$XDG_RUNTIME_DIR
2022-07-23 17:42:19 +00:00
--env XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR
--env XDG_SESSION_TYPE=wayland
--ipc host
)
2022-08-01 20:02:10 +00:00
_podman_certs_args=(
--volume /etc/pki/ca-trust:/etc/pki/ca-trust:ro
--volume /etc/ssl/certs:/etc/ssl/certs:ro
)
2022-07-29 11:08:10 +00:00
alias prun="podman run ${_podman_run_args[@]}"
2022-08-01 20:02:10 +00:00
alias pruncerts="podman run ${_podman_run_args[@]} ${_podman_certs_args[@]}"
unset _podman_certs_args
2022-07-29 11:08:10 +00:00
unset _podman_run_args
2021-02-20 22:59:54 +00:00
# Desktop files (used by desktop environments within both X11 and Wayland) are
# looked for in XDG_DATA_DIRS; make sure it includes the relevant directory for
# snappy applications' desktop files.
_snap_xdg_path=/var/lib/snapd/desktop
if [ -n "${XDG_DATA_DIRS##*${_snap_xdg_path}}" ] && [ -n "${XDG_DATA_DIRS##*${_snap_xdg_path}:*}" ]
then
2021-05-02 21:17:58 +00:00
export XDG_DATA_DIRS="${XDG_DATA_DIRS}:${_snap_xdg_path}"
2021-02-20 22:59:54 +00:00
fi
unset _snap_xdg_path
mkcd () {
2021-05-02 21:17:58 +00:00
if [ $# -ne 1 ]
then
echo "mkcd: incorrect arguments (one directory required)." >&2
return 1
else
mkdir -p "$1"
cd "$1"
fi
2021-02-20 22:59:54 +00:00
}
termtitle () {
2021-05-02 21:17:58 +00:00
echo -en "\033]0;$1\007"
2021-02-20 22:59:54 +00:00
}
linapm () {
2021-05-02 21:17:58 +00:00
cat /sys/class/power_supply/BAT0/{status,capacity}
2021-02-20 22:59:54 +00:00
}
xa () {
2021-05-02 21:17:58 +00:00
out=$(xrandr --listactivemonitors | awk 'NR!=1{print " "$NF" "}')
for monitor in "$out"
do
m=$(echo "$monitor" | sed 's/ //g')
xrandr --output "$m" --auto
done
2021-02-20 22:59:54 +00:00
}
emacsro () {
2021-05-02 21:17:58 +00:00
emacs "$1" -f view-mode
2021-02-20 22:59:54 +00:00
}
tm () {
2021-12-30 19:29:43 +00:00
if tmux list-sessions 2>&1 1>/dev/null
2021-05-02 21:17:58 +00:00
then
2021-11-19 12:45:38 +00:00
tmux attach-session
2021-05-02 21:17:58 +00:00
else
2021-11-19 12:45:38 +00:00
tmux new-session
2021-05-02 21:17:58 +00:00
fi
2021-02-20 22:59:54 +00:00
}
if [ -x /usr/bin/yum ] && [ ! -x /usr/bin/dnf ]
then
2021-05-02 21:17:58 +00:00
alias dnf=yum
2021-02-20 22:59:54 +00:00
fi
2021-03-18 10:02:00 +00:00
if [ -S "$SSH_AUTH_SOCK" ] \
2021-05-02 21:17:58 +00:00
&& [ -r "$HOME/.ssh/id_ed25519" ] \
&& [ -r "$HOME/.ssh/id_ed25519.pub" ] \
&& [ $(ssh-add -l | grep "$(ssh-keygen -l -f "$HOME/.ssh/id_ed25519.pub")" | wc -l) = 0 ]
2021-02-20 22:59:54 +00:00
then
2021-05-02 21:17:58 +00:00
ssh-add "$HOME/.ssh/id_ed25519"
2021-02-20 22:59:54 +00:00
fi
if [ -r "${XDG_CONFIG_HOME:-$HOME/.config}/bashrc.local" ]
2021-02-20 22:59:54 +00:00
then
. "${XDG_CONFIG_HOME:-$HOME/.config}/bashrc.local"
fi
usermotd () {
2022-08-18 07:49:32 +00:00
if [ -n "$_acp_motd_shown" ]
then
return 1
2022-08-18 07:49:32 +00:00
fi
export _acp_motd_shown=1
printf "\033[7m"; uname -sr; printf "\033[0m"
if [ -r /etc/os-release ]
then
echo " $(. /etc/os-release; echo $NAME $VERSION)"
fi
2022-08-18 07:44:25 +00:00
if [ "x$(uname -s)" = "xDarwin" ]
2022-08-15 11:10:34 +00:00
then
echo " $(sw_vers -productName) $(sw_vers -productVersion)"
fi
uptime
echo "LANG=$LANG"
2022-07-01 16:01:26 +00:00
if [ -n "$TZ" ]
then
echo "TZ=$TZ"
elif [ -x /usr/bin/timedatectl ] && [ "x$(systemctl is-system-running)" != "xoffline" ]
2021-11-16 20:56:36 +00:00
then
timedatectl show --property=Timezone | sed 's/Timezone=/TZ=/'
fi
}
2022-02-10 08:40:56 +00:00
simpletls () {
# Create a self-signed certificate with:
# openssl req -new -x509 \
2022-08-15 10:35:34 +00:00
# -keyout $ACP/openssl/$(hostname)-key.pem \
# -out $ACP/openssl/$(hostname-cert.pem
2022-02-10 08:40:56 +00:00
if [ "x$1" == "x-l" ]
then
2022-08-15 10:35:34 +00:00
openssl s_server -cert $ACP/openssl/$(hostname)-cert.pem -key ~/data/openssl/$(hostname)-key.pem -tls1_3 -port $2
2022-02-10 08:40:56 +00:00
else
2022-08-15 10:35:34 +00:00
openssl s_client -CAfile $ACP/openssl/$1-cert.pem -tls1_3 -connect $1:$2
2022-02-10 08:40:56 +00:00
fi
}
2022-03-31 13:24:44 +00:00
tlscheck () {
if [ "x$1" == "x" ]
then
echo "Usage: tlscheck hostname:port" >&2
else
2022-07-11 14:55:00 +00:00
openssl s_client -connect $1 </dev/null | openssl x509 -text
2022-03-31 13:24:44 +00:00
fi
}
case "$-" in
*i*)
# Shell is interactive
usermotd
;;
*)
;;
esac
2022-08-18 07:44:25 +00:00
if [ -n "$ZSH_VERSION" ]
then
# These commands do not exist in zsh.
unalias shopt
unalias bind
fi