Introduction to Shell Customization
Shortcuts to Improving Your Terminal Experience
by Randy Pitcher
Regardless of the type of software development you engage in, what flavor of Linux you prefer, or which MacBook you use, many developers spend large amounts of time in the terminal. This is one common environment for most developers and system admins.
Because the terminal is so widely used, I’m surprised to see how many people do no customization of their shell to make their terminal experience better. With just a few lines of code, you can save yourself countless keystrokes through the course of your daily work by modifying your bash
settings.
Scope
The scope of this intro is focused on modifying .bashrc
or .bash_profile
for the purposes of adding aliases and environment variables to make using the terminal better. I’ll be going over my own .bashrc
file to share some of the shortcuts that I’ve found useful for improving my terminal experience.
Discussions on different shells (like ZSH) or upgraded terminals (like iTerm2) are outside the scope of this discussion. That said, I would definitely encourage you to look into that kind of thing if you spend any significant time inside a terminal environment.
A Few Disclaimers
First off, I’ll be using examples from my own .bashrc
file in this post. These shortcuts work great for me, but you should feel free to modify them for your own uses. I’d also like to mention that I’ve built this file from years of software development work and not all of the commands are my own; a ton of them are copied and pasted from great posts online. StackOverflow and Quora were huge sources of information for building my .bashrc
.
Secondly, thoughtless bash customizations are a quick way to confuse yourself. I generally think twice before modifying existing commands (except in cases where the changes are for changing aesthetics). I also tend to shy away from 1 character commands so I don’t accidentally trigger anything. I’ve seen examples of people setting q
as a shortcut for exiting the terminal, but I don’t have enough confidence in my typing to know I won’t do that on accident. In other words, really think before making shell changes.
.bashrc VS .bash_profile
In Linux environments,.bashrc
is a special bash script that runs when a new terminal is started. Modifications to this file will be available in your terminal context.
On a Mac,.bash_profile
is commonly used instead of .bashrc
. While the .bashrc
file also exists on Mac and behaves slightly differently than the .bash_profile
, for the purposes of this post we will assume that the .bash_profile
on Mac is the same as the .bashrc
for Linux.
For both Linux and MacOS, the bash files are typically located in your $HOME
directory.
ls and cd Aliases
I prefer to have colorized, alphabetized output from my ls
command, so I have overwritten the default ls
command to contain flags to improve the output.
The cd
shortcuts link to my most used project directories. I’ve also added a shortcut for cd ..
, which I probably use more than all other shortcuts combined.
Kubernetes and Docker Aliases
The kubectl
command is one I use frequently, but is often way too verbose. By shortening it, and using different aliases for different environments, I can save time with fewer keystrokes and also avoid accidentally using the wrong --kubeconfig
.
The first 3 docker aliases are pretty self explanatory and help in the everyday management of my local docker environment, but the last one deserves more explanation.
The default docker ps
output is too verbose and too wide for my taste. I find it difficult to get straight to the information I need. I also like to see all containers, not just those that are running.
For that reason, I have used a custom table format to give just the information I need from docker ps
and added the -a
flag to get all containers.
Compare the output with and without the alias below:
GIT Aliases
I use branch
mostly for checking which branch I’m currently on and gs
to make sure I haven’t forgotten to git add
any important files.
My coding style leads me to developing in tight, easily commit-able segments of work. I often find my self needing to commit all changed files. For this reason, having a short way to git commit -am
is a good way to save time.
Golang Configs for Non-Golang Developers
I know this may be controversial, but the standard Go package structure doesn’t work for me. Most of my Go code exists with other code inside a single git repository and I don’t have the patience to make that work with Go’s structure.
For me, it’s much easier to modify the GOPATH
to include my current Go working directories. This allows me to compile and test within my git repository clone.
“True” Go developers may find this distasteful, and there are valid reasons to conform to the standard Go structure. But if you’re like me and just need to build working solutions more than you need to conform to ideologies, maybe this will help you too.
Say Goodbye to .pyc Files
I hate the headaches involved with working around pycache
directories and *.pyc
files when working on a python project. In order to avoid seeing these files clutter my working directory, I have 2 approaches.
The first approach is to set the PYTHONDONTWRITEBYTECODE
environment variable to 1. This should prevent the creation of these pesky files in the first place.
However, if you come across a directory that does have the files, I have a pyclean
command that will recursively remove them.
SSH Aliases
I’m terrible at remembering usernames or host values for ssh
ing into remote machines. To avoid this, I create *Shell
aliases to make it easy to get to the right machine.
Having an alias also helps to add a little context to a random <username>@<host>
target.
Conclusion
Knowing just a little bit about how to modify your shell can save time and keystrokes (important for good ergonomics).
If you have some good .bashrc
/ .bash_profile
additions, please share them in the comments! It’s always good to see what other people are using to improve their terminal.
Randy Pitcher is a Big Data and IoT Developer at Hashmap working across industries with a group of innovative technologists and domain experts accelerating high value business outcomes for customers and the community. You can connect with him on LinkedIn or collaborate with him on the Tempus IIoT project.
Also, be sure to catch Randy on the Weekly IoT on Tap Podcast for a casual conversation about IoT from a developer’s perspective.