From 9b3bc72eaf04918320ed5ffbbd2cd0736c7e1fc9 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Sat, 7 Aug 2021 00:54:06 +0200 Subject: [PATCH] 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... --- bootstrap | 8 ++- files/aliases.sh | 43 ++++++++++++++++ files/bash_profile | 6 +++ files/bashrc | 119 ++------------------------------------------- files/profile | 65 +++++++++++++++++++++++++ files/solarized.sh | 43 ++++++++++++++++ 6 files changed, 168 insertions(+), 116 deletions(-) create mode 100644 files/aliases.sh create mode 100644 files/bash_profile create mode 100644 files/profile create mode 100644 files/solarized.sh diff --git a/bootstrap b/bootstrap index a8cd92a..24154db 100755 --- a/bootstrap +++ b/bootstrap @@ -25,6 +25,7 @@ ${XDG_CONFIG_HOME}/readline ${XDG_CONFIG_HOME}/tmux ${XDG_CONFIG_HOME}/nvim ${XDG_CONFIG_HOME}/nvim/autoload +${XDG_CONFIG_HOME}/shell ${XDG_DATA_HOME}/bash ${XDG_DATA_HOME}/bash-completion/completions ${XDG_DATA_HOME}/less @@ -37,10 +38,11 @@ EOF DOTFILES=$(cat </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 + diff --git a/files/bash_profile b/files/bash_profile new file mode 100644 index 0000000..c5cefcf --- /dev/null +++ b/files/bash_profile @@ -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" + diff --git a/files/bashrc b/files/bashrc index a9f1d0f..3d5dcb1 100644 --- a/files/bashrc +++ b/files/bashrc @@ -1,77 +1,8 @@ -# shellcheck shell=bash - -# 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 </dev/null 2>&1; then - LOCAL_PREFIX="$(brew --prefix)" - export HOMEBREW_NO_ANALYTICS=1 - export HOMEBREW_NO_AUTO_UPDATE=1 -fi +#!/bin/bash +# Set up bash for interactive use (options, prompt, aliases, etc.) stty -ixon # disable ctrl-s and ctrl-q -############################################################################## -# Customize shell options & variables -############################################################################## - shopt -s cdspell checkwinsize globstar histappend nocaseglob set -o noclobber # Prevent overwriting files with output redirection. @@ -82,8 +13,8 @@ HISTSIZE= HISTTIMEFORMAT="[%F %T] " HISTFILE="$XDG_DATA_HOME/bash/history" -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 +[ -f "${XDG_CONFIG_HOME}/shell/aliases.sh" ] && source "${XDG_CONFIG_HOME}/shell/aliases.sh" +[ -f "${XDG_CONFIG_HOME}/shell/solarized.sh" ] && source "${XDG_CONFIG_HOME}/shell/solarized.sh" Reset="\[$(tput sgr0)\]" PS1_EXIT="\[$(tput setaf "$Red" )\]" # color for last exit code if non-zero @@ -93,7 +24,7 @@ PS1_PWD="\[$(tput setaf "$Cyan" )\]" # PWD color PS1_GIT="\[$(tput setaf "$Blue" )\]" # color for git branch PS1_VENV="\[$(tput setaf "$Violet" )\]" # color for python virtual env PS1_JOBS="\[$(tput setaf "$Magenta")\]" # color for background jobs -PS1_SEP_COLOR="\[$(tput setaf "$Base01" )\]" +PS1_SEP_COLOR="\[$(tput setaf "$Base01" )\]" PS1_SEP=" > " # separator between prompt parts GIT_PS1_SHOWDIRTYSTATE=1 @@ -123,46 +54,6 @@ __ps1_set() { PS1="${ps[0]}$extra$sep_color ${prompt:0:$SHLVL}$Reset " } &>/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() { local configs=( /usr/local/etc/profile.d/bash_completion.sh diff --git a/files/profile b/files/profile new file mode 100644 index 0000000..ca10213 --- /dev/null +++ b/files/profile @@ -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 </dev/null 2>&1; then + LOCAL_PREFIX="$(brew --prefix)" + export HOMEBREW_NO_ANALYTICS=1 + export HOMEBREW_NO_AUTO_UPDATE=1 +fi + diff --git a/files/solarized.sh b/files/solarized.sh new file mode 100644 index 0000000..83152b7 --- /dev/null +++ b/files/solarized.sh @@ -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 +