Utilizing aliases

Since my transition to fish I have started using aliases more and more, and I think you should too.

By standard, the .bashrc file has the following piece of code:

1

2

3

if[-f~/.bash_aliases];then

.~/.bash_aliases

fi

In layman’s terms: If the file .bash_aliases exists in the user’s home folder, parse all lines in it as bash commands. This is a perfect container for all your aliases.

fish doesn’t really have a good config file in the beginning, so you’ll have to create your own file. If you’re (planning on) using fish, I highly recommend you to use my config.fish file which you can download from Scripts and Stuff, which comes with the following snippet:

1

2

3

4

# Load aliases

if[-f$HOME/.config/fish/aliases.fish]

.$HOME/.config/fish/aliases.fish

end

This does exactly the same as the snippet from .bashrc mentioned above, except it searches for the file aliases.fish in fish‘s configuration folder instead of .bashrc in the home folder. Since I use fish as my primary interactive shell, I won’t be giving much guidance regarding bash, but you should be able to translate most of my code fairly easily.

My aliases.fish looks like this:

aliases.fish

1

2

3

4

5

6

7

8

9

10

11

12

13

# Global aliases

aliasc'cd ..'# Up one folder

alias-'cd -'# Last folder

aliasbc'bc -ql'# No welcome message, improved functions

aliasiftop'iftop -B'# Measure in Bytes instead of Bits

aliasping'ping -c3'# Default to 3 attemps instead of unlimited

aliastmux'tmux -2'# Force 256 colors

alias:e'vim'# Launch vim with ":e"

aliascrontab'crontab -i'# No accidental removals

functionplease# Sudo last command

eval sudo$history[1]

end

I personally find all my aliases self-explanatory, but I put comments everywhere for the sake of it.

Changing to the parent directory is a very common, but surprisingly annoying, task. I’ve seen many people having “..” and/or “cd..” aliased to “cd ..“, but I prefer a simple c. Since I have switched from screen to tmux I’ve gotten used to typing something as simple as tmux -L irssi attach. For some reason, tmuxdoesn’t want me to use 256 colors, so I have to force it with the -2 switch.

When I was new to crontab I was very fond of using crontab -r for absolutely no reason at all. I can’t even remember why, but I think I wanted it to simply print the current crontab, and -r would mean “read” and be the correct switch. It was not. crontab -r removes the user’s crontab immediately and without warning. To stop myself from screwing up further, I’ve added a backup task of crontab, in crontab, should I ever delete it again, but I have also made crontab aliased to crontab -i. This means I will be asked for a confirmation if I attempt to use crontab -ragain.

As you see, the last three lines are not aliases per se, but rather a single function. Alises are after all very simple functions. Anyway, the function please repeats the last command with sudo prepended, like so:

This is a very useful function, commonly used by me.

Of course, everyone has their preferences, but these are my favorite aliases and I hope I have inspired you somewhat.

Leave a comment if you have any suggestions or ideas for new aliases that you wish to share!