← Back to all posts

Improving your Developer Experience with ZSH alias'

Improving your Developer Experience with ZSH alias'

5 min read

Photo Credit: Allison & Rupert Photography

Introduction

As software engineers we have a lot of different terminal commands we need to remember and often I find myself repeating the same commands. This is what my shell file looks like.

If you haven't created a Zsh file yet, I would recommend creating it and using vim add the command to open the file in VS Code. That will make adding more commands way easier.

Setup steps

In your terminal enter:

• vim ~/.zshrc

• Press "i" in vim (It should say 'insert')

• Paste in the following commands:

• alias edit='code $HOME/.zshrc'

• alias reload='source ~/.zshrc'

• Press 'Esc' to exit insert mode

• Enter 'wq!' to save

• Close and restart the terminal

• You should now be able to enter 'code' in your terminal and it will open in VS Code. Copy and paste the commands below and save.

• Enter the 'reload' command and you will now be able to enter all the commands and use them.

Commands

1# Terminal Color choices
2# Bold
3BBlack='\033[1;30m'       # Black
4BRed='\033[1;31m'         # Red
5BGreen='\033[1;32m'       # Green
6BYellow='\033[1;33m'      # Yellow
7BBlue='\033[1;34m'        # Blue
8BPurple='\033[1;35m'      # Purple
9BCyan='\033[1;36m'        # Cyan
10BWhite='\033[1;37m'       # White
11
12# Underline
13UBlack='\033[4;30m'       # Black
14URed='\033[4;31m'         # Red
15UGreen='\033[4;32m'       # Green
16UYellow='\033[4;33m'      # Yellow
17UBlue='\033[4;34m'        # Blue
18UPurple='\033[4;35m'      # Purple
19UCyan='\033[4;36m'        # Cyan
20UWhite='\033[4;37m'       # White
21
22# Background
23On_Black='\033[40m'       # Black
24On_Red='\033[41m'         # Red
25On_Green='\033[42m'       # Green
26On_Yellow='\033[43m'      # Yellow
27On_Blue='\033[44m'        # Blue
28On_Purple='\033[45m'      # Purple
29On_Cyan='\033[46m'        # Cyan
30On_White='\033[47m'       # White
31
32# High Intensity
33IBlack='\033[0;90m'       # Black
34IRed='\033[0;91m'         # Red
35IGreen='\033[0;92m'       # Green
36IYellow='\033[0;93m'      # Yellow
37IBlue='\033[0;94m'        # Blue
38IPurple='\033[0;95m'      # Purple
39ICyan='\033[0;96m'        # Cyan
40IWhite='\033[0;97m'       # White
41
42# Bold High Intensity
43BIBlack='\033[1;90m'      # Black
44BIRed='\033[1;91m'        # Red
45BIGreen='\033[1;92m'      # Green
46BIYellow='\033[1;93m'     # Yellow
47BIBlue='\033[1;94m'       # Blue
48BIPurple='\033[1;95m'     # Purple
49BICyan='\033[1;96m'       # Cyan
50BIWhite='\033[1;97m'      # White
51
52# High Intensity backgrounds
53On_IBlack='\033[0;100m'   # Black
54On_IRed='\033[0;101m'     # Red
55On_IGreen='\033[0;102m'   # Green
56On_IYellow='\033[0;103m'  # Yellow
57On_IBlue='\033[0;104m'    # Blue
58On_IPurple='\033[0;105m'  # Purple
59On_ICyan='\033[0;106m'    # Cyan
60On_IWhite='\033[0;107m'   # White
61
62# General MacOS commands
63alias reload='source ~/.zshrc'
64alias zshconfig='code ~/.zshrc'
65
66alias ll='ls -lah'
67alias cls='clear'
68alias ls='ls --color=auto'
69alias lh='ls -d .* --color=auto'
70
71alias ..='cd ../'
72alias ~='cd ~/'
73
74export ANDROID_SDK_ROOT=$HOME/Library/Android/sdk
75export PATH=$PATH:$ANDROID_SDK_ROOT/emulator
76export PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools
77export PATH=${PATH}:/usr/local/mysql/bin
78export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$PATH"
79export PATH=/opt/homebrew/bin:$PATH
80
81
82# Git Commands
83alias stat='git status'
84alias pull='git pull origin'
85alias pullm='git pull origin master'
86alias pullt='git pull origin test'
87alias pulld='git pull origin develop'
88
89function get_branch() {
90    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/\1/p'
91}
92
93function add() {
94    git add -A
95    git status
96}
97
98function commit() {
99  project=""
100  jiraNumber=""
101  message=""
102
103  echo "${BBlue}Select Project: "
104  select opts in "MMR" "RC"; do
105    case $opts in
106      "MMR" ) 
107        project="MMR"
108        break;;
109      "RC" ) 
110        project="RC"
111        break;;
112      "exit" ) 
113        echo "Exiting"
114        break;;
115      * ) echo "invalid option";;
116    esac
117  done
118
119
120  echo -n "${BBlue}Enter Jira Number: "
121  read -r jiraNumber
122
123  echo -n "${BBlue}Enter Commit Number: "
124  read -r message
125
126  git commit -m "$project-$jiraNumber: $message"
127  echo "${BBlue}$project-$jiraNumber: '$message' successfully committed"
128}
129
130function push() {``
131    echo "running... git push origin $(get_branch)"
132    git push origin $(get_branch)
133}
134
135function rebase-push() {
136  echo "running... git push origin $(get_branch)"
137  git push -f origin $(get_branch)
138}
139
140function newb() {
141  project=""
142  jiraNumber=""
143  branchType=""
144  branchName=""
145
146  echo "${BBlue}Select Project: "
147  select opts in "MMR" "RC"; do
148    case $opts in
149      "MMR" ) 
150        project="MMR"
151        break;;
152      "RC" ) 
153        project="RC"
154        break;;
155      "exit" ) 
156        echo "Exiting"
157        break;;
158      * ) echo "invalid option";;
159    esac
160  done
161  
162  echo -n "${BBlue}Enter Jira Number: "
163  read -r jiraNumber
164
165  echo "${BBlue}Select Branch Type: "
166  select opts in "feature" "bugfix" "hotfix" "release" "exit"; do
167    case $opts in
168      "feature" ) 
169        branchType="feature"
170        break;;
171      "bugfix" ) 
172        branchType="bugfix"
173        break;;
174      "hotfix" ) 
175        branchType="hotfix"
176        break;;
177      "release" ) 
178        branchType="release"
179        break;;
180      "exit" ) 
181        echo "Exiting"
182        break;;
183      * ) echo "invalid option";;
184    esac
185  done
186
187  echo -n "${BBlue}Enter Branch Name: "
188  read -r branchName
189  
190  echo "${BBlue}Creating new branch with name: $branchType/$project-$jiraNumber-$branchName"
191  git checkout -b "$branchType/$project-$jiraNumber-$branchName"
192}
193
194function renameb() {
195  if [ $# -lt 3 ]
196  then
197    echo "THIS IS RISKY: BACK UP LOCAL CODE FIRST! > Usage: $funcstack[1] <Old-Branch-Name> <New-Branch-Name>"
198    return
199  fi
200
201  git branch -m $1 $2
202  git push origin --delete $1
203  git branch --unset-upstream $2
204  git push origin $2
205  git push origin -u $2
206}
207
208function rebase() {
209  git pull --rebase
210}
211
212function continue() {
213  git rebase --continue
214}
215
216# Frontend Commands
217dev() {
218  echo -e "${BBlue}Starting frontend server"
219  npm run dev
220}
221
222start() {
223  echo -e "${BBlue}Starting frontend server"
224  npm run start
225}
226
227alias reinstall='rm -rf node_modules/ && rm package-lock.json && npm ci'
228
229
230# Python Django Commands
231pyinstall() {
232  echo -e "${BBlue}Installing django dependencies"
233  python3 -m pip install -r requirements.txt
234}
235
236pyenv() {
237  echo -e "${BBlue}Starting python virtual environment"
238  source venv/bin/activate
239}
240
241pyrun() {
242  echo -e "${BBlue}Starting django server"
243  python3 manage.py runserver
244}
245
246pymigrate() {
247  echo -e "${BBlue}Making django migration files"
248  python3 manage.py makemigration
249
250  echo -e "${BBlue}Migrating migration files to DB"
251  python3 manage.py migrate
252}
253
254function help() {
255    
256    echo -e "\n"
257    echo "---------------------------------------------------------------------------------------------------------------------------"
258    echo "Rupert's Bash Scripts --Help"
259    echo -e "\n"
260    echo -e "Command: pulld\n Args: None\n Result: Does a git fetch and pull of dev branch\n\n"
261    echo -e "Command: pullt\n Args: None\n Result: Does a git fetch and pull of test branch\n\n"
262    echo -e "Command: pullm\n Args: None\n Result: Does a git fetch and pull of master branch\n\n"
263    echo -e "Command: add\n Args: None\n Result: Adds all files to Git, Returns git status\n\n"
264    echo -e "Command: commit\n Args: <Jira Number> <Commit Message>\n Result: Commits files with prefix 'project-'\n\n"
265    echo -e "Command: push\n Args: None\n Result: Set's local branch as upstream branch, pushes to remote\n\n"
266    echo -e "Command: newb\n Args: <Jira Number> <Type> <New-Branch-Name>\n Result: Creates a new branch with prefix 'project-'\n\n"
267    echo -e "Command: renameb\n Args: <Old-Branch-Name> <New-Branch-Name>\n Result: Renames local and remote branch\n\n"
268    echo -e "Command: rebase\n Args: None\n Result: Performs a git rebase\n\n"
269    echo -e "Command: continue\n Args: None\n Result: Once rebase merge conflicts have been resolved it continues the rebase\n\n"
270    echo -e "Command: reinstall\n Args: None\n Result: Removes node_modules, package-lock and runs npm ci\n\n"
271    echo -e "Command: myip\n Args: None\n Result: Returns your IP address\n\n"
272    echo -e "Command: edit\n Args: None\n Result: Opens the .zshrc file to edit these bash scripts\n\n"
273    echo -e "Command: speedtest\n Args: None\n Result: Runs the speedtest-cli to get download/ upload speeds"
274
275    echo "---------------------------------------------------------------------------------------------------------------------------"
276    echo -e "\n"
277}
278
279joke() {
280  curl -H "Accept: text/plain" https://icanhazdadjoke.com/
281}
282function speedtest() {
283  speedtest-cli
284}
285alias myip='curl http://ipecho.net/plain; echo'
286alias edit='code $HOME/.zshrc'

Conclusion

Make sure to change the command outputs to suit you and hopefully they help speed up your development workflow.

Thanks for reading!