Beginner's Guide To Shell Aliases With Examples
Tags: bash, unix, linux
Published: 2021-05-24 07:09:42

## 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:

```sh
alias <keyword>="some long command"
```

So, for the above example, I can write

```sh
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:

```sh
source ~/.zshrc   # bash users replace with ~/.bashrc
```

## Some Useful Aliases I Keep

### Text Editor

```sh
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.

```sh
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.

```sh
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

```sh
alias l="ls -alh --color=tty"
alias ls="ls --color=tty"
```

I simply use `l` to list files in long format.

### cat -> bat

```sh
alias cat="bat"
```

[bat](https://github.com/sharkdp/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

```sh
alias c="clear"
```

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

### Copy to Clipboard

```sh
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.

```sh
inxi -F | cpy   # output of inxi -F is copied to clipboard
```

### Youtube-dl Command

```sh
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.

## 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/](https://flyingcakes85.github.io/blog/)

Follow me on GitHub : [flyingcakes85](https://github.com/flyingcakes85)

Cover photo by [Anete Lusina](https://www.pexels.com/@anete-lusina?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels) from [Pexels](https://www.pexels.com/photo/crop-hacker-silhouette-typing-on-computer-keyboard-while-hacking-system-5240547/?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels)