Dot bashrc

Last modified on 2019-08-04

Interactive shells source ~/.bashrc and read commands from its standard input, ie a terminal (Debian, by default, also sources .bashrc from .profile). See man bash and INVOCATION for more nitty gritty.

Creating a custom .bashrc configuration allows me to craft a more desirable prompt, create functions and aliases for oft-used commands, enable unlimited history, and tweak default settings to my preferences.

Prompt

I like a bit of colour in my prompt. First, I add some colour codes to my palette …

# Colour codes
GREEN="\\[\\e[1;32m\\]"
YELLOW="\\[\\e[1;33m\\]"
BLUE="\\[\\e[1;34m\\]"
MAGENTA="\\[\\e[1;35m\\]"
WHITE="\\[\\e[1;37m\\]"
RESET="\\[\\e[0m\\]"

I set a two-line prompt (details on the first line; $ on the second with lots of space for input). If accessing console via ssh I include an ‘ssh-session’ message in the prompt …

if [[ -n "$SSH_CLIENT" ]]; then
    ssh_message="-ssh_session"
fi
PS1="${MAGENTA}\\u ${WHITE}at ${GREEN}\\h${YELLOW}${ssh_message} ${WHITE}in ${BLUE}\\w \\n$WHITE\$${RESET} "

Functions

Backup and timestamp files …

bak() { for f in "$@"; do cp "$f" "$f.$(date +%FT%H%M%S).bak"; done; }

List my top 10 most used commands (which make good aliases) …

mostUsedCommands() { history | awk '{print $3}' | sort | uniq -c | sort -rn | head; }

One of my most frequently used commands is to change directories, immediately followed by listing its contents. This function does both by entering a single letter command …

c() { cd "$@" && ls -aFlhNv --color=always; }

Make a directory and change to it immediately …

md() { mkdir -p "$@" && cd "$@" || return; }

Clean up filenames by replacing spaces and non-ascii characters in a filename with underscore …

mtg() { for f in "$@"; do mv "$f" "${f//[^a-zA-Z0-9\.\-]/_}"; done; }

“Mind the gap” (mtg) … I remember hearing this when riding the tube in London … It stuck in my head! 🐧

Aliases

Debian-specific: 0. Generate a list of packages installed (generatePkgList); 1. update repositories; 2. upgrade; 3. remove packages no longer required by any other package …

alias aaa="generatePkgList -d ~/code/debian && sudo apt update && apt list --upgradable && sudo apt full-upgrade && sudo apt autoremove"

Search to determine if a package is installed …

alias dpkgg="dpkg -l | grep -i"

Distro-independent: A few handy aliases …

alias dff="df -hT --total"
alias e="nvim"
alias l="ls -aFlhNv --color=always"
alias myip="ip -f inet address | grep inet | grep -v 'lo$' | cut -d ' ' -f 6,13 && curl ifconfig.me && echo ' external ip'"
alias p="less -R"
alias x="exit"

History

Disable truncating Bash history and save everything …

# Unlimited history.
HISTSIZE=
HISTFILESIZE=

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

HISTFILE=~/.bash_unlimited_history

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.

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

shopt -s histappend
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

Now you can see the commands from all shells in near real-time in ~/.bash_unlimited history. Starting a new shell displays the combined history from all terminals.

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

HISTTIMEFORMAT="%FT%T  "

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

shopt -s cmdhist

Re-edit a history substitution line if it failed …

shopt -s histreedit

Edit a recalled history line before executing …

shopt -s histverify

Do not put lines starting with space in the history …

HISTCONTROL=ignorespace

Toggle history off/on for a current shell …

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

These links were helpful configuring the unlimited history: BASH history truncated to 500 lines on each login and Preserve bash history in multiple terminal windows.

Extras

When resizing a terminal emulator, check the window size after each command and, if necessary, update the values of LINES and COLUMNS …

shopt -s checkwinsize

Disable XON/XOFF flow control. Enables use of Ctrl-S in other commands. Examples: forward search in history; disable screen freeze in vim …

stty -ixon

Bash completion …

if [[ -f /etc/profile.d/bash_completion.sh ]]; then
    # shellcheck source=/dev/null
    . /etc/profile.d/bash_completion.sh
fi

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

$ . ~/.bashrc

Links: my dot bashrc and dot profile.

Happy hacking! 🐧