Skip to Content

Dot bashrc

Posted on

I go ahead and customize my experience with the Bash shell by modifying the default ~/.bashrc placed in $HOME.


Disable truncating Bash history and save it all

# Unlimited history.

Change the history file location because certain bash sessions will still try and truncate ~/.bash_history upon close …


Default is to write history at the end of each session, overwriting the existing file with an updated version. If logged in with multiple sessions, only the last session to exit will have its history saved.

Configure the prompt to write to history after every command and append to the history file, don’t overwrite it …

shopt -s histappend

Add a timestamp per entry. Useful for context when viewing logfiles …


Save all lines of a multiple-line command in the same history entry …

shopt -s cmdhist

Reedit a history substitution line if it failed, and edit a recalled history line before executing …

shopt -s histreedit
shopt -s histverify

Don’t put lines starting with space in the history …


Toggle recording history off/on for a current shell …

alias stophistory="set +o history"
alias starthistory="set -o history"

Links: Unlimited bash history, History truncated on each login, and Preserve history in multiple terminal windows


I set a two-line prompt (handy when displaying long pathnames), adjust the colour based on HOSTNAME, and if logged in remotely include an ‘ssh-session’ message …

if [[ -n "$SSH_CLIENT" ]]; then
    ssh_message=": ssh-session"

if [[ $HOSTNAME = "deb"* ]] || [[ $HOSTNAME = "ull"* ]]; then
    PS1="\[\e[32;1m\]:(\[\e[37;1m\]\u@\h\[\e[33;1m\]${ssh_message}\[\e[32;1m\])-(\[\e[34;1m\]\w\e[32;1m\])\n:.(\[\e[37;1m\]\!\[\e[32;1m\])-\[\e[37;1m\]\$\[\e[0m\] "
    PS1="\[\e[32;1m\]:(\[\e[31;1m\]\u@\h\[\e[33;1m\]${ssh_message}\[\e[32;1m\])-(\[\e[34;1m\]\w\e[32;1m\])\n:.(\[\e[31;1m\]\!\[\e[32;1m\])-\[\e[37;1m\]\$\[\e[0m\] "

Which generates …

:.(1054)-$ ssh foobian
:(daniel@foobian: ssh-session)-(~)

Aliases and functions

Enable color support of ls and a few handy aliases …

if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls="ls -aFlhv --color=auto"
    alias diff="colordiff"
    alias dir="dir --color=auto"
    alias vdir="vdir --color=auto"
    alias grep="grep --color=auto"
    alias fgrep="fgrep --color=auto"
    alias egrep="egrep --color=auto"

# More aliases and functions.
alias ..="cd .."
alias aaa="sudo apt update && apt list --upgradable && sudo apt full-upgrade"
alias arst="setxkbmap us && xmodmap ~/.xmodmap"
alias asdf="setxkbmap us -variant colemak && xmodmap ~/.xmodmap"
bak() { for f in "$@"; do cp "$f" "$f.$(date +%FT%H%M%S).bak"; done; }
alias df="df -hT --total"
alias dpkgg="dpkg -l | grep -i"
dsrt() { du -ach $1 | sort -h; }
alias free="free -h"
alias gpush="git push -u origin master"
alias gsave="git commit -m 'save'"
alias gs="git status"
alias histg="history | grep"
alias lsl="ls | less"
alias mkdir="mkdir -pv"
mcd() { mkdir -p $1; cd $1; } 
mtg() { for f in "$@"; do mv "$f" "${f//[^a-zA-Z0-9\.\-]/_}"; done; }
alias pgrep="pgrep -a"
alias psg="ps aux | grep -v grep | grep -i -e VSZ -e"
alias tmuxa="tmux -f $HOME/.tmux.default.conf attach"
alias wget="wget -c"
alias zzz="sync && systemctl suspend"


Automatically prepend cd when entering just a path in the shell …

shopt -s autocd

Setup keychain for ssh-agent management …

if [ -x /usr/bin/keychain ]; then
    keychain ~/.ssh/id_rsa
    . ~/.keychain/$HOSTNAME-sh

Add directories to my $PATH

export PATH=$PATH:/sbin

Disable XON/XOFF flow control …

stty -ixon

… which enables the use of CNTRL-S in other commands. Example: forward search in history, and disabling screen freeze in vim.

When happy with the changes, save file and reload the config …

source ~/.bashrc

Source: .bashrc

Happy hacking!