This article is aimed at Windows/Mac users.
Most users, more often then not, hate working in the terminal, as they are not used to use it.
They really despise it in favor of shiny graphical user interface.
GUI's sure have they're benefits:
- Icons and visual indicators to help remember software functionality
- Easier to understand for beginners
- Customization
- More flexible than a CLI
CLI drawback's:
- Not as user friendly
- You need to remember commands
- Less flexibility
- Not possible to change appearance (partially true)
But, even all that said, I can't stress enough, that if you really into development, you should know how to use a CLI, and here it's why:
- Speed up your workflow once you get used to it
- Focus on the keyboard rather then mouse and keyboard and save time as you don't have to click around as much
- Performance: GUI require minimal resources compared to nowadays advanced GUI's
- A CLI tool, always works the same way, no matter where you go work or witch machine you use.
- Automation, with tiny simple scripts you can automate most of your boring daily tasks
This might not be enough of a reason to really get into the command line for many, but to demonstrate my points I will go through some of the things I use in a bash terminal and the .bashrc
file.
What is bash
Bash is a Unix based command line tool and has some commands that are not native in Windows such as cat
, grep
, find
, ls
, rm
etc, and it's also available in Windows 10 subsystem (WSL).
As an alternative it can be installed with Git
(gitbash).
.bash_profile
In order to make our life easier later on, we need to edit the .bash_profile
, witch should be in C:/Users/$username$/.bash_profile
, otherwise create it.
Add this to .bash_profile
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
This will allow us to reload the the .bashrc file every time we make a change, so we don't have to kill the terminal each time.
.bashrc
This mysterious file is just a script that will execute automatically whenever a user open a new bash instance.
In windows usually it can be found under C:/Users/$username$/.bashrc
.
If the file do not exist, please create one in bash:
cd C:/Users/*yourUserName*/
nano .bashrc
# you don't want to use nano just run
touch .bashrc
# then open with you favorite editor
Usually the .bashrc
holds our aliases, functions and variables that are available to us, as long as we are in the bash terminal.
Let's open .bashrc
with the editor of your choice, anything works, even though I would recommend using Visual Studio Code or Sublime text.
Please make sure to add this to
.bashrc
before we go any further
# Reload .bashrc
alias reload='source $HOME/.bash_profile'
Save the file, kill the terminal and open a new one.
From now on, we will be able to reload .bashrc
directly from the terminal just typing reload
.
Git alias
# Everyday git commands
alias gs='git status'
alias gf='git fetch'
alias ga='git add .'
alias pu='git pull'
alias ph='git push'
#List branch
alias gb='git branch'
#List all branches, press 'Enter' to scroll the list or 'q' to quit the command
alias gba='git branch -a'
# Git stash
alias st='git stash'
alias stp='git stash pop'
alias std='git stash drop'
# Discard all unstaged changes
alias discard='git checkout -- .'
# Useful alias for misspelling git
alias got='git'
alias gut='git'
alias giy='git'
This is just a taste of what the .bashrc
file can do for us.
Open browser
# Open firefox
alias ff='start firefox'
# Open edge
alias edge='start msedge'
# Open chrome
alias chrome='start chrome'
Open a specific web page
# Open gitLab activity
alias glab='start msedge https://gitlab.yourgitlabinstance.com/dashboard/activity'
# Open edge on localhost
alias loc='start msedge http://localhost:5000'
# Open firefox on production
alias prod='start firefox http://mypord.site.com'
# Open firefox and dev tools on localhost
alias ff-loc='start firefox -devTools http://localhost:5000'
Work with directories
# Moving up directories
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
# Move to home directory
alias h='cd ~'
# Move to desktop directory
alias dk='cd c:/Users/*MyUserName*/Desktop'
# Move to c: disk
alias dc='cd c:'
# Move to my repo directory
alias repo='cd c:/repo/test'
Advanced Git alias
# This command will delete all the tracking information for branches that are on
# your local machine that are not in the remote repository
# but it does not delete your local branches
alias rmb='git remote update --prune'
# List all branch that are gone from the remote repositories
# How to use: gone branchname
alias gone="git branch -vv | awk '/: gone]/{print $1}'"
# This command will list out all of your branches
# and then search for any branches that have the remote tracking
# set to gone then delete them locally
# How to use: gone1 branchname
alias gone1="git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d"
# Print a log for all the commit in the dev branch
# You need to change 'dev' to match your branch
# How to use: dev-log
alias dev-log='git log --full-history --pretty=oneline --date-order --decorate=full --skip=0 --max-count=100 dev '
# List your most recently-used branches
# Please read this article on how to make this work
# https://ses4j.github.io/2020/04/01/git-alias-recent-branches/
alias lb='git lb'
Use function to automate small Git tasks
Commit and push
# Function to fetch, pull, add, commit and push at the same time
# How to use: cap "commit message here"
function commitAndPush() {
git fetch
git pull
git add .
git commit -a -m "$1"
git push
}
alias cap='commitAndPush'
Change branch
# Change branch
# How to use: cb yourBranchName
function changeBranch()
{
if [ "$1" ]; then
eval "git checkout $1 && git fetch && git pull"
else
printf '%s\n' "You need to type a Branch name, will now exit..."
return
fi
}
alias cb='changeBranch'
Commit a bug fix
# Commit a bug fix
# Need to pass 3 parameters: bug number, section and comment
# How to use: cm 47322 'frontend/marketing calendar' 'fix marketing calendar buttons'
# What will look like:
# Bug Fix #47322 - Section: frontend/marketing calendar - fix marketing calendar buttons - Ready for test next build
function commit(){
if [ "$1" ] && [ "$2" ] && [ "$3" ] ; then
git pull
git add .
git commit -a -m "Bug Fix #$1 - Section: $2 - $3 - Ready for test next build"
git push
else
echo -e -ne $(alert 'You need to pass a bug #number,section and comment')
exit 1
fi
}
alias cm='commit'
Create branch
# Create a new branch from existing branch
# How to use: cbf dev master
# This will create a new dev branch from master
function createBranchFrom()
{
if [ "$1" ] && [ "$2" ] ; then
printf "\n\r Creating branch: $1 from \"$2\" branch\n\r"
# Switching to the branch we want to start the new branch
eval "git checkout $2"
# Pull the latest from $2 before creating the new branch
eval "git pull"
# Create the new branch
eval "git checkout -b $1 $2"
eval "git push --set-upstream origin $1"
# Success message
printf "\n\r Success! Created branch: $1 from \"$2\" branch\n\r"
else
printf "\n\r Error in creating the feature branch.\n\r"
printf "\n\r Please provide branch to create and branch to create from.\n\r"
printf "\n\r Example: \"cbf dev master\".\n\r"
fi
}
alias cbf='createBranchFrom'
Delete branch
# Delete branch
# How to use: delb branchName
clear='\e[0m'
RED='\033[0;31m'
alert(){
echo -ne $RED$1$clear
}
function deleteBranch(){
if [ "$1" ] ; then
branchName=$(git branch --show-current 2>&1)
# Ask for branch deletion confirmation
echo #space
echo -ne $(alert "- Are you sure you want to PERMANENTLY DELETE the branch: \"$1\" ? (y,yes or n,no): ")
read yesorno
if [ "$yesorno" = yes ] ; then
echo #space
# Can't delete a branch while you are actively on it
branchName=$(git branch --show-current 2>&1)
if [ "$branchName" = $1 ] ; then
printf "\r Error: You can't delete $1 branch while you are actively on it.\n\r"
printf "\n\r Please change branch if you want to delete $branchName.\n\r"
printf "\n\r Use the command: \"cb branchName\" then try deleting $1 again.\n\r"
else
# Delete branch locally
eval "git branch -d $1"
# Delete branch from remote
eval "git push --delete origin $1"
# Need git fetch to update branch deletion
eval "git fetch -p"
# Try to checkout deleted branch, should return a pathspec error if branch was deleted
printf "\n\r If git return a pathspec error below, it means the feature branch as been deleted correctly.\n\r"
eval "git checkout $1"
fi
elif [ "$yesorno" = no ] || [ "$yesorno" = n ] ; then
printf "\n\rBranch: \"$1\" was NOT DELETED\n\r"
printf "\n\rScript will now exit...\n\r"
else
printf "\n\rNo vailid answer, script will now exit...\n\r"
fi
else
printf "\n\rYou need to pass an existing branch name.\n\r"
printf "\n\rEx: \"delb dev\"\n\r"
printf "\n\rScript will now exit...\n\r"
fi
}
alias delb=deleteBranch
Reverting branch to a previous commit
# https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#4114122
# Reverting The Working Copy to an Older Commit
# How to use: rv-to 1be3c174 (replace 1be3c174 with your commit hash)
# To revert to a commit that's older than the most recent commit:
function revert-to-older-commit(){
# Resets index to former commit;
git reset $1
# Moves pointer back to previous HEAD
git reset --soft HEAD@{1}
git commit -m "Revert to $1"
# Updates working copy to reflect the new commit
git reset --hard
}
alias rv-to=revert-to-older-commit
Take advantage of alias when working with nodejs
You can install node packages and then call them through alias.
Just for example, let's say we want to check out internet connetion speed.
We could use this speed-test package.
You need to have nodejs installed for this to work
npm i -g speed-test
Then in our .bashrc we will add
alias speed='speed-test --bytes -b'
This would be an hypotetic result when typing speed
in the CLI
Ping 31 ms
Download 12 MBps
Upload 2.8 MBps
Another example might be that we want to find the specificity of one of our css selectors.
npm i -g specificity
alias spec='specificity'
Typing spec "ul#nav li.active a"
in the CLI
0,1,1,3
List the node packages we have installed globally along with their version numbers
alias npmg='npm list -g --depth=0'
I know this example is not as useful as it only spare us from writing few words but it's important that you get the drill on how this can be used.
Run scripts from alias
Sometime we might need to run a certain script and we would have to remember the name and the arguments we pass, if we have any, so we assign them to an alias
# Exe file
alias restore='.\nuget.exe restore'
# JS script
alias nm='node menu.js'
# Powershell script using PS7
alias ms='pwsh ./ms-build.ps1'
Other things we can achieve with .bashrc
I will go through some other example that I currently use myself just to show you that the possibility are endless.
ip address and file names below are just a placeholder
Open termux on phone over ssh
# Open termux on phone over ssh
alias tmux='ssh -p 8022 [email protected]'
Open ssh connection to EC2 server in amazon aws
# Open ssh connection to EC2 server in amazon aws
alias ec2=' ssh -i "file.pem" [email protected]'
Find all files
# Find all files
alias only-files='find . -maxdepth 1 -type f'
Run openvpn
# Run openvpn (for this to work you need to open the bash as administrator)
alias aws='openvpn --config C:/Users/*yourUserName*/OpenVPN/config/folder/myvpn.ovpn'
Run man page
# Run man page
function m()
{
"$1" --help | less
}
alias man='m'
Run winget
# Run winget
function winget { cmd.exe /c "winget $1 $2 $3";}
Open .bashrc
# Open .bashrc
function openBashrc()
{
if [ "$user" ]; then
eval "code . c:/Users/$user/.bashrc"
else
echo -e -ne $(alert 'No user name available. Exit the script...')
exit
fi
}
Test the status of a website
# Test the status of a website
alias wss="curl -Is https://odysee.com -L | grep HTTP/"
Find external IP address
# Find external IP address
alias eip="curl ifconfig.me"
Get the weather
# Get the weather
alias wth="curl http://wttr.in/LIVERPOOL,UK"
If you are still not convinced on using the terminal, it's perfectly fine.
I just hope you did find this article useful in some shape or form.