August 9, 2023

Create a permanent alias for a command

Create an alias

To create an alias run:

$ alias youralias='thecommandyouwanttocreateanaliasfor'

For example:

$ todo='vi /home/username/Documents/todo.txt' 

will create an alias named todo that will open the file todo.txt in Vim. In other words, once you have created the alias, when you want to open todo.txt with Vim, run:

$ todo

But: you will notice that when you quit the shell (a.k.a. the terminal), and open it again, the alias no longer exists.

Make an alias permanent

For an alias to be permanent, add it to file: ~/.bash_aliases (tilde ~ is a substitute for /home/username/).

If setting the alias is the last command you’ve executed, running:

$ echo !!:q >> /home/username/.bash_aliases

will add the alias to the .bash_aliases.

If this file does not exist, create it: $ touch ~/.bash_aliases.

Then check if the following lines are present in the file ~/.bashrc:

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

If not, add them.

The first four lines are mnemonics. The pound key # comments them out for the script to ignore them. The three following lines tell the Bash configuration file (a.k.a. a dot file”) to check if a ~/.bash_aliases exists; if so, to run it.

Let me know if you have questions.

Feedback welcome at yctct dot domain name.

See also:


personal computing command-line interface (cli) gnu linux trisquel shell literacy office applications wiki

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2024 by yctct is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88