The Power of Aliases

Aug 12, 2024

An alias is the ability to make a very long, often complicated and usually very powerful command be very simple.

Aliases vs Functions

So an alias can be only really used to perform one command or multiple but you wont be able to use arguments. This is where functions come in, bear in mind that functions can be so much more than what we will discuss below but we will try to keep things simple. So a function can run several commands with arguments.

Aliases

Bellow is an example of an alias:

alias kittyupdate="curl -L https://sw.kovidgoyal.net/kitty/installer.sh | sh /dev/stdin"

It needs to start with "alias" followed by the command you will type in this case "kittyupdate" and then followed by the command you making an alias of.

Another useful alias or list of aliases that I personally use all the time are:

# Quick access to the .zshrc, .bashrc, .vimrc and init.lua
alias zshrc='${=EDITOR} ~/.zshrc' 
alias bashrc='${=EDITOR} ~/.bashrc' 
alias vimrc='${=EDITOR} ~/.vimrc' 
alias init='${=EDITOR} ~/.config/nvim/init.lua' 

For those to work you will need to set your editor, for example:

EDITOR=nvim

Functions

Below is an example of a simple but powerful function:

# "proton" tricks
function pt() { WINEPREFIX="$@" winetricks; }
        

In this example we are telling winetricks to run in a specific place, if you don't care about winetricks or gaming then that's fair but stick with me. Just like the alias its best to start with "function" then define the name of the function "pt()" then we define the commands. The "$@" will be what you type into the terminal.

It will look something like this:

pt path/to/games/pfx

Whereas without the function you would need to type this:

WINEPREFIX="path/to/games/pfx" winetricks

As you can see the function is a much faster way of running this and instead of needing to remember all of that you only need to remember "pt".

That last function essentially ran one command, if you want a simple function that runs multiple commands you only meed to define the function then enter your first command and end the line with ";" and move onto the next command.

Example:

function spicetify-update() {
    spicetify update;
    spicetify restore backup apply;
    sudo chmod a+wr /var/lib/flatpak/app/com.spotify.Client/x86_64/stable/active/files/extra/share/spotify;
    sudo chmod a+wr -R /var/lib/flatpak/app/com.spotify.Client/x86_64/stable/active/files/extra/share/spotify/Apps;
    spicetify apply;
}
        

As you can tell that without this function, properly updating that application is a pain.

A more complex function is the ability to make a new directory and immediately move into it:

# mkcd - mkdir & cd combo
function mkcd {
  if [ ! -n "$1" ]; then
    echo "Enter a directory name"
  elif [ -d $1 ]; then
    echo "`$1' already exists"
  else
    mkdir $1 && cd $1
  fi
}        

I am not going to fully explain this one and instead encourage you to go and learn more about functions if you wish to learn more. This is a great resource for that www.shellscript.sh and I will also link you to my zshrc so you can see that as an example.