DEV Community

Cover image for Get your work done faster with bash command shortcuts

Posted on

Get your work done faster with bash command shortcuts

Do you despise typing? especially long commands? same long commands repetitively for the 1008th time?

Don't you ever wish you could type just single letter and get the 10 commands executed!

Imagine you say "go" and the repository changes gets

  1. staged,
  2. committed and
  3. pushed!!

Wow! Wouldn't that be magical! Let's do some magic!!

Do you know you can create shortcuts for the bash commands? You need not type a long command every single time and also create meaningful names for the commands.

In this blog, we will see

  1. how to create aliases to bash commands,
  2. create functions that execute a series of commands and
  3. awesome Github repo that has the list of predefined shortcuts that are ready to use.

Creating Aliases

Say you want to type just 'b' instead 'cd ..' every time you want to go behind a directory, you would have to create an alias.

alias b="cd .."

Copy the above code and execute it in your terminal.

Now just try executing "b".


BOOM! That's it! You need not type "cd .." every time! You can just type "b".

Don't' worry, alias won't stop you from using the actual command. This is just another way to use the command, you can also use "cd ..".

Now try the alias in the new terminal.

Is it working? No, right?

You would have to declare alias again, like in step 1, in that terminal window for it to work.

OMG! Looks like we would have to declare alias in each terminal window.

Thank GOD! No! We have a way to tell the terminal to load the alias on every interactive terminal launch.

.bashrc to the rescue!

What is .bashrc?

.bashrc (bash run control) is a shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session. You can put any command in that file that you could type at the command prompt.

This is the file where our alias is going to go! So inside this, we can just add alias b="cd.." and get it working in all the terminal windows.

But what if you want to add a list of aliases. Like below,

alias e="exit"
alias s="sudo $1"
alias b="cd .."
alias c="clear"
alias t="touch $1"
alias md="mkdir $1"
alias rd="rmdir $1"
alias nn="nano $1"

Wouldn't that make the .bashrc script crowded? And also we have to change .bashrc directly every time we want to add/change an alias, which is not a best practice.

So we are going to create a new shell script and add all our aliases inside that. Now it's enough if we just source that file inside .bashrc and also in future, it's just enough to make changes in that file if we want to add/change an alias.

Let's see how to do that!

  1. Let's create our script in the home directory. This is where .bashrc file is placed. So better to keep it all in a single place. Navigate to home directory.

    cd ~
  2. Create a new script named "" in your home directory. "." is prefixed to the name to hide the script in the file directory. So that no one can accidentally change it.

  3. Open the script with the editor of your choice, like vim or nano. If you have Visual studio code you can open with it.

    ##To open in VSCode you can use $code
  4. Add the list of aliases inside. You can use the below list to start with. Change it to your preference.
    Note: "$1" stands for the first parameter.

    alias e="exit"
    alias s="sudo $1"
    alias b="cd .."
    alias c="clear"
    alias t="touch $1"
    alias md="mkdir $1"
    alias rd="rmdir $1"
    alias nn="nano $1"
    #open in vscode
    alias v="code $1"
  5. Don't forget to save it. Now let's open .bashrc file.

    nano .bashrc
  6. Source the file inside .bashrc. Add the below line at the end of .bashrc file.

    #adding custom aliases 
    source ~/
  7. Save and close it.

  8. Open a new terminal window and try the aliases now! (already open terminals won't have the aliases as .bashrc only get executed during the launch.) Type "b" to go back a directory or "c" to clear.

If there is an error in script then you will be seeing the error when you open a new terminal window. Make sure to fix and save it.

To see the full list aliases for your terminal session, try "alias" command.


Executing a bunch of commands with a single command

Okay! All these are fine but how can you create an alias for a bunch of commands?
This is where functions help!

You can create a function like below.

function doc() {
    cd Documents
    echo "Here is your Documents directory, Master!"

The above function executes 3 commands.

  1. Clears your terminal,
  2. navigates to your Documents directory and
  3. prints "Here is your Documents directory, Master!".

You can try it by pasting the above function declaration directly in your terminal and execute the function by just using the function name - "doc"!


You can use it just like any other alias.

The above is just to get you understand function. You can shorten a lot of repetitive tasks with it. As mentioned in the intro, you can add, commit and push to a branch with just one command.

# git add, commit and push to a branch
function gacpb() {
    git add .
    git commit -m "$1"
    git push origin $2

You can execute it like below.

$gacpb "commit message" branch-name

Go ahead and add functions to your script and then they are at your service all the time!

Awesome Bash command shortcuts repository

If you love using shortcuts for commands, you are in for a treat! Here is a repo that has a list of predefined shortcuts for commands of various programs like git, npm,..

Awesome Bash command shortcuts repo

This will help you easily manage shortcuts for different programs and yet import them as a single file in .bashrc. Once set up you can customize it for your preference.

You can also contribute to it. #hacktoberfest Do check it out!

Short commands means less time spent on typing and more time spent on hacking!

Congrats on getting the work done faster!

Cover image source: Google

Top comments (4)

greyfade profile image
Charles Banas • Edited

Sometimes I see people talking about short aliases like this and I find myself wondering, am I the only one who touchtypes and uses tab-completion?

Also, that gacpb command? git commit has a -a switch so you don't have to do git add.

bhuvanaguna profile image

But once you start using short aliases you will not look back! You should give it a try!

gacpb, yeah just noticed that! I will change that! Thank you!

chukrobertson profile image

Kubuntu uses .bash_aliases for your own custom aliases. .bashrc points to .bash_aliases if it exists.

Just a heads up.

bhuvanaguna profile image

Thank you!