Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
494 lines (411 sloc) 15.7 KB
#
# ~/.bashrc
#
# FABIC / fabic.net / Bash-it customization.
#
# Note to self :
#
# Dude, this is `dot_bashrc` from your Bash-it personal fork.
#
# This is sourced by `dot_bash_profile` **for interactive shells** ;
# Else (for non-interactive shells) Bash would source it itself.
#
# You may want to use the `install.sh` script.
#
# Some time ago, _they_ used to have these stuff in `dot_bash_profile`.
#
# If the sentinel variable is set, stop here and now.
# ^ not a good idea, one _needs_ to be able to reload its .bashrc on many
# circumstances.
#if [ ! -z ${FABIC_BASHRC_SOURCED+x} ]; then
# return
#fi
# Sentinel variable for me to debug things, may have values 1,2,3
# depending on where this script ended.
export FABIC_BASHRC_SOURCED=1
function _is_interactive_shell() {
[[ $- == *i* ]] && return 0 || return 127
}
function _is_login_shell() {
shopt -q login_shell && return 0 || return 127
}
# See https://stackoverflow.com/a/30520299
function _shell_has_terminal() {
[ -t 1 ] && return 0 || return 127
}
function _shell_stdout_is_redirected() {
[ -p /dev/stdout ] && return 0 || return 127
}
# Define the `echox()` wrapper function.
#if _shell_has_terminal || _shell_stdout_is_redirected ; then
# ^ DON'T: Causes Rsync to complain with “protocol version mismatch -- is your shell clean?”
# Test with: `ssh vps.fabic.net /bin/true > tutu.dat`
if _shell_has_terminal ; then
function echox() {
echo "$@"
}
else
function echox() {
true
}
fi
# FABIC: Group-work friendly umask now/here so that any of g+rwx is preserved,
# (done here early for Rsync/Scp/etc).
umask 0002
# Test for an interactive shell. There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
if ! _is_interactive_shell ; then
# Shell is non-interactive. Be done now!
# FIXME: (?) Dude, this ensures you will never have non-interactive SSH
# (FIXED)(?) connection like Rsync or whatever, that break unexpectedly
# (?) due to some script writing to stdout/err where it shall not
# (?) have.
# (?) BUT: this also means that your environment customizations like
# (?) $PATH, etc, won't be processed at all.
# return
# ^ EDIT/2018-03-16: We're no longer stopping here, for I do need ~/.bashrc
# to set things up, without echoing anything though, so that
# I may run CRON/Systemd-timer scripts that source this script
# as needed.
# => hence `echo` is replaced with `echox()` ;
# and _is_interactive_shell() or _is_login_shell() are
# used where applicable.
false;
fi
FABIC_BASHRC_SOURCED=2
## ~~Auto-Screen~~ (edit: this is your old setup).
#
# FABIC/2012-11-03
# See http://geekscrap.com/2010/02/using-screen-as-your-login-shell/
#
if false && _is_interactive_shell && _shell_has_terminal \
&& [ "x`which screen 2> /dev/null`" != "x" -a ${SHLVL} -eq 1 ];
then
((SHLVL+=1)); export SHLVL
#exec screen -R -e "^Ee" ${SHELL} -l
screen -R -e "^Ee" ${SHELL} -l
((SHLVL-=1)); export SHLVL
echox
echox "///////////////////////////////////////////////////////"
echox "~/.bash_profile: End of screen-itized session, SHLVL=$SHLVL"
echox "///////////////////////////////////////////////////////"
echox
fi
# F.2012-10-31 : List of screens.
#screen -ls
# TMUX / Screen : re-attach to existing sessions automatically.
#
# Find out if we have any TMUX detached session and automatically re-attach
# (note that this test of nested bash processes is _not_ totally correct,
# but works well in practice).
if true && _is_interactive_shell && _shell_has_terminal \
&& [[ $SHLVL == 1 ]] && [ -z ${TMUX+x} ]; then
# TMUX !
if type -P tmux > /dev/null && tmux has-session 2> /dev/null ; then
if tmux attach ; then
echox
echox "# ${BASH_SOURCE[0]} : \`tmux attach\` terminated, ok."
echox
else
echox
echox "# ${BASH_SOURCE[0]} : WARNING: tmux attached terminated with non-zero exit status -_- (nevermind)"
echox
fi
fi
# SCREEN ? (TODO: chaining this here after we run Tmux ain't great, but you don't resort to Screen that often, so anyway...).
if type -P screen > /dev/null && [[ ! -z "$(screen -ls | grep '^\s*[0-9]*\.\S\+\s\+(Detached)\s*$')" ]]; then
if screen -r ; then
echox
echox "# ${BASH_SOURCE[0]} : “screen -r” terminated, ok."
echox
else
echox
echox "# ${BASH_SOURCE[0]} : WARNING: “screen -r” terminated with non-zero exit status -_- (nevermind)"
echox
fi
fi
fi
# TMUX: Inform user that he is run by tmux.
if [ ! -z ${TMUX+x} ]; then
echox "FYI: We're being run by TMUX somehow here."
false && tmux list-session |& sed -e 's/^/ - /'
[ ! -S "${TMUX%%,*}" ] &&
echox "FYI: WARNING: \$TMUX='${TMUX%%,*}' is supposed to be a socket."
fi
# Source the system-wide bashrc.
if true; then
echox "FYI: About to source the system-wide bashrc."
# CentOS
if [ -f /etc/bashrc ]; then
. /etc/bashrc
# Gentoo / Sabayon
elif [ -f /etc/bash/bashrc ]; then
. /etc/bash/bashrc
# Arch Linux, Debian/Kali.
elif [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
fi
# FABIC/2011-08-16
export LANG=en_US.UTF-8
if _shell_has_terminal ; then
# FABIC/2015-12-03
# For Ctrl-S forward search ability (the converse of Ctrl-R) we're disabling
# the “stop scrolling terminal ctrl thing / signal”
stty stop ""
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
shopt -s globstar
# make `less` more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
fi
# FABIC/2015-12-03
# Replace current bash process with a new one, needed here in .bashrc
# so that I can quickly "boot" a new shell with Bash-it initialized.
alias ebl='exec bash -l'
# FABIC: New root shell (and replace current process).
alias esu='exec sudo su -'
#export GIT_EDITOR='/usr/bin/mate -w'
export EDITOR="vim"
# FABIC/2014-11-04 : Maven HOME, Java stuff.
#export M2_HOME=/usr/share/maven-bin-3.0
# Path to the bash it configuration
if [ -d ~/.bash_it ]; then
export BASH_IT="$HOME/.bash_it"
elif [ -d "$HOME/bash-it" ]; then
export BASH_IT="$HOME/bash-it"
elif [ -d "$HOME/dev/bash-it" ]; then
export BASH_IT="$HOME/dev/bash-it"
fi
if [[ -e ~/.warn-server-guard ]]; then
export DUDE_BEWARE_ON_SERVER_YOU_ARE=1
fi
# Lock and Load a custom theme file
# ( location ~/.bash_it/themes/ )
export BASH_IT_THEME='fabi'
# (Advanced): Change this to the name of your remote repo if you
# cloned bash-it with a remote other than origin such as `bash-it`.
# export BASH_IT_REMOTE='bash-it'
# Your place for hosting Git repos. I use this for private repos.
export GIT_HOSTING='git@github.com:fabic'
# Don't check mail when opening terminal.
#unset MAILCHECK
# Change this to your console based IRC client of choice.
export IRC_CLIENT='irssi'
# Set this to the command you use for todo.txt-cli
export TODO="t"
# Set this to false to turn off version control status checking within the prompt for all themes
export SCM_CHECK=true
# Set Xterm/screen/Tmux title with only a short hostname.
# Uncomment this (or set SHORT_HOSTNAME to something else),
# Will otherwise fall back on $HOSTNAME.
export SHORT_HOSTNAME=$(hostname -s)
# Set vcprompt executable path for scm advance info in prompt (demula theme)
# https://github.com/djl/vcprompt
#export VCPROMPT_EXECUTABLE=~/.vcprompt/bin/vcprompt
# (Advanced): Uncomment this to make Bash-it reload itself automatically
# after enabling or disabling aliases, plugins, and completions.
# export BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE=1
# FABIC: For `custom/05_slash_opt_env.bash`: list of sub-directories under /opt
# there that follow the FHS-like layout (bin, etc, share/man, lib)
#SlashOptSubdirs=( )
#SlashOptSubdirs=( mercurial html_tidy apache php$PHP_VERSION git vim )
## # # # # # # # # # # # # # #
# Load Bash It
## # # # # # # # # # # # # # #
source $BASH_IT/bash_it.sh
# FABIC/2011-08-16 : Prompt color, slightly modified from BLFS.
# Setup a red prompt for root and a green one for users.
# Gentoo's PS1 : \[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\]
if false; then
NORMAL="\[\e[0m\]"
RED="\[\e[1;31m\]"
GREEN="\[\e[1;32m\]"
CYAN="\[\e[1;36m\]"
YELLOW="\[\e[33;1m\]"
WHITE="\[\e[37;1m\]"
if [[ $EUID == 0 ]] ; then
USERATHOST=$RED
else
USERATHOST=$GREEN
fi
PS1="$USERATHOST\u@\h $YELLOW\j $WHITE\w $USERATHOST\\$ $NORMAL"
PS2="$USERATHOST> $NORMAL"
fi
# Further aliases may be defined per home@host.
if [ -f ~/.bash_aliases ]; then
echox "# FYI: Found ~/.bash_aliases, sourcing it."
. ~/.bash_aliases
fi
# FABIC: Start an SSH Agent... (is this fine ?)
# @link http://unix.stackexchange.com/a/90869
# ( See also the SSH_ASKPASS setup somewhere here ).
if _is_login_shell \
&& ( [ -z "$SSH_AUTH_SOCK" ] || [ ! -S "$SSH_AUTH_SOCK" ] ) ;
then
echox "# FYI: Dude, starting ssh-agent from ${BASH_SOURCE[0]}"
echox "# BTW: This may _not_ be what you want (!!)"
eval `ssh-agent -s`
# TODO: ^ check exit status or sthg ?
#ssh-add
echox
fi
# SSH agent: If the agent is running then we're on the "local" machine (probably
# not in a remote shell) => check that the agent has at least one key
# and add it if not.
if _is_login_shell && [ -n "$SSH_AUTH_SOCK" ] \
&& [ ! -z ${SSH_AGENT_PID+x} ] \
&& [ $SSH_AGENT_PID -gt 0 ] ;
then
if ! ssh-add -l >/dev/null 2>&1 ; then
echox -e "# \e[31m (!!)\e[0m SSH agent has no keys."
if [ -r ~/.ssh/id_rsa ]; then
echox -e "# \e[31m \`\e[0m Adding key ~/.ssh/id_rsa"
if ! ssh-add ~/.ssh/id_rsa ; then
echox -e "# \e[31m \` (!!!)\e[0m WARNING: \`ssh-add ~/.ssh/id_rsa\` failed (!)"
fi
else
echox -e "# \e[31m \` (!)\e[0m Found no ~/.ssh/id_rsa key."
fi
else
echox -e "# SSH agent has key(s):"
ssh-add -l | sed -e 's/^/# - /'
fi
fi
# SSH: Seahorse wrapper script for “SSH ask pass” instrumentation, which
# hardly ever works as expected btw.
# FABIC/2014-08-18 : temp. here (shall I set this in ~/.xinit ot xsession ?
#export SSH_ASKPASS=/usr/lib64/seahorse/seahorse-ssh-askpass
if false && [ -x ~/bin/seahorse_wrapper.sh -a -z "$SSH_ASKPASS" ]; then
export SSH_ASKPASS=~/bin/seahorse_wrapper.sh
fi
# FIXME:^ move that script under ur custom/ dir. ? or have a dedicated bin/ ?
# X11 $DISPLAY last resort auto-setup, this is _not_ want you generally want
# but this does "solve" a few problems the absurd/incorrect way admittedly,
# and you really do not want to read again about how to setup X11 authN/Z,
# I mean, really.
if [ -z "$DISPLAY" ] && [ ! -z "$SSH_CONNECTION" ];
then
export DISPLAY="${SSH_CONNECTION%% *}:0"
echox
echox "# WARN: Dude! \$DISPLAY is not set, and it happens that you're doing"
echox "# SSH here, so we are inferring \$DISPLAY='$DISPLAY'"
echox "# in the unlikely case you would like to forward back some X11..."
echox "# (like issuing \`xhost +\` from whence you came, or whatever)"
echox
fi
# FABIC: Ruby GEMs bin. dir. (fixme: dirty).
# (skept if RVM is installed).
if [ -d ~/.gem/ruby/ ];
then
echox "FYI: Found some Ruby stuff under ~/.gem/ruby/..."
rubies=( $(ls -1d ~/.gem/ruby/*/bin | sort -Vr) )
if [ ${#rubies[@]} -gt 0 ]; then
if [ -d ~/.rvm/bin/rvm ]; then
echox " ^ Skipping \$PATH setup : RVM is installed."
else
echox " ^ Adding Ruby GEM bin. dir. '${rubies[0]}'."
pathprepend "${rubies[0]}"
fi
else
echox " ^ Found no ~/.gem/ruby/*/bin/ GEM bin. dir."
fi
unset rubies
fi
# Ruby RVM
if [ -x ~/.rvm/bin/rvm ]; then
echox "FYI: Found RVM (Ruby Versions Manager)."
pathprepend $HOME/.rvm/bin
if [ -s "$HOME/.rvm/scripts/rvm" ]; then
source "$HOME/.rvm/scripts/rvm"
echox " ^ Sourced RVM shell script '$HOME/.rvm/scripts/rvm'."
fi
fi
# Host-local ~/.bashrc.d/ : may contain some .sh / .bash scripts that are
# local to the system.
# FABIC/2011-08-16 : Source additional shell scripts that I do not include
# in my Bash-it repo. (ex. per machine scripts)...
if [ -d ~/.bashrc.d/ ]; then
for script in $(find ~/.bashrc.d/ -name \*.sh -o -name \*.bash); do
echox "# Sourcing shell script '$script'"
[ -r "$script" ] && source "$script"
done
fi
# PHP / Composer.phar "globally" installed stuff, e.g.
# `` composer.phar global require fabpot/php-cs-fixer `` :
[ -d ~/.composer/vendor/bin ] && pathprepend ~/.composer/vendor/bin
# ^ it appears this was relocated under ~/.config/ [2018-05].
[ -d ~/.config/composer/vendor/bin ] && pathprepend ~/.config/composer/vendor/bin
# MacPorts installs its stuff there.
[ -d /opt/local/bin ] && pathprepend /opt/local/bin
[ -d /opt/local/sbin ] && pathprepend /opt/local/sbin
# Max OS X : Liip's PHP. MySQL.
[ -d /usr/local/php5/bin ] && pathprepend /usr/local/php5/bin
[ -d /usr/local/mysql/bin ] && pathprepend /usr/local/mysql/bin
# FABIC.2010-08-16 : Additional binary paths.
[ -d ~/local/bin ] && pathprepend ~/local/bin
[ -d ~/.local/bin ] && pathprepend ~/.local/bin
# Node.js pre-compiled binaries from https://nodejs.org/ (FABIC.2017-08-29 ).
nodejs_local_binary_dist="$( (ls -1d ~/.local/node-v*-linux-* | sort -V | tail -n1) 2> /dev/null )"
if [ -d "${nodejs_local_binary_dist}" ] && [ -x "$nodejs_local_binary_dist/bin/node" ]; then
echox "FYI: Found Node.js at '$nodejs_local_binary_dist/bin/node'"
pathprepend "$nodejs_local_binary_dist/bin" PATH
pathprepend "$nodejs_local_binary_dist/include" INCLUDE
#pathprepend "$nodejs_local_binary_dist/share/man" MANPATH
# ^ DON'T -_- `man` ain't no smart at all.
fi
unset nodejs_local_binary_dist
# Rust! ( https://www.rust-lang.org/ )
if [ -f "$HOME/.cargo/bin/rustc" ]; then
echox "FYI: Dude, do some Rust coding or you might rust o_o"
pathprepend "$HOME/.cargo/bin"
fi
# `~/bin` shall come last so that I can override about anything.
# ( Remember that you typically symlink `bash-it/fabic/bin/` as `~/bin/` )
[ -d ~/bin ] && pathprepend ~/bin
# FABIC: gVim forks a shell in a 'dumb' terminal that do not comprehend colors,
# hence we're setting up a simple PS1/2 prompt here.
if [ "$TERM" == "dumb" ] && [ ! -z ${PS1+x} ]; then
echox "# FYI: dumb terminal detected (\$TERM='$TERM') => adjusting prompt (w/o colors)."
[ -n "$PROMPT_COMMAND" ] && unset PROMPT_COMMAND
PS1="(\t) [\u@\H] \j \w $ "
PS2=''
PS4='+ '
echox "# \`-> PS1='$PS1'"
fi
# FABIC: Print out some environment variables :
if true; then
echox "Your environment, some of it :"
for e in SHLVL DISPLAY EDITOR LANG LD_LIBRARY_PATH PATH SSH_CONNECTION SSH_ASKPASS SSH_AUTH_SOCK MANPATH TMUX INCLUDE ;
do
if [ ! -z ${!e+x} ]; then
echox -e " \e[1m$e\e[0m = ${!e}"
fi
done
fi
# FABIC: Print out the path of some executables.
if true; then
Clang="$(type -p clang)"
Rust="$(type -p rustc)"
NodeJS="$(type -p node || type -p nodejs)"
JavaC="$(type -p javac)"
Php="$(type -p php)"
Python="$(type -p python)"
Ruby="$(type -p ruby)"
for vname in Clang Rust NodeJS JavaC Php Python Ruby ; do
if [ ! -z "${!vname}" ]; then
if [ -L "${!vname}" ]; then
echox -e "> \e[94m$vname\e[0m is \e[96m${!vname} \e[39;2m--symlink-to-->\e[22;34m $(readlink -m "${!vname}")\e[0m"
else
echox -e "> \e[94m$vname\e[0m is \e[34m${!vname}\e[0m"
fi
fi
done
unset Clang Rust NodeJS JavaC Php Ruby
fi
FABIC_BASHRC_SOURCED=3
# vim: ft=sh