Skip to content

Instantly share code, notes, and snippets.

@bahadirdonmez
Last active December 14, 2023 22:51
Show Gist options
  • Save bahadirdonmez/283b452c8edc3f332bdb966adbe6b1df to your computer and use it in GitHub Desktop.
Save bahadirdonmez/283b452c8edc3f332bdb966adbe6b1df to your computer and use it in GitHub Desktop.
Bahadir's Personal Bash Startup File
#!/bin/bash
########################################################################################
# Name: Beautiful and Efficient Terminal
#
# Customize the Bash environment to enhance usability, efficiency, and aesthetics.
#
# Author: Bahadır Dönmez
# Date: 2023/12/10
########################################################################################
########################################################################################
# Global Configuration and Script Loading
########################################################################################
# Load global Bash configuration file if available.
GLOBAL_BASH_CONFIG="/etc/bash.bashrc"
if [[ -r "$GLOBAL_BASH_CONFIG" ]]; then
. "$GLOBAL_BASH_CONFIG"
else
echo "Warning: $(basename $GLOBAL_BASH_CONFIG) not found or not readable."
fi
# Load advanced Bash completion features.
BASH_COMPLETION_SCRIPT="/usr/share/bash-completion/bash_completion"
if [[ -r "$BASH_COMPLETION_SCRIPT" ]]; then
. "$BASH_COMPLETION_SCRIPT"
else
echo "Warning: $(basename $BASH_COMPLETION_SCRIPT) not found or not readable."
fi
########################################################################################
# History and Auto-Completion Enhancements
########################################################################################
# Enable searching in history for previous commands that start with the current input.
bind '"\e[A": history-search-backward' # Up arrow for backward history search.
bind '"\e[B": history-search-forward' # Down arrow for forward history search.
# Improve auto-completion behavior.
bind "set completion-ignore-case on" # Case-insensitive completion.
bind "set show-all-if-ambiguous on" # Show all completions on single tab press.
# Enable Ctrl-S for forward history navigation (complementing Ctrl-R for reverse).
stty -ixon
# Configure history settings.
export HISTCONTROL=erasedups:ignoredups:ignorespace # Remove duplicates, ignore spaces.
export HISTSIZE=100000 # Max history lines.
export HISTFILESIZE=100000 # Max history file size.
# Preserve history across terminal sessions by appending rather than overwriting.
shopt -s histappend
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
########################################################################################
# General Utility Aliases and Functions
########################################################################################
# Enhanced 'ls' command aliases for various listing formats.
alias la='ls -Alh --color=auto' # Display all files in long format.
alias ls='ls -AFh --color=auto' # Default listing with color, file type indicators.
alias lk='ls -AlhS --color=auto' # Sort files by size in long format.
# Enhanced 'grep' command aliases for improved search capabilities.
alias grep='grep --color=auto' # Color-highlighted grep search results.
# Searche for text in all files in the current folder
# -i case-insensitive
# -I ignore binary files
# -H causes filename to be printed
# -r recursive search
# -n causes line number to be printed
# optional: -F treat search term as a literal, not a regular expression
# optional: -l only print filenames and not the matching lines ex. grep -irl "$1" *
ftext () {
grep -iIHrn --color=always "$1" . | less -r
}
########################################################################################
# Custom Prompt Design and Welcome Message
########################################################################################
alias mem="free | grep Mem | awk '{printf(\"%.1f\n\", (\$3+\$5)/\$2 * 100.0)}'"
# Function to generate a colorful, informative prompt.
function __setprompt {
# This needs to be first to capture the exit status of the last command
local EXIT="$?"
# Define text colors
local WHITE_BOLD_TXT="\[\e[1;97m\]" # Bold White text
local LBLUE_BOLD_TXT="\[\e[1;94m\]" # Bold Light Blue text
local RED_BOLD_TXT="\[\e[0;31m\]" # Bold Red text
local BLUE_BOLD_TXT="\[\e[0;34m\]" # Bold Blue text
# Define background colors
local DARK_RED_BG="\[\e[41m\]" # Red background
local DARK_BLUE_BG="\[\e[44m\]" # Red background
# Define symbols
local TOP_LINE='╭─'
local BOTTOM_LINE='╰─'
local LEFT_ARROW=$'\uE0B2'
local RIGHT_ARROW=$'\uE0B0'
# Topline
PS1="${LBLUE_BOLD_TXT}${TOP_LINE}"
# Username and host
PS1+="${RED_BOLD_TXT}${LEFT_ARROW}${WHITE_BOLD_TXT}${DARK_RED_BG} \u@\h "
# Seperator
PS1+="${RED_BOLD_TXT}${DARK_BLUE_BG}${RIGHT_ARROW}"
# CPU load
local CPU_LOAD=$(uptime | sed 's/.*load average: //' | awk -F\, '{print $1}')
PS1+="${WHITE_BOLD_TXT}${DARK_BLUE_BG} CPU ${CPU_LOAD}"
# RAM usage
local RAM_USAGE=$(free | grep Mem | awk '{printf("%.1f", $3/$2 * 100.0)}')
PS1+="${WHITE_BOLD_TXT}${DARK_BLUE_BG} · RAM ${RAM_USAGE}% "
# Seperator
PS1+="${BLUE_BOLD_TXT}${DARK_RED_BG}${RIGHT_ARROW}"
# Test
PS1+="${RED_BOLD_TXT}${WHITE_BOLD_TXT}${DARK_RED_BG} \u@\h "
# Date
PS1+="\[${DARKGRAY}\](\[${CYAN}\]\$(date +%a) $(date +%b-'%-m')" # Date
PS1+="${BLUE} $(date +'%-I':%M:%S%P)\[${DARKGRAY}\])-" # Time
# Skip to the next line
PS1+="\n"
# Bottomline
PS1+="${LBLUE_BOLD_TXT}${BOTTOM_LINE}"
# Prefix
if [[ $EUID -ne 0 ]]; then
PS1+="${LBLUE_BOLD_TXT} \$ \[\033[0m\]" # Normal user
else
PS1+="${LBLUE_BOLD_TXT} \# \[\033[0m\]" # Root user
fi
}
# Welcome message
echo -e "${GREEN_BOLD_TXT}Welcome to your terminal, $(whoami)! ${HEART}${WHITE_BOLD_TXT}"
echo -e "${BLUE_BOLD_TXT}Type 'help' for a list of useful commands.${WHITE_BOLD_TXT}\n"
# Set the custom prompt
PROMPT_COMMAND="__setprompt; $PROMPT_COMMAND"
########################################################################################
# Machine Specific Aliases and Functions
########################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment