My bashrc script¶
I've configured my bashrc to provide what I need in my day to day work.
It's very convenient to get all the alias and shortcuts you need already there in your terminal. In this page I'll describe what I use that really helps me.
Using the basic bashrc as base¶
The start of my bashrc comes from my Ubuntu environment, I'll not comment on it. Still, it's a good idea to have a separator in your bashrc file to mark the start of your editions.
#==================================================================
# My defs:
#==================================================================
My overrides¶
# HistorySize override:
export HISTSIZE=100000
export HISTFILESIZE=200000
# My prompt
case "$TERM" in
xterm-color|*-256color) PS1='${debian_chroot:+($debian_chroot)}\[\033[01;35m\][\t]\[\033[01;32m\]\u@\[\033[01;31m\]\h\[\033[00m\]: \[\033[01;34m\]\w\[\033[00m\]\n\$ ';;
esac
Helpers¶
mkcd¶
Add a function to create a directory and go inside it right after
# make a directory and make it the current directory
function mkcd() {
# Can use -v with the other params ($@), keep this log, the last line is the latest created dir to inject in 'cd'
mkdir -p "$1"
cd "$1"
}
copy completion for alias¶
Add possibility to "copy" the completion for an alias.
###################################################################
# My aliases
function alias_with_complete() {
# first alias name, then alias command and finally command to steel complete value
if [ -f "/usr/share/bash-completion/completions/$3" ]; then
source "/usr/share/bash-completion/completions/$3"
fi
completion=$(complete -p $3 2> /dev/null| awk '{ sub(" [^ ]+$", ""); print }')
if [ -n "$completion" ]; then
eval "$completion $1"
else
echo "$3 has no complete entry!!!!"
fi
eval "alias $1='$2'"
}
alias_with_complete "mgit" "mgit" "git"
my aliases¶
Some more aliases:
alias lh='ls -halF'
alias less='less -R'
alias ninja='time ninja'
alias janine='ninja'
alias gti='echo -e "\033[1;36mGTI, et la voiture fait: \033[0;32mvroum, \033[1;33mVRoum, \033[0;31mVROUM!!!!\033[0m"; echo -e " [\033[0;32m###############\033[1;33m#####\033[0;31m##⧫\033[0m]"; git'
alias npm='time npm'
alias ansible-playbook='time ansible-playbook'
alias meteo='curl wttr.in/nancy?m'
Git Prompt¶
I use git-prompt to get a different prompt in my git repositories. Makes it easy to see that's a git repository and this state as well.
###################################################################
# Git prompt
# see https://github.com/magicmonty/bash-git-prompt
if [ -f ~/Private/bash-git-prompt/gitprompt.sh ]; then
# Set config variables first
GIT_PROMPT_ONLY_IN_REPO=1
# GIT_PROMPT_FETCH_REMOTE_STATUS=0 # uncomment to avoid fetching remote status
# GIT_PROMPT_IGNORE_SUBMODULES=1 # uncomment to avoid searching for changed files in submodules
# GIT_PROMPT_WITH_VIRTUAL_ENV=0 # uncomment to avoid setting virtual environment infos for node/python/conda environments
# GIT_PROMPT_SHOW_UPSTREAM=1 # uncomment to show upstream tracking branch
# GIT_PROMPT_SHOW_UNTRACKED_FILES=normal # can be no, normal or all; determines counting of untracked files
# GIT_PROMPT_SHOW_CHANGED_FILES_COUNT=0 # uncomment to avoid printing the number of changed files
# GIT_PROMPT_STATUS_COMMAND=gitstatus_pre-1.7.10.sh # uncomment to support Git older than 1.7.10
# GIT_PROMPT_START=... # uncomment for custom prompt start sequence
# GIT_PROMPT_END=... # uncomment for custom prompt end sequence
# GIT_PROMPT_THEME=Custom # use custom theme specified in file GIT_PROMPT_THEME_FILE (default ~/.git-prompt-colors.sh)
# GIT_PROMPT_THEME_FILE=~/.git-prompt-colors.sh
# GIT_PROMPT_THEME=Solarized # use theme optimized for solarized color scheme
# finaly source the gitprompt script
. ~/Private/bash-git-prompt/gitprompt.sh
fi
direnv¶
You'll need to install the direnv package (sudo apt install direnv). This allows you to have a dedicated .direnv in the directory you want to have a dedicated environment for this dir and its children.
###################################################################
# direnv: per directory environment variables.
# see https://direnv.net/
if [ "$(which direnv)" == "" ];then
echo "Error: direnv package is not installed!!"
else
echo "Per directory environment activated"
eval "$(direnv hook bash)"
fi
History Guardian¶
This is a reinterpretation of mine of the very good idea published in "Linux Magazine France" some years ago. It allows you to have per directory history.
###################################################################
# History Guardian
# see https://github.com/FabienBoutantin/HistoryGuardian
# Source History Guardian script to enable per-project history
if [ -f "/usr/share/history-guardian/activate" ]; then
# Uncomment and change value to set option
#HISTORY_GUARDIAN_ENABLED=false # history guardian is enabled by default
#HISTORY_GUARDIAN_HISTORY_FILE_NAME=.hg_history # defaults to .hg_history
#HISTORY_GUARDIAN_PROPOSE_ON_PROJECTS=false # history guardian propose to init on git directories
#HISTORY_GUARDIAN_PRINT_MESSAGE_AT_EACH_COMMAND=true # history guardian prints an informative message before terminal prompt
#HISTORY_GUARDIAN_APPEND_COMMAND_TO_HISTORY=false # Do not register each command directly in history (resister when closing terminal)
#HISTORY_GUARDIAN_RELOAD_HISTORY_AFTER_EACH_COMMAND=true # Reload history after each command (shares history between different terminals)
# Actually add history guardian to shell
source "/usr/share/history-guardian/activate"
alias_with_complete hgc history-guardian history-guardian
fi
CD helpers¶
This block is for having a command that "cd" you to a dedicated directory. I used to have aliases for that, but, with this function version, I can add completion too. The idea is to have something like:
cd MY/GLOBAL/PATH/WHERE/I/WANT/TO/GO
cd <TAB>
In a single command:
###################################################################
# install cd helpers
# Those are functions that cd directly to the dedicated directory.
# It also includes cd like completion, so you can go inside the provided directory.
# $ github His<TAB>
# becomes $ github HistoryGuardian
# then hit <ENTER> and it will cd you into ${HOME}/Private/GitHub/HistoryGuardian
#
# Note: must be below HistoryGuardian block to have HG works with those functions.
function install_cd_helper() {
local func_name=$1
local path=$2
eval "$func_name() { cd "$path/\$1"; }"
complete -W "$(find "${path}/" -mindepth 1 -maxdepth 1 -type d -regex "[^ ]*" -printf "%f\n")" $func_name
# echo "installed cd helper for $func_name in $path"
}
install_cd_helper work "${HOME}/Work/"
install_cd_helper private "${HOME}/Private"
install_cd_helper projects "${HOME}/Private/projects"
install_cd_helper github "${HOME}/Private/GitHub"
Lazy XXX¶
To have some interactive docker introspection directly in your terminal.
###################################################################
# LazyDocker & Al. Stuff
if command -v docker > /dev/null ; then
if ! command -v lazydocker > /dev/null ; then
alias lazydocker="docker run --rm -ti --name=lazydocker -v /var/run/docker.sock:/var/run/docker.sock -v ~/.config/jesseduffield/lazydocker:/.config/jesseduffield/lazydocker lazyteam/lazydocker"
fi
if ! command -v ctop > /dev/null ; then
alias ctop="docker run --rm -ti --name=ctop -v /var/run/docker.sock:/var/run/docker.sock:ro -v ~/.config/ctop/config:/.ctop quay.io/vektorlab/ctop:latest"
fi
if ! command -v dive > /dev/null ; then
alias dive="docker run --rm -ti --name=dive -v /var/run/docker.sock:/var/run/docker.sock wagoodman/dive"
fi
fi
Conclusion¶
I'm more and more happy I decided to configure my bashrc to my needs. It makes me more confortable in my terminal every day. DO not hesitate to add your own stuff, you'll be pleased to be able to create your own tool(s) with so few lines of bash...