Beginner's Guide To Shell Aliases With Examples

⌂ home      ⇧ one level up
Mon May 24, 2021 · 571 words · 3 min
Tags :  shell


*nix shell is a powerful tool that can help you go about your work easily. At the lowest level, it can be thought of as an interpreted language (like python). However, 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"

So, for the above example, I can write

alias w="wget"

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

Some Useful Aliases I Keep

Text Editor

alias n="nvim"

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"

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"

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"

I simply use l to list files in long format.

cat -> bat

alias cat="bat"

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"

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

Copy to Clipboard

alias cpy="xclip -sel clip"

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

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\""

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


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

blog · about · resume · github · kde invent · endeavour · twitter · email · home