PrinzPiuz

Software Engineer | Ziff Davis | Cochin


A Small Tweak To Enhance Your Shell

Published March 18, 2023 🕐 2 Mins

This is a short blog about, How a small enhancement in my .zshrc file helped me to do some repetitive steps automatically. So coming to the topic, Let me explain the situation, Te problem here was navigating between each of the project folders, also to activate virtual environments in the case of python projects. I need to remember all the paths of projects folder and their virtual environments in case of python projects. Sounds boring?. So I did some tweaks in .zshrc to make things easy. I am using zsh as my shell, There are other shells available like bash, and fish, bash is the default shell in most operating systems. I am using zsh because it brings some extra features to the terminal. And every shell will be having corresponding rc files located ~/, It will be a hidden file. You can see it by doing a ls -al ~/. Its functionality is, As the name suggests rc is short for Run Command, which means it runs commands we wrote in the rc file every time we open a new terminal. Since I am using zsh my rc will be seen as .zshrc. So here is what I did to solve the above-mentioned problem.

#to auto alias projects in hobby dir
for repo in $(ls ~/projects/hobby)
do
  alias $repo="cd ~/projects/hobby/$repo"
done

#to auto alias activate command for virtual envs in venv directory
for venv in $(ls ~/venvs)
do
  alias $venv"env"="source ~/venvs/$venv/bin/activate && $venv"
done

This is a shell script, what happening here is, I am creating alias for every directory in my hobby projects folder, so that if I enter the name of my hobby projects folder in terminal, I can change my location into that directory. Also second part is more interesting, I am appending env to the last section of virtual environments folders and aliasing them, So when I write <project name> + env, it will activate virtual environment and then change location to that folder, since zsh has built in auto-completion for alias, after typing one or two letters of project name and hitting tab, zsh will show available options.

Here is a short screencast of how it actually works.


Further References