bash: factor out profile and aliases

This is a preparation to start experimenting with zsh. The idea is to
share the aliases and environment setup between both shells to avoid
having a bunch of duplication. Even if I decide to stick with zsh it
would be nice to keep the bash configs around for systems where I might
not want to install zsh for some reason. We'll see how this goes...
This commit is contained in:
Fernando Schauenburg 2021-08-07 00:54:06 +02:00
parent d550659828
commit 9b3bc72eaf
6 changed files with 168 additions and 116 deletions

View file

@ -25,6 +25,7 @@ ${XDG_CONFIG_HOME}/readline
${XDG_CONFIG_HOME}/tmux ${XDG_CONFIG_HOME}/tmux
${XDG_CONFIG_HOME}/nvim ${XDG_CONFIG_HOME}/nvim
${XDG_CONFIG_HOME}/nvim/autoload ${XDG_CONFIG_HOME}/nvim/autoload
${XDG_CONFIG_HOME}/shell
${XDG_DATA_HOME}/bash ${XDG_DATA_HOME}/bash
${XDG_DATA_HOME}/bash-completion/completions ${XDG_DATA_HOME}/bash-completion/completions
${XDG_DATA_HOME}/less ${XDG_DATA_HOME}/less
@ -37,10 +38,11 @@ EOF
DOTFILES=$(cat <<EOF DOTFILES=$(cat <<EOF
files/xcompose ${HOME}/.XCompose files/xcompose ${HOME}/.XCompose
files/bashrc ${HOME}/.bash_profile
files/bashrc ${HOME}/.bashrc files/bashrc ${HOME}/.bashrc
files/bash_profile ${HOME}/.bash_profile
files/jupyter_custom.js ${HOME}/.jupyter/custom/custom.js files/jupyter_custom.js ${HOME}/.jupyter/custom/custom.js
files/jupyter_notebook.json ${HOME}/.jupyter/nbconfig/notebook.json files/jupyter_notebook.json ${HOME}/.jupyter/nbconfig/notebook.json
files/profile ${HOME}/.profile
files/ssh_config ${HOME}/.ssh/config files/ssh_config ${HOME}/.ssh/config
files/alacritty.yml ${XDG_CONFIG_HOME}/alacritty/alacritty.yml files/alacritty.yml ${XDG_CONFIG_HOME}/alacritty/alacritty.yml
files/gitconfig ${XDG_CONFIG_HOME}/git/config files/gitconfig ${XDG_CONFIG_HOME}/git/config
@ -51,6 +53,8 @@ files/plug.vim ${XDG_CONFIG_HOME}/nvim/autoload/plug.vim
files/python-startup.py ${XDG_CONFIG_HOME}/python/startup.py files/python-startup.py ${XDG_CONFIG_HOME}/python/startup.py
files/tmux.conf ${XDG_CONFIG_HOME}/tmux/tmux.conf files/tmux.conf ${XDG_CONFIG_HOME}/tmux/tmux.conf
files/init.vim ${XDG_CONFIG_HOME}/nvim/init.vim files/init.vim ${XDG_CONFIG_HOME}/nvim/init.vim
files/aliases.sh ${XDG_CONFIG_HOME}/shell/aliases.sh
files/solarized.sh ${XDG_CONFIG_HOME}/shell/solarized.sh
EOF EOF
) )
@ -136,7 +140,7 @@ task_gather_user_info() {
task_remove_bash_profile() { task_remove_bash_profile() {
heading "Remove pre-installed bash profile" heading "Remove pre-installed bash profile"
for f in .bash_logout .profile; do for f in .bash_logout; do
file_absent "$HOME/$f" file_absent "$HOME/$f"
done done
} }

43
files/aliases.sh Normal file
View file

@ -0,0 +1,43 @@
#!/bin/sh
# ls: make `ls` group directories first if supported.
# lsc: force `ls` to use color output (e.g. for piping into `less`).
if command -v exa >/dev/null 2>&1; then
# Prefer exa if installed
alias \
ls="exa -F --git --group-directories-first --group --links" \
la="ls -a" \
lt="ls -lT -I'.git'" \
lta="lt -a" \
lsc="ls --color=always" \
ltc="lt --color=always"
elif ls --group-directories-first --color=auto >/dev/null 2>&1; then
# GNU ls
alias \
ls="ls -hF --group-directories-first --color=auto" \
la="ls -A" \
lt="tree --dirsfirst -FI '.git|Spotlight-V100|.fseventsd'" \
lsc="ls --color=always" \
ltc="tree -C --dirsfirst -FI '.git'"
else
# BSD ls (e.g. macOS)
alias \
ls="ls -hF -G" \
la="ls -A" \
lt="tree --dirsfirst -FI '.git|Spotlight-V100|.fseventsd'" \
lsc="/usr/bin/env CLICOLOR_FORCE=1 ls" \
ltc="tree -C --dirsfirst -FI '.git'"
fi
alias ll="ls -l"
alias lla="la -l"
alias grep="grep --color=auto"
alias egrep="egrep --color=auto"
alias fgrep="fgrep --color=auto"
if command -v nvim >/dev/null 2>&1; then
alias vim="nvim"
alias vimdiff="nvim -d"
fi

6
files/bash_profile Normal file
View file

@ -0,0 +1,6 @@
#!/bin/bash
# Called by bash (1) on for login shells. Execute ~/.profile if present and
# ~/.bashrc if shell is interactive.
[ -f "${HOME}/.profile" ] && source "${HOME}/.profile"
[[ "$-" == *i* ]] && [ -f "${HOME}/.bashrc" ] && source "${HOME}/.bashrc"

View file

@ -1,77 +1,8 @@
# shellcheck shell=bash #!/bin/bash
# Set up bash for interactive use (options, prompt, aliases, etc.)
# Return immediately if non-interactive (makes FTP clients happy)
[[ "$-" == *i* ]] || return
##############################################################################
# Customize environment
##############################################################################
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
export EDITOR="nvim"
export INPUTRC="$XDG_CONFIG_HOME/readline/inputrc"
export LANG="en_US.UTF-8"
export LANGUAGE="en_US"
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
export LESS="-i -j.49 -M -R -z-2"
export LESSHISTFILE="$XDG_DATA_HOME/less/history"
export LESSHISTSIZE=1000
export LOCAL_CONFIG="$HOME/.local/etc"
export LOCAL_PREFIX="/usr/local"
export PAGER=less
export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/startup.py"
if command -v manpath >/dev/null 2>&1; then
MANPATH="$(unset MANPATH; manpath)"
export MANPATH
fi
# Prevent path_helper from messing with the PATH when starting tmux.
# See: https://superuser.com/a/583502
# shellcheck disable=SC2123 # PATH is being intentionally manipulated here.
# shellcheck disable=SC1091 # /etc/profile is provided by macOS.
[ "$(uname -s)" == "Darwin" ] && { PATH=""; source /etc/profile; }
_prepend_path() { # prepend $1 to var $2 avoiding duplicates using : as separator
if [ -d "$1" ] && [ -n "$2" ]; then
local _path="${!2}" # get path variable value
case ":$_path:" in
*":$1:"*) :;; # dir already in path, noop (:)
*) _path="$1${_path:+:}$_path";; # prepend (adding : if not empty)
esac
printf -v "$2" "%s" "$_path" # write back to path variable
fi
}
# Add custom bin dirs to PATH if they exist and are not already in PATH.
# Prepend custom man directories to MANPATH if they exist, so that we get
# correct man page entries when multiple versions of a command are
# available.
while read -r var dir; do _prepend_path "$dir" "$var"; done <<EOL
PATH $LOCAL_PREFIX/bin
MANPATH $LOCAL_PREFIX/share/man
PATH $HOME/.local/bin
MANPATH $HOME/.local/share/man
EOL
unset var dir _prepend_path
# This check has to be done after PATH manipulation above so we can find brew.
if command -v brew >/dev/null 2>&1; then
LOCAL_PREFIX="$(brew --prefix)"
export HOMEBREW_NO_ANALYTICS=1
export HOMEBREW_NO_AUTO_UPDATE=1
fi
stty -ixon # disable ctrl-s and ctrl-q stty -ixon # disable ctrl-s and ctrl-q
##############################################################################
# Customize shell options & variables
##############################################################################
shopt -s cdspell checkwinsize globstar histappend nocaseglob shopt -s cdspell checkwinsize globstar histappend nocaseglob
set -o noclobber # Prevent overwriting files with output redirection. set -o noclobber # Prevent overwriting files with output redirection.
@ -82,8 +13,8 @@ HISTSIZE=
HISTTIMEFORMAT="[%F %T] " HISTTIMEFORMAT="[%F %T] "
HISTFILE="$XDG_DATA_HOME/bash/history" HISTFILE="$XDG_DATA_HOME/bash/history"
Base03=8 Base02=0 Base01=10 Base00=11 Base0=12 Base1=14 Base2=7 Base3=15 [ -f "${XDG_CONFIG_HOME}/shell/aliases.sh" ] && source "${XDG_CONFIG_HOME}/shell/aliases.sh"
Red=1 Orange=9 Yellow=3 Green=2 Cyan=6 Blue=4 Violet=13 Magenta=5 [ -f "${XDG_CONFIG_HOME}/shell/solarized.sh" ] && source "${XDG_CONFIG_HOME}/shell/solarized.sh"
Reset="\[$(tput sgr0)\]" Reset="\[$(tput sgr0)\]"
PS1_EXIT="\[$(tput setaf "$Red" )\]" # color for last exit code if non-zero PS1_EXIT="\[$(tput setaf "$Red" )\]" # color for last exit code if non-zero
@ -123,46 +54,6 @@ __ps1_set() {
PS1="${ps[0]}$extra$sep_color ${prompt:0:$SHLVL}$Reset " PS1="${ps[0]}$extra$sep_color ${prompt:0:$SHLVL}$Reset "
} &>/dev/null } &>/dev/null
##############################################################################
# Customize shell aliases
##############################################################################
# ls: make `ls` group directories first if supported.
# lsc: force `ls` to use color output (e.g. for piping into `less`).
if command -v exa >/dev/null; then
alias ls="exa -F --git --group-directories-first --group --links"
alias la="ls -a"
alias lt="ls -lT -I'.git'"
alias lta="lt -a"
alias lsc="ls --color=always"
alias ltc="lt --color=always"
elif ls --group-directories-first --color=auto &>/dev/null 2>&1; then
# GNU ls
alias ls="ls -hF --group-directories-first --color=auto"
alias la="ls -A"
alias lt="tree --dirsfirst -FI '.git|Spotlight-V100|.fseventsd'"
alias lsc="ls --color=always"
alias ltc="tree -C --dirsfirst -FI '.git'"
else
# BSD ls (e.g. macOS)
alias ls="ls -hF -G"
alias la="ls -A"
alias lt="tree --dirsfirst -FI '.git|Spotlight-V100|.fseventsd'"
alias lsc="/usr/bin/env CLICOLOR_FORCE=1 ls"
alias ltc="tree -C --dirsfirst -FI '.git'"
fi
alias ll="ls -l"
alias lla="la -l"
alias grep="grep --color=auto"
alias egrep="egrep --color=auto"
alias fgrep="fgrep --color=auto"
##############################################################################
# Run external customizations
##############################################################################
_source_extra_configs() { _source_extra_configs() {
local configs=( local configs=(
/usr/local/etc/profile.d/bash_completion.sh /usr/local/etc/profile.d/bash_completion.sh

65
files/profile Normal file
View file

@ -0,0 +1,65 @@
#!/bin/sh
# Environment variables are set here for login shells.
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
if command -v nvim >/dev/null 2>&1; then
export EDITOR="nvim"
else
export EDITOR="vim"
fi
export INPUTRC="$XDG_CONFIG_HOME/readline/inputrc"
export LANG="en_US.UTF-8"
export LANGUAGE="en_US"
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
export LESS="-i -j.49 -M -R -z-2"
export LESSHISTFILE="$XDG_DATA_HOME/less/history"
export LESSHISTSIZE=1000
export LOCAL_CONFIG="$HOME/.local/etc"
export LOCAL_PREFIX="/usr/local"
export PAGER=less
export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/startup.py"
if command -v manpath >/dev/null 2>&1; then
MANPATH="$(unset MANPATH; manpath)"
export MANPATH
fi
# Prevent path_helper from messing with the PATH when starting tmux.
# See: https://superuser.com/a/583502
[ "$(uname -s)" == "Darwin" ] && { PATH=""; source /etc/profile; }
_prepend_path() { # prepend $1 to var $2 avoiding duplicates using : as separator
if [ -d "$1" ] && [ -n "$2" ]; then
local _path="${!2}" # get path variable value
case ":$_path:" in
*":$1:"*) :;; # dir already in path, noop (:)
*) _path="$1${_path:+:}$_path";; # prepend (adding : if not empty)
esac
printf -v "$2" "%s" "$_path" # write back to path variable
fi
}
# Add custom bin dirs to PATH if they exist and are not already in PATH.
# Prepend custom man directories to MANPATH if they exist, so that we get
# correct man page entries when multiple versions of a command are
# available.
while read -r var dir; do _prepend_path "$dir" "$var"; done <<EOL
PATH $LOCAL_PREFIX/bin
MANPATH $LOCAL_PREFIX/share/man
PATH $HOME/.local/bin
MANPATH $HOME/.local/share/man
EOL
unset var dir _prepend_path
# This check has to be done after PATH manipulation above so we can find brew.
if command -v brew >/dev/null 2>&1; then
LOCAL_PREFIX="$(brew --prefix)"
export HOMEBREW_NO_ANALYTICS=1
export HOMEBREW_NO_AUTO_UPDATE=1
fi

43
files/solarized.sh Normal file
View file

@ -0,0 +1,43 @@
#!/bin/sh
# From: https://ethanschoonover.com/solarized/
# SOLARIZED HEX 16/8 TERMCOL XTERM/HEX L*A*B RGB HSB
# --------- ------- ---- ------- ----------- ---------- ----------- -----------
# base03 #002b36 8/4 brblack 234 #1c1c1c 15 -12 -12 0 43 54 193 100 21
# base02 #073642 0/4 black 235 #262626 20 -12 -12 7 54 66 192 90 26
# base01 #586e75 10/7 brgreen 240 #585858 45 -07 -07 88 110 117 194 25 46
# base00 #657b83 11/7 bryellow 241 #626262 50 -07 -07 101 123 131 195 23 51
# base0 #839496 12/6 brblue 244 #808080 60 -06 -03 131 148 150 186 13 59
# base1 #93a1a1 14/4 brcyan 245 #8a8a8a 65 -05 -02 147 161 161 180 9 63
# base2 #eee8d5 7/7 white 254 #e4e4e4 92 -00 10 238 232 213 44 11 93
# base3 #fdf6e3 15/7 brwhite 230 #ffffd7 97 00 10 253 246 227 44 10 99
# yellow #b58900 3/3 yellow 136 #af8700 60 10 65 181 137 0 45 100 71
# orange #cb4b16 9/3 brred 166 #d75f00 50 50 55 203 75 22 18 89 80
# red #dc322f 1/1 red 160 #d70000 50 65 45 220 50 47 1 79 86
# magenta #d33682 5/5 magenta 125 #af005f 50 65 -05 211 54 130 331 74 83
# violet #6c71c4 13/5 brmagenta 61 #5f5faf 50 15 -45 108 113 196 237 45 77
# blue #268bd2 4/4 blue 33 #0087ff 55 -10 -45 38 139 210 205 82 82
# cyan #2aa198 6/6 cyan 37 #00afaf 60 -35 -05 42 161 152 175 74 63
# green #859900 2/2 green 64 #5f8700 60 -20 65 133 153 0 68 100 60
# ^
# |
# +--- set colors according to this column, assuming a
# properly configured terminal.
Base03=8
Base02=0
Base01=10
Base00=11
Base0=12
Base1=14
Base2=7
Base3=15
Red=1
Orange=9
Yellow=3
Green=2
Cyan=6
Blue=4
Violet=13
Magenta=5