An Interest In:
Web News this Week
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
Increasing your productivity using aliases
To the guy who invented zero... thanks for nothing.
Well, seriously, zero is very important and aliases too, and that's why I'm going to explain how you can add them to your workflow, if you're not already using them, to increase your productivity.
What is an alias
It is just a shortcut, but an important one. Take a second to think in a command that you use every day, for example, cd
a basic one to change the directory.
Suppose you are on this path:
~/projects/2021/technologies/react/my-project/src/Components
Now, what are your options to back to the projects
directory? Well, you can do some basic like this:
cd ../cd ../cd ../cd ../cd ../cd ../# orcd ../../../../../../# orcd $HOME/projects# orcd ~/projects
an easy way could be something like:
projects
internally the real command could be defined as:
alias projects='cd ~/projects'
As can you see in the examples use of aliases is helpful because you can save time and avoid typos while running a command.
Yes, using an alias may seem complicated, don't worry once you finish reading will look for a command to create your alias.
Creating an alias
There are several ways to create an alias but my recommendation is to work on an isolated file instead of edit several times the .bashrc
or .zshrc
. Also, if you keep your aliases in a separate file you can version on a gist
or some dedicated repository for your configurations.
- Go to your home directory:
cd $HOME
- Create a hidden file easy to identify:
touch .custom_alias
- Use
vi
,nano
,vim
or whatever editor you want to open the file. Add these aliases (something basic to understand the usage):
alias h='cd $HOME'alias r='reset'alias c='clear'
Save the current changes
Now you have to edit your
.bashrc
or.zshrc
to load the alias on terminal start, in my case I'm using Oh My ZSH!, so I need to run:vim .zshrc
.In your file move down and add something like:
[ -f $HOME/.custom_aliases] && . $HOME/.custom_aliases
To "reload" the aliases you need to open a new terminal or in the current one run:
source .zshrc
orsource .bashrc
, of course it depends on your shell preference.That's it, now if you move to some path like:
~/projects/2021/technologies/react/my-project/src/Components
just to need pressh
to back to the home path.
Advanced mode
Let's move on to something more complex just to cover the functions as an alias. Of course, you can run inline commands but for specific cases you need to run a set of instructions before reaching the desired.
In an imaginary scenario where you are working with React and you always use the same component structure maybe you go to use your editor to add each file but it would be much easier just run a command to generate the structure, so, you could add a function like this:
# constants to define the output colors for warnings or errorsERROR='\033[0;31m'WARNING='\033[1;33m'SRC='src'function rcomponent() { if [ -z $1 ] then echo -e "${ERROR}You need specify the component name!" else if [[ -d $SRC ]] then if [[ -d "${SRC}/${1}" ]] then echo -e "${WARNING}${1} component already exists!" else CURRENT_DIRECTORY=$(pwd) cd $SRC && mkdir $1 && cd $1 touch index.js $1.js useStyles.js actions.js reducer.js cd $CURRENT_DIRECTORY fi else echo -e "${ERROR}${SRC} directory does not exists" fi fi}
Now when you need to create a new component just need to run: rcomponent
, too long alias for you, ok, let's do it shorter adding a line at the bottom alias rc=rcomponent
and now you can run rc
in your terminal .
After change
.custom_alias
,.zshrc
or.bashrc
files you need to close your terminal or runsource .zshrc
orsource .bashrc
, of course now you can add an alias to do that .
My aliases
I will entrust you with my aliases maybe some of them might be useful to you.
# constants to define the output colors for warnings or errorsERROR='\033[0;31m'WARNING='\033[1;33m'SRC='src'function rcomponent() { if [ -z $1 ] then echo -e "${ERROR}You need specify the component name!" else if [[ -d $SRC ]] then if [[ -d "${SRC}/${1}" ]] then echo -e "${WARNING}${1} component already exists!" else CURRENT_DIRECTORY=$(pwd) cd $SRC && mkdir $1 && cd $1 touch index.js $1.js useStyles.js actions.js reducer.js cd $CURRENT_DIRECTORY fi else echo -e "${ERROR}${SRC} directory does not exists" fi fi}# allows editing my .zshrc file no care the current pathfunction my_zsh_edit() { vim ~/.zshrc}# allows reloading my .zshrc file no care the current pathfunction my_zsh_reload() { source ~/.zshrc}# create a git branch prefixed as "feature/" : feature login -> feature/loginfunction feature() { git checkout -b "feature/"$1}# create a git branch prefixed as "hotfix/" : hotfix invalid-token -> hotfix/invalid-tokenfunction hotfix() { git checkout -b "hotfix/"$1}alias develop='git checkout develop' # move directly to develop branchalias master='git checkout master' # move directly to master branchalias here='git rev-parse --abbrev-ref HEAD && git rev-parse --abbrev-ref HEAD | pbcopy' # show and copy the current branch to clipboardalias commit='git commit -m $@' # creates a commit: commit "initial commit"alias delete='git branch -D $@' # deletes a branch: delete hotfix/invalid-loginalias undo='git reset --soft HEAD^' # revert the last commit keeping the changesalias amend_msg='git commit --amend -m $@' # update the commit message: amend_msg "this is a new message"alias amend_date='git commit --amend --date "$(date -R)" --no-edit' # update the commit timestamp: amend_datealias amend_files='git commit --amend --no-edit' # add missing files to last commit without change the message: add . then amend_filesalias amend_author='git commit --amend --reset-author --no-edit' # change the author after change the git configuration: git config --global user.email [email protected] then amend_authoralias change='git checkout $@' # allows change branch: change feature/loginalias status='git status' # pretty clear just shorteralias logtree='git log --graph --oneline --decorate --all'alias log='git log'alias pull='git pull'alias push='git push -u origin $(git rev-parse --abbrev-ref HEAD)' # allow push the current branch directly, instead git push -u origin login just pushalias tc='git rev-list --count $(git rev-parse --abbrev-ref HEAD)' # count all commits in the current branchalias branch='git checkout -b $@' # creates custom named branch: branch my-featurealias feature='feature' # function aliasalias merge='git merge $@' # shortcutalias rebase='git rebase $@' # shortcutalias add='git add $@' # shortcutalias rename='git branch -m $@' # allows renaming the current branch: my-feature -> rename my-renamed-featurealias diff='git diff $@' # shortcutalias stash='git stash' # shortcutalias drop_stash='git stash drop $@' # drops specified stash: drop_stash 0alias stash_file='git stash push -m $@ .' # stash files with a custom message: stash_file "my staged files"alias pop='git stash pop' # drop the last stashalias checkout='git checkout $@' # moves to specified branch: checkout feature/Loginalias pick='git cherry-pick $@' #takes the changes from a specified hash commit: pick 86ee11cdfd932a8e858e802f8f6d9367c6f607d5alias commited='git diff-tree --no-commit-id --name-only -r $(git rev-parse --short HEAD)' #show the commited filesalias clean_repo='git branch | grep -v "release/" | grep -v "master" | xargs git branch -D' # runs a repo clean up just keeping the specified branchesalias c='clear'alias r='reset'alias h='cd ~/'alias rcomponent='rcomponent'alias ys='yarn start'alias yb='yarn build'alias ns='npm run start'
Tip: You can list all your aliases executing
alias
in your terminal.
This is a list with the basic colors(ANSI codes) to customize your text outputs, there are more configurations but that is another topic.
# ResetORIGINAL_COLOR='\033[0m' # Text Reset# Regular ColorsBLACK='\033[0;30m' # BlackRED='\033[0;31m' # RedGREEN='\033[0;32m' # GreenYELLOW='\033[0;33m' # YellowBLUE='\033[0;34m' # BluePURPLE='\033[0;35m' # PurpleCYAN='\033[0;36m' # CyanWHITE='\033[0;37m' # White
Closing thoughts
Could they say you're lazy for using aliases? Maybe, but it's a way to be smarter and make better use of your time.
As a recommendation, do not make aliases of commands that you still do not fully understand. Once you fully understand them, you will be ready to start saving some keys.
Don't be afraid of aliases
Original Link: https://dev.to/agusrdz/increasing-your-productivity-using-aliases-3o3o
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To