[Bash]: Setting Alias on Terminal

bash

04/16/2019


Start

Bash (Terminal's default command line interpreter) allows you to create aliases. You can think of aliases as nothing more than a keyboard shortcut.

To add/modify alias on terminal:

Creating alias is simple. On command line, you can type

BASH
alias ll="ls -alh"

Creating this alias allows user to type in 'll' instead of 'ls -alh' on a command-line (to view list of all itmes with its file size). However, this is not permanent; once the terminal is quit, this alias will be removed. Unless:

Setting a permanent alias

By using your favorite text editor, open .bash_profile in your home directory. On Windows, you would need to have git bash installed.

BASH
vim ~/.bash_profile
# OR
nano ~/.bash_profile

Using zsh? Create alias inside ~/.zshrc

In this file you can simply add in

BASH
alias ll="ls -alh"

and save it. This will work after you restart your terminal

BASH
alias

By typing 'alias' on command line shows you what alias you currently have (not functions)

Another example:

BASH
function postup() {
git add .
git commit -m "[UPDATE]: updated post"
git push
}
function pu() {
git add .
git commit -m "[UPDATE]: updated post"
git push
}

You can also create a function to have a multiple command prompt in one keyword. I use this commonly when modifying a post on this page.

Usage:

BASH
postup

is equivalent to typing:

BASH
git add .
git commit -m "[UPDATE]: updated post"
git push

Using a different commit message:

BASH
function lazy() {
git add .
git commit -a -m "$1"
git push
}

Usage:

BASH
lazy "added a new post"

More useful aliases

BASH
# Clears the screen
alias c="clear"
BASH
# Moves your directory to your desired location. I use this often to get to my project folder
alias movdir="/your/favorite/directory"
BASH
# Open the folder in finder
alias o="open ."

Above two commands can be a good combination; movedir -> o

To open a folder on Windows in command line, use start . instead of open .

BASH
# Shows how long the system has been up
alias ut="uptime"
BASH
alias dev="npm run dev"
BASH
# Push to heroku production
alias hero="git push heroku master"
BASH
alias sql="mysql -u root"
BASH
function newrepo() {
git init
git add .
git commit -m "[INITIAL]: initial commit 🔥"
git remote add origin $1
git push -u origin master
}

Alias to shorten categorized commit messages

Refer to Categorize your GitHub Commit Messages

BASH
function add() {
git add .
git commit -a -m "[ADD]: $1"
git push
}

Note: Make sure to include message inside "" on command-line. Ex) add "some message"

BASH
function update() {
git add .
git commit -a -m "[UPDATE]: $1"
git push
}
BASH
function update() {
git add .
git commit -a -m "[UPDATE]: $1"
git push
}
BASH
function styles() {
git add .
git commit -a -m "[STYLES]: $1"
git push
}

WRITTEN BY

Keeping a record