Skip to content

Instantly share code, notes, and snippets.

@ernstki
Last active May 1, 2024 14:19
Show Gist options
  • Save ernstki/4f370ada5e9ba02a57799ad88b3e855f to your computer and use it in GitHub Desktop.
Save ernstki/4f370ada5e9ba02a57799ad88b3e855f to your computer and use it in GitHub Desktop.
Bash login essentials: stuff I always add to my login scripts, but never had all in one place
# .bashrc essentials
# copy and paste any parts that you need into your own ~/.bashrc
# you may get a nicer one by default from your *nix distro; uncomment this
# only if you have an ugly default "bash-x.y$" prompt string
#PROMPT="[\u@\h:\w]$ "
# trim the display of '\w' to just the last three path elements
PROMPT_DIRTRIM=3
# expand environment variables with tab completion if they would form part of
# a valid file/pathname (previously default behavior changed in Bash >=4.2)
# source: https://superuser.com/a/604757/73744
# assistance from: https://unix.stackexchange.com/a/498859/278323
shopt | grep -q '^direxpand\b' && shopt -s direxpand
# other useful options for 'cd'; uncomment if they seem appealing to you
# allow cd-ing to variable names WITHOUT the "$" (e.g., 'cd LABROOT')
#shopt -s cdable_vars
# allow minor speling mistakes in 'cd' commands
#shopt -s cdspell
# don't use Vim as a default editor
# (see also: https://stackoverflow.com/q/11828270)
export EDITOR=nano
export VISUAL=nano
# don't store commands in history if they're prefixed with a space; also keep
# only the most recent duplicate command; if you want to keep commands from
# being written to the history file, also try 'set +o history' (history
# recording is on by default in interactive shells)
export HISTCONTROL=ignorespace:erasedups
# older Bashes might need something like this
#export HISTIGNORE="[ \t]*:cd *:ls *:history *"
# sync history to disk immediately after every command; use 'history -n' to
# re-read history from a different Screen or tmux session
# source: https://unix.stackexchange.com/a/48113
HISTSIZE=100000 # how many commands to remember for current session
HISTFILESIZE=100000 # how many commands to keep in ~/.bash_history
HISTTIMEFORMAT='%F %T ' # give each one a date/timestamp
shopt -s histappend # append to history, don't overwrite
# append current history list to the history file after each command finishes
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }history -a"
# Allow 'less' to display colors and prevent clearing the screen when it quits:
export LESS=-RX
# If 'xclip' exists in the PATH, this is probably Linux, create more
# convenient aliases for manipulating the clipboard that work like
# 'pbcopy'/'pbpaste' (pasteboard copy/paste) on a Mac.
if [[ $(type -t xclip) == file ]]; then
alias pbcopy='xclip -i -selection clipboard'
alias pbpaste='xclip -o '
fi
# enable color support of ls and also add handy aliases
if [[ $TERM != dumb ]] && [[ -x /usr/bin/dircolors ]]; then
eval "`dircolors -b`" # might be done already in system-wide login script(s)
alias ls='ls -F --color=auto' # use 'ls -FG' instead on Darwin / macOS
#alias dir='ls --color=auto --format=vertical'
#alias vdir='ls --color=auto --format=long'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# prevent damaging mistakes by forcing 'rm' and others into "interactive" mode
# by default; use 'rm -f' or '\rm' to force normal (don't-ask) behavior
alias rm='rm -i'
alias mv='mv -i'
alias cp='cp -i'
alias ln='ln -i'
# a better 'which'; identifies *all* copies of a program in your PATH
# directories, aliases and shell functions, too
alias whence='type -a'
# ISO 8601 date; use like this: 'touch filename-$(isod)'
alias isod='date +%Y%m%d'
alias isodd='date +%Y-%m-%d'
# some helpful 'ls' aliases that are often present on Linux systems
alias ll='ls -l'
alias la='ls -A'
alias l='ls -CF'
##
## other aliases you may be interested in (uncomment to enable them)
##
# disk usage by subdirectory; see also `du --max-depth` if on Linux
# Source: https://serverfault.com/a/76201
#alias dud='find . -maxdepth 1 -type d -print0 | xargs -0 du -sh | sort -rh'
# Like 'DIR /ad' on MS-DOS; just show directories
#alias lsd='find * -maxdepth 0 -type d | column'
# 'cd' into the most-recently-modified directory; normally you wouldn't want
# to parse 'ls' (http://mywiki.wooledge.org/ParsingLs), in this case it's OK
#alias cdlast='cd "$(
# \ls -t \
# | while read x; do if [[ -d $x ]]; then echo "$x"; break; fi; done )"'
##
## third-party shell productivity utilities
##
# quickly 'j'ump to recently/frequently-used directories, with fuzzy matching
# please install fasd (https://github.com/clvv/fasd) and uncomment these
#eval "$(fasd --init auto)"
#alias j='fasd_cd -d'
#alias v='f -e vim' # quick opening files with vim
#_fasd_bash_hook_cmd_complete j v
# reformat input lines, rewrapping, preserving indents and comment characters
# please build and/or install Par (http://www.nicemice.net/par/) then
# uncomment this line
#export PARINIT='rTbgqR B=.,?_A_a Q=_s>|'
# a nice .inputrc
# source: http://robertmarkbramprogrammer.blogspot.com/2008/08/inputrc-for-bash-history-completion.html
# Include system wide settings which are ignored by default if one has their
# own .inputrc
$include /etc/inputrc
# By default up/down are bound to previous-history and next-history
# respectively. The following does the same but gives the extra functionality
# where if you type any text (or more accurately, if there is any text between
# the start of the line and the cursor), the subset of the history starting
# with that text is searched (like 4dos for e.g.).
# Note to get rid of a line just Ctrl-C
"\e[B": history-search-forward
"\e[A": history-search-backward
$if Bash
# F10 toggles mc on and off
# Note Ctrl-o toggles panes on and off in mc
"\e[21~": "mc\C-M"
# source: http://jesrui.sdf-eu.org/remember-all-your-bash-history-forever.html
# delete last command from history
#"\e\C-x": "history -d $((HISTCMD-2))"
# delete second to last command from history
#"\e\C-y": "history -d $((HISTCMD-3))"
# do history expansion when space entered
#Space: magic-space
$endif
# case-insensitive completion
set completion-ignore-case On
# Unlock SSH private key upon login and ensure that multiple login sessions
# don't start extra instances of the agent.
#
# Works great with Cygwin.
#
# source: https://help.github.com/articles/working-with-ssh-key-passphrases/
env=~/.ssh/agent.env
agent_load_env () {
test -f "$env" && . "$env" >| /dev/null
}
agent_start () {
(umask 077; ssh-agent >| "$env")
. "$env" >| /dev/null
}
agent_load_env
# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
agent_start
ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
ssh-add
fi
unset env
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment