DEV Community

Cover image for Increasing your productivity using aliases
Agustín Rodríguez
Agustín Rodríguez

Posted on

Increasing your productivity using aliases

To the guy who invented zero... thanks for nothing. 😂

Well, seriously, zero is very important and aliases too, and that's why I'm going to explain how you can add them to your workflow, if you're not already using them, to increase your productivity.

What is an alias

It is just a shortcut, but an important one. Take a second to think in a command that you use every day, for example, cd a basic one to change the directory.

Suppose you are on this path:

~/projects/2021/technologies/react/my-project/src/Components
Enter fullscreen mode Exit fullscreen mode

Now, what are your options to back to the projects directory? Well, you can do some basic like this:

cd ../
cd ../
cd ../
cd ../
cd ../
cd ../
# or
cd ../../../../../../
# or
cd $HOME/projects
# or
cd ~/projects
Enter fullscreen mode Exit fullscreen mode

an easy way could be something like:

projects
Enter fullscreen mode Exit fullscreen mode

internally the real command could be defined as:

alias projects='cd ~/projects'
Enter fullscreen mode Exit fullscreen mode

As can you see in the examples use of aliases is helpful because you can save time and avoid typos while running a command.
Yes, using an alias may seem complicated, don't worry once you finish reading will look for a command to create your alias.

Creating an alias

There are several ways to create an alias but my recommendation is to work on an isolated file instead of edit several times the .bashrcor .zshrc. Also, if you keep your aliases in a separate file you can version on a gist or some dedicated repository for your configurations.

  1. Go to your home directory: cd $HOME
  2. Create a hidden file easy to identify: touch .custom_alias
  3. Use vi, nano, vim or whatever editor you want to open the file.
  4. Add these aliases (something basic to understand the usage):

    alias h='cd $HOME'
    alias r='reset'
    alias c='clear'
    
  5. Save the current changes

  6. Now you have to edit your .bashrc or .zshrc to load the alias on terminal start, in my case I'm using Oh My ZSH!, so I need to run: vim .zshrc.

  7. In your file move down and add something like:

    [ -f $HOME/.custom_aliases] && . $HOME/.custom_aliases
    
  8. To "reload" the aliases you need to open a new terminal or in the current one run: source .zshrc or source .bashrc, of course it depends on your shell preference.

  9. That's it, now if you move to some path like: ~/projects/2021/technologies/react/my-project/src/Components just to need press h to back to the home path.

Advanced mode

Let's move on to something more complex just to cover the functions as an alias. Of course, you can run inline commands but for specific cases you need to run a set of instructions before reaching the desired.

In an imaginary scenario where you are working with React and you always use the same component structure maybe you go to use your editor to add each file but it would be much easier just run a command to generate the structure, so, you could add a function like this:

# constants to define the output colors for warnings or errors
ERROR='\033[0;31m'
WARNING='\033[1;33m'
SRC='src'

function rcomponent() {
  if [ -z $1 ]
  then
    echo -e "${ERROR}You need specify the component name!"
  else
    if [[ -d $SRC ]]
    then
      if [[ -d "${SRC}/components/${1}" ]]
      then
        echo -e "${WARNING}${1} component already exists!"
      else
        CURRENT_DIRECTORY=$(pwd)
        cd "${SRC}/components" && mkdir $1 && cd $1
        touch index.js $1.js useStyles.js actions.js reducer.js
        cd $CURRENT_DIRECTORY
      fi
    else
      echo -e "${ERROR}${SRC} directory does not exists"
    fi
  fi
}
Enter fullscreen mode Exit fullscreen mode

Now when you need to create a new component just need to run: rcomponent, too long alias for you, ok, let's do it shorter adding a line at the bottom alias rc=rcomponent and now you can run rc in your terminal 🤯.

After change .custom_alias, .zshrc or .bashrcfiles you need to close your terminal or run source .zshrc or source .bashrc, of course now you can add an alias to do that 😂.

My aliases

I will entrust you with my aliases maybe some of them might be useful to you.

# constants to define the output colors for warnings or errors
ERROR='\033[0;31m'
WARNING='\033[1;33m'
SRC='src'

function rcomponent() {
  if [ -z $1 ]
  then
    echo -e "${ERROR}You need specify the component name!"
  else
    if [[ -d $SRC ]]
    then
      if [[ -d "${SRC}/components/${1}" ]]
      then
        echo -e "${WARNING}${1} component already exists!"
      else
        CURRENT_DIRECTORY=$(pwd)
        cd "${SRC}/components" && mkdir $1 && cd $1
        touch index.js $1.js useStyles.js actions.js reducer.js
        cd $CURRENT_DIRECTORY
      fi
    else
      echo -e "${ERROR}${SRC} directory does not exists"
    fi
  fi
}

# allows editing my .zshrc file no care the current path
function my_zsh_edit() {
  vim ~/.zshrc
}

# allows reloading my .zshrc file no care the current path
function my_zsh_reload() {
  source ~/.zshrc
}

# create a git branch prefixed as "feature/" : feature login -> feature/login
function feature() {
  git checkout -b "feature/"$1
}

# create a git branch prefixed as "hotfix/" :  hotfix invalid-token -> hotfix/invalid-token
function hotfix() {
  git checkout -b "hotfix/"$1
}

alias develop='git checkout develop' # move directly to develop branch
alias master='git checkout master' # move directly to master branch
alias here='git rev-parse --abbrev-ref HEAD && git rev-parse --abbrev-ref HEAD | pbcopy' # show and copy the current branch to clipboard
alias commit='git commit -m $@' # creates a commit: commit "initial commit"
alias delete='git branch -D $@' # deletes a branch: delete hotfix/invalid-login
alias undo='git reset --soft HEAD^' # revert the last commit keeping the changes
alias amend_msg='git commit --amend -m $@' # update the commit message: amend_msg "this is a new message"
alias amend_date='git commit --amend --date "$(date -R)" --no-edit' # update the commit timestamp: amend_date
alias amend_files='git commit --amend --no-edit' # add missing files to last commit without change the message: add . then amend_files
alias amend_author='git commit --amend --reset-author --no-edit' # change the author after change the git configuration: git config --global user.email example@email.com then amend_author
alias change='git checkout $@' # allows change branch: change feature/login
alias status='git status' # pretty clear just shorter
alias logtree='git log --graph --oneline --decorate --all'
alias log='git log'
alias pull='git pull'
alias push='git push -u origin $(git rev-parse --abbrev-ref HEAD)' # allow push the current branch directly, instead git push -u origin login just push
alias tc='git rev-list --count $(git rev-parse --abbrev-ref HEAD)' # count all commits in the current branch
alias branch='git checkout -b $@' # creates custom named branch: branch my-feature
alias feature='feature' # function alias
alias merge='git merge $@' # shortcut
alias rebase='git rebase $@' # shortcut
alias add='git add $@' # shortcut
alias rename='git branch -m $@' # allows renaming the current branch: my-feature -> rename my-renamed-feature
alias diff='git diff $@' # shortcut
alias stash='git stash' # shortcut
alias drop_stash='git stash drop $@' # drops specified stash: drop_stash 0
alias stash_file='git stash push -m $@ .' # stash files with a custom message: stash_file "my staged files"
alias pop='git stash pop' # drop the last stash
alias checkout='git checkout $@' # moves to specified branch: checkout feature/Login
alias pick='git cherry-pick $@' # takes the changes from a specified hash commit: pick 86ee11cdfd932a8e858e802f8f6d9367c6f607d5
alias commited='git diff-tree --no-commit-id --name-only -r $(git rev-parse --short HEAD)' # show the commited files
alias clean_repo='git branch | grep -v "release/" | grep -v "master" | xargs git branch -D' # runs a repo clean up just keeping the specified branches
alias c='clear'
alias r='reset'
alias h='cd ~/'
alias rcomponent='rcomponent'
alias ys='yarn start'
alias yb='yarn build'
alias ns='npm run start'
Enter fullscreen mode Exit fullscreen mode

Tip: You can list all your aliases executing alias in your terminal.

This is a list with the basic colors(ANSI codes) to customize your text outputs, there are more configurations but that is another topic.

# Reset
ORIGINAL_COLOR='\033[0m'       # Text Reset

# Regular Colors
BLACK='\033[0;30m'        # Black
RED='\033[0;31m'          # Red
GREEN='\033[0;32m'        # Green
YELLOW='\033[0;33m'       # Yellow
BLUE='\033[0;34m'         # Blue
PURPLE='\033[0;35m'       # Purple
CYAN='\033[0;36m'         # Cyan
WHITE='\033[0;37m'        # White
Enter fullscreen mode Exit fullscreen mode

Closing thoughts

Could they say you're lazy for using aliases? Maybe, but it's a way to be smarter and make better use of your time.
As a recommendation, do not make aliases of commands that you still do not fully understand. Once you fully understand them, you will be ready to start saving some keys.
Don't be afraid of aliases 😄

Top comments (3)

Collapse
 
epszaw profile image
Konstantin Epishev

I love to create some kind of shortcuts for git as aliases. For example:

alias gs='git status'
alias gc='git commit'
alias gp='git pull'
alias gP='git push'

# and so on
Enter fullscreen mode Exit fullscreen mode

It's very similar to my vim mnemonic shortcuts and looks very consistent.

Collapse
 
agusrdz profile image
Agustín Rodríguez

Aliases are wonderful, before I used some very similar to yours but I opted for something short and declarative so that when I shared them with my colleagues, they would be understandable at first glance 🤓

Collapse
 
dland profile image
Dave Land (D-CA)

Aliases, like keyboard shortcuts in a good UI, are amazing productivity accellerators.