DEV Community

Cover image for Beginner's Guide To Shell Aliases With Examples
Snehit Sah
Snehit Sah

Posted on • Originally published at flyingcakes85.github.io

Beginner's Guide To Shell Aliases With Examples

Introduction

*nix shell is a powerful tool that can help you go about your work easily. At the lowest level, it can be It can seamlessly run binaries and this power combined with shell grammar, you can write scripts to automate processes on your system.

Shell has this feature where you can define aliases to existing commands. So, if I alias w to wget, then every time I run w, it will be interpreted as wget.

Defining Aliases

Shell aliases can be easily defined by the following syntax:

alias <keyword>="some long command"
Enter fullscreen mode Exit fullscreen mode

So, for the above example, I can write

alias w="wget"
Enter fullscreen mode Exit fullscreen mode

This line goes in your ~/.bashrc or ~/.zshrc depending on the shell you use. After adding an alias, you need to reload your shell config. This can be done simply by restarting your teminal, or if you don't want to close the current running terminal, you can run the following command:

source ~/.zshrc   # bash users replace with ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Some Useful Aliases I Keep

Text Editor

alias n="nvim"
Enter fullscreen mode Exit fullscreen mode

I am quite often in the terminal messing with configuration files and all, meaning I regularly open the text editor. So, I created a single character alias to launch nvim.

Git Commands

I alias the common Git commands. Some people like to go overboard and add lots of Git related aliases. Do whatever you are comfortable with.

alias gcm="git checkout main"
alias gp="git pull"
alias gpu="git push"
Enter fullscreen mode Exit fullscreen mode

Aliases for Development Tools

I first did this when I was learning Rust.

alias ccl="cargo clean"  # cc conflicts with gcc/cc
alias cch="cargo check"
alias cb="cargo build"
alias cr="cargo run"
alias ca="cargo add"
alias rn="rustup override set nightly"
Enter fullscreen mode Exit fullscreen mode

You can set aliases for whichever language or build tools you generally use.

ls commands

alias l="ls -alh --color=tty"
alias ls="ls --color=tty"
Enter fullscreen mode Exit fullscreen mode

I simply use l to list files in long format.

cat -> bat

alias cat="bat"
Enter fullscreen mode Exit fullscreen mode

bat is a cat clone with host of new features. The major reason why I use bat is the syntax highlight and line numbering. Using Linux distros for a few years now, I am used to running cat in the terminal, so I aliased it to bat.

Clear Terminal

alias c="clear"
Enter fullscreen mode Exit fullscreen mode

Yes I am too lazy to type clear so I aliased it to c.

Copy to Clipboard

alias cpy="xclip -sel clip"
Enter fullscreen mode Exit fullscreen mode

Often you need to copy the output of a command on to the clipboard. If the output is short, you can use your mouse to select text and press Ctrl+Shift+C. However, if the output is long, good luck copying it.

With the above alias, I pipe the output of a command to cpy and it will be copied on to the clipboard.

inxi -F | cpy   # output of inxi -F is copied to clipboard
Enter fullscreen mode Exit fullscreen mode

Youtube-dl Command

alias ytmp3="youtube-dl --extract-audio --audio-format mp3 --audio-quality 256K --add-metadata --metadata-from-title \"%(artist)s - %(title)s\" -o \"%(title)s.%(ext)s\""
Enter fullscreen mode Exit fullscreen mode

This downloads the audio stream from the supplied link and add metadata to file in Artist - Title format.

Conclusion

These were just some examples of the aliases I use. You can set more such aliases to make your workflow faster.

You can follow my personal blog at https://flyingcakes85.github.io/blog/

Follow me on GitHub : flyingcakes85

Cover photo by Anete Lusina from Pexels

Top comments (0)