Bash Aliases and Functions: a Programmer’s Productivity Hack
By: Madison Gipson
Tags: Productivity Hacks, Productivity, Programming, Developer, Coding

As a college student, intern, and relatively new programmer, I’m always looking to my professors, mentors and experienced teammates for coding & workflow tips. Recently, I gained a new appreciation for Bash aliases & functions, which has helped me streamline workflows and cut down on headaches.

Photo by Rich Tervet on Unsplash

What is Bash & .bashrc?

If you’re a programmer, this probably isn’t the first time you’ve heard about Bash. Bash is a shell; in action, it reads and executes commands from either the command line or within a file (files that it can read and execute from are called shell scripts).

Speaking of shell scripts… that’s what the .bashrc file is! It’s a special one that Bash runs whenever it’s started interactively (e.g. when you open a terminal window). Because it’s a shell script and Bash can read and execute any command included in the file, you can set up variables, aliases and functions in .bashrc and execute them in the command line.

Why should you care?

Because anything in .bashrc can be executed via the command line, it means you can configure your own shortcuts and completely customize commands… now that’s what I call a productivity hack.

This isn’t just for programmers; we may get a tad more use out of it if we have a lot of business in the command line, but automating processes and quickly opening applications/folders/files is for everyone.

Plus, it makes you feel super hacker-esque.

Setting up .bashrc

Side note: a lot of people prefer Zsh over Bash, and you can do the same shortcut stuff in both, it just uses different formatting. I personally use Bash because it’s standard across the different systems I use.

To start, find or create your .bashrc file. I’m on Mac and didn’t have one to begin with, so I created one in the /Users/your-username directory and added these simple aliases at the top, for starters:

alias reload = 'source ~/.bashrc'

alias edbash = 'vi ~/.bashrc'

Close the file and return to the /Users/your-username directory. Execute source ~/.bashrc in your command line to reload the .bashrc file. Now executing one of the aliases in the .bashrc file, like edbash , should work.

There’s an issue here though (if you’re on Mac); if you close and re-open the terminal session then try using one of the aliases, it gives the error zsh command not found . Since Mac uses Zsh, you need Zsh to reload .bashrc in each terminal session in order for it to recognize the aliases from that file.

To resolve this, create/edit the file .zshrc from your /Users/your-username directory and add:

[[ -s ~/.bashrc ]] && source ~/.bashrc

Close the file and return to the /Users/your-username directory once again. Try executing an alias from your .bashrc file now; it should work!

You’re well on your way to making Bash your best friend.

Now for the Fun Stuff…

edbash and reload are great, but they just scratch the surface and only apply to the .bashrc file. Here are a few aliases and functions that have come in handy in different cases:

Full disclosure: some of these aliases and functions were provided or inspired by online resources & friends (Lannie Hough). I came up with a few from scratch, but most of my effort was spent coming up with creative ways to apply basic operations.

Everyday Linux Operations

#Lazy way to list files

alias l = 'ls -la' #Jump to often-used directories

alias sr = 'cd ~/Documents/School/Senior\ Research' #Change directory & list files

cl() {

cd $1

ls -la

} #Make directory & enter it

mkcd() {

mkdir -p $1

cd $1

} #Search for a specific file

#Use: "findfile example"

#Results: prints any files that begin with "example", is not case-sensitive, picks up any file type (ex. result: ExampleTest.docx)

findfile() {

file = "$@"

file += "*"

find . -iname $file 2>&1 | grep -v "Operation not permitted"

} #Search for all files with a specific extension

#Use: "fondest swift"

#Results: prints all .swift files

findext() {

ext = "*."

ext += "$@"

find . -iname $ext 2>&1 | grep -v "Operation not permitted"

}

Git & CocoaPods Handling

#Git

alias pull = 'git pull origin master'

alias add = 'git add --all'

alias push = 'git push --all' gitcombo() {

add

message=""

for arg in "$@"

do

message += "$arg"

message += " "

done

git commit -m "$message"

push

} #CocoaPods

pod refresh() {

pod deintegrate

pod update

pod install

}

Process Control

This is where I expose myself for being incredibly lazy… what can I say? I hate having to manually close the 30 applications I have open. Also, I’m aware that these can be dangerous if you haven’t saved your work then kill a program, which is why I was selective about which applications I included in these functions. Some applications, such as VSCode, don’t respond well to be killed (don’t blame them, honestly), so those will have to be closed manually.

stopcoding() {

pkill Trello

pkill Xcode #seems to save changes (I tested it)

pkill Simulator

pkill Discord

pkill SF\ Symbols

} freeram() {

pkill Mail

pkill Calendar

pkill Music

pkill Notes

pkill Pages

pkill Messages

pkill -9 Microsoft\ Outlook #complained without the -9

#pkill literally any application... you get the point

#but I don't include Safari/Chrome or apps used constantly

}

Project Setup

Every time I sat down to work on a code project, I found myself having to open up the same directory, webpage and applications. Why not automate?

#Project Shortcuts (commands generalized)

alias projectgit = 'open https://github.com/mgipson/MyProject'

alias project = 'cd ~/Documents/ProjectFolder; open -a Xcode MyProject.xcworkspace'

project() {

projectgit

project

open -a Trello

}

Happy hacking!