Quick Tip: Use Bash Aliases To Save Time on The Command Line

As a developer, a fair portion of your time is spent on the command line. Certain command are used religiously, so why not save a keystroke or ten? Let’s take a common unix need: updating your packages.

When you update, you use something like this:

sudo apt update && sudo apt dist-upgrade

Wouldn’t it be nice to only have to type in a single, easy to remember command to run that? With aliases, you can. To update your system, you’d use a command like update, updatecomputer, or whatever you want it to be.

Aliases are basically ways to ‘name’ commands. The nice part is, you can name them to whatever works for you. So how do you set this all up?

Setting Up Aliases

Setting them up is easy! To set up the example above, open a terminal and type alias update="sudo apt update && sudo apt dist-upgrade". Typing update in the terminal now allows you to update your packages, instead of the verbose command above.

Though this works, this command won’t persist across sessions. To make it permanent, you need to edit your bashrc file(keep in mind this may vary between systems). To open it up, you should be able to run sudo nano ~/.bashrc. You can replace nano with your preferred text editor. Once the file is opened, find a nice place to store your commands. Once you save the file, they should start working between sessions and reboots.

Here’s where I put my aliases:

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
 
#custom aliases
alias update="sudo apt update && apt dist-upgrade"
alias lampp="sudo /opt/lampp/manager-linux-x64.run"
alias editalias="sudo gedit ~/.bashrc"
...

There’s plenty more.. but the ls aliases were there by default. They may or may not be there for you. Either way, be sure to organize yours in a neat place with comments.

These won’t work until you restart the terminal or source the bashrc file. If you don’t want to restart your terminal, just run source ~/.bashrc and they should start working!

Example Use Cases

When opening a client or personal project, there’s generally a repeatable process. You cd into the directory, open your code editor, then run some kind of build process.

If you’re anything like me, this can take no less than 30 seconds, and ends up being quite distracting when you just want to get up and running. Turns out, this is pretty simple to alias. Here’s a quick example.

cd ~/Desktop/project/path-to-your-project && code ./ && npm run start

As you can see, this command cds into the path, opens up vscode, then runs your main build script. Other use cases include shortening common unix operations, git commands, and abstracting other commands that are difficult to memorize. At this point, I’m sure you already have a few ideas of your own, enjoy!

Comments