Supercharge your command line experience
.bashrc and .bash_profile organization
Something that can't be overstated is how handy it can be to split up your dotfile/configuration files that are loaded in when your shell session is kicked off. If you know your way around the shell a little then you've probably seen the .bashrc
and .bash_profile
files.
The .bashrc
is run everytime a new non-login shell is opened (a non-login shell would be like when you've logged into a server already, and then opened a new shell). .bash_profile
is executed once on login; so when you ssh into a system or login to a computer.
Typically you will find the following in your .bashrc
so that all your shell configurtion can live in .bash_profile
and everything is loaded in when a new shell is opened.
[ -n "$PS1" ] && source ~/.bash_profile;
Doing the above is fine, however I'm a firm believer that it's better to split up the configuration in your .bashrc
file into modular chunks. Adding the following block near the top of your .bash_profile
lets you split up configuration for aliases
, exports
and any other separation you might want to create.
# Load the shell dotfiles, and then some:
for file in ~/.{exports,aliases}; do
[ -r "$file" ] && [ -f "$file" ] && source "$file";
done;
unset file;
The above then allows you too create the files .aliases
and .exports
and populate them with any configuration that will be loaded in on shell launch as well.
.exports
exports
expose envirnoment settings to your system and the software that runs on it. You'd be surprised by the level of customization available for most commonly use tools that can be tweaked by changing specific environment variables.
Below are a couple fantastic options you might not know about that can be set in your .exports
or .bash_profile
.
Note: the following configuration can live in your
.bash_profile
as well if you didn't setup a separate.exports
file.
# Increase Bash history size. Allow 32³ entries; the default is 500.
export HISTSIZE='32768';
export HISTFILESIZE="${HISTSIZE}";
# Omit duplicates and commands that begin with a space from history.
export HISTCONTROL='ignoreboth';
# Prefer AU English and use UTF-8. - universal way of setting language
export LANG='en_AU.UTF-8';
export LC_ALL='en_AU.UTF-8';
.inputrc
The .inputrc file provides different customization options that affect interactive shell programs (such as Bash and Python). Though any libraries that leverage the GNU Realine library can adopt the customiations contained by the .inputrc file.
Surprisingly, much of this functionality is disabled by default! Below are some of the options I use and a description of what they do.
# When pressing Tab autocomplete, match regardless of case
set completion-ignore-case on
# Use the text that has already been typed as the prefix for searching through
# commands (i.e. more intelligent Up/Down behavior)
"\e[B": history-search-forward
"\e[A": history-search-backward
# List all matches in case multiple possible completions are possible
set show-all-if-ambiguous on
.aliases
Aliases are another highly underrated way you can customize your shell experience that allows you to alias
a set of commands to another word/command.
Below are a couple of great examples of how I use aliases to speed up my shell use experience.
Note: the following configuration can live in your
.bash_profile
as well if you didn't setup a separate.aliases
file.
# Easier navigation: .., ..., ...., ....., ~ and -
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias ~="cd ~" # `cd` is probably faster to type though
alias -- -="cd -"
# Reload the shell (i.e. invoke as a login shell)
alias reload="exec ${SHELL} -l"
# Print each PATH entry on a separate line
alias path='echo -e ${PATH//:/\\n}'
# Enable aliases to be sudo’ed
alias sudo='sudo '
Another colourful change you can apply! adding the following config can allow you to customize the color of the output from ls
and grep
# Detect which `ls` flavor is in use
if ls --color > /dev/null 2>&1; then # GNU `ls`
colorflag="--color"
export LS_COLORS='no=00:fi=00:di=01;31:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:'
else # macOS `ls`
colorflag="-G"
export LSCOLORS='BxBxhxDxfxhxhxhxhxcxcx'
fi
# List all files colorized in long format
alias l="ls -lF ${colorflag}"
# List all files colorized in long format, excluding . and ..
alias la="ls -lAF ${colorflag}"
# List only directories
alias lsd="ls -lF ${colorflag} | grep --color=never '^d'"
# Always use color output for `ls`
alias ls="command ls ${colorflag}"
# Always enable colored `grep` output
# Note: `GREP_OPTIONS="--color=auto"` is deprecated, hence the alias usage.
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
The above config can be seen below, where:
folders are coloured red, files with execute permissions are green and whereas standard files come up as grey.
Summary
Do you have any other awesome command line hacks that work really well for you? Let me know by reaching out to me on twitter and show me @nathangloverAUS.