DEV Community

Gokuldroid
Gokuldroid

Posted on • Originally published at codefromdude.com

Local aliases using zen aliases

We can configure aliases very easily in bash or zsh. but those aliases are global to all directory. sometimes we need aliases or commands only for some directories alone. this plugin makes the process easy. those aliases will be available only to that particular directory. aliases will be loaded when you cd into the directory and unloaded once you leave the directory.


Configure in zsh

clone plugin from github

git clone https://github.com/Gokuldroid/zen-alias ~/.oh-my-zsh/plugins/zen-alias
Enter fullscreen mode Exit fullscreen mode

check if the plugin is cloned correctly

ls ~/.oh-my-zsh/plugins
Enter fullscreen mode Exit fullscreen mode

open .zshrc

open ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

or

vim ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

add zen-alias like this

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
    git
    zen-alias
)
Enter fullscreen mode Exit fullscreen mode

updating the plugin

cd <cloned directory> && git pull
Enter fullscreen mode Exit fullscreen mode

Configure in bash

copy the file to root directory or somewhere in your system.

curl -S https://raw.githubusercontent.com/Gokuldroid/zen-alias/main/zen-alias.plugin.zsh > ~/.zen-alias.bash
Enter fullscreen mode Exit fullscreen mode

source the file in bashrc

open ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

add this line at the end of the bashrc

source ~/.zen-alias.bash
Enter fullscreen mode Exit fullscreen mode

Aliases syntax

Create .aliases file in the root directory of the project. add your per project aliases like this. You can find a sample file here.

yc: yarn w @zen-alias/client
ys: yarn w @zen-alias/server
ycommon: yarn w @zen-alias/common
ybg: yarn w @zen-alias/bg-jobs
yb: yarn build
yw: yarn watch
Enter fullscreen mode Exit fullscreen mode

Tips

Excluding the .aliases file in git tree

incase if you don't want others to use your aliases, you can add it global .gitignore or project .gitignore

cd ~
touch .gitignore_global
git config --global core.excludesfile ~/.gitignore_global
echo ".aliases" > ~/.gitignore_global
Enter fullscreen mode Exit fullscreen mode

Init command for a directory.

we might want to execute some script as soon as we enter the project. like updating local branch or setting up nvm or rvm version etc. zen-alias provides an easy way for this.

define a zen-alias like this,

init_zen_dir: nvm use && rvm use
Enter fullscreen mode Exit fullscreen mode

Top comments (0)