Skip to main content

Add shortcuts to your bash terminal

ยท 2 min read
Carlos Angulo Mascarell

In this post, I will define shortcuts in my bash terminal. The implementation is based on using aliases and functions. Super simple!

Requirements

  • Linux (I'm using Elementary OS, a Ubuntu-based distro, try it!)
  • VS Code. If you don't want to use it, change all the code commands in the bash code for your text editor. Alternatives: gedit or gnome-text-editor.
  • Remember to add execution permissions to the scripts through chmod +x

Aliases

You can create aliases to avoid type long commands. Next are some examples:

alias tf="terraform"
alias goToRepos="cd $HOME/repos"
alias editAwsCredentials="code $HOME/.aws"
alias editBashProfile="code $HOME/.bashrc"

aliases-bash.sh

goToReposExecution

Please note $HOME is an environment variable defined by the system, it refers to your home path, in my case /home/carlos.

Functions

You can also define functions as shortcuts for daily tasks. I have the next ones:

#!/bin/bash

openRepoUrl() {
local gitUrl=$(git remote get-url --all origin)
local repoUrl="${gitUrl%".git"}" # Removing .git prefix
if [[ -n "$repoUrl" ]]; then
echo "repoUrl:$repoUrl"
xdg-open $repoUrl
fi
}

# REQUIRES fzf, this list the current folder interactively

lsf() {
local chosenDir=$(ls | fzf)
if [[ -n "$chosenDir" ]]; then
cd $chosenDir
fi
}

No need to focus on the implementation, I just want to point out some examples.

functionsExecution

How to integrate those shortcuts in the bash terminal?

In order to load the shortcuts every time we open a terminal, we have to append them in the shell profile ($HOME/.bashrc for bash terminal, and $HOME/.zshrc for zsh).

However, the profile script will become bigger for every new shortcut we add, to make this extensible we will source (load) the shortcuts from separate scripts. Let me list them:

  1. alias-bash.sh.sh / alias-zsh.sh
  2. functions.sh

Append the next code to your profile:

# update this to your scripts folder
scriptsFolder="YOUR_PATH/scripts"

# source all scripts (instead of `source`, we could use `.` both are the same command)
for i in $scriptsFolder/*.sh; do source "$i"; done
Some notes about loading the functions:

if you want to use the functions in scripts that you call manually from the terminal, you have to export them as next:

funcName(){

}
export -f funcName

reference

Nothing else! I hope this saves you some time using the terminal. Do you have similar shortcuts? Share them in the comments below.

About me

I'm a Software Engineer with experience as Developer and DevOps. The technologies I have worked with are DotNet, Terraform and AWS. For the last one, I have the Developer Associate certification. I define myself as a challenge-seeker person and team player. I simply give it all to deliver high-quality solutions. On the other hand, I like to analyze and improve processes, promote productivity and document implementations (yes, I'm a developer that likes to document ๐Ÿง‘โ€๐Ÿ’ป).

You can check my experience here.

Personal Blog - cangulo.github.io
GitHub - Carlos Angulo Mascarell - cangulo
LinkedIn - Carlos Angulo Mascarell
Twitter - @AnguloMascarell

Did you like it? Share It!


Comments