I don't use a huge number of aliases, so I have all of my aliases in my ~/.bashrc.
You can see the number of aliases you have set up via the command:
alias | wc -l
I can see I have 78 aliases set up. Which is quite a lot, but certainly nowhere near thousands.
But if you have a really large number of aliases, you could put them in a separate .bash_aliases file in your home directory. That way they're all together in a discrete file and won't be polluting/complicating your .bashrc.
To see what aliases you have defined, simply use the
alias
command and it will list all of the aliases that you have set up.
If you have a large number of aliases, consider piping the output from
alias
to a pager like
more
, or
less
, or to a terminal based browser like
w3m
or
lynx
.
e.g.
or
That way the pager/browser will receive the output from the
alias
command and you can scroll through your list of aliases to find the aliases you're looking for.
Regarding managing aliases - I don't see any point in numbering your aliases, or anything like that.
Numnbering your aliases isn't going to make them memorable. Give them short names that help you to remember what they do.
I sometimes forget which aliases I have set up. If I ever want to know what aliases I've got set up for a particular tool, I'll pipe the output of
alias
to something like
grep
or
ag
(AKA The Silver Searcher - a multi-threaded grep on steroids) to search for matching aliases.
So for example, I use
cmus
(terminal based audio player) a LOT. I have a bunch of aliases set up for various tasks in
cmus
.
Rather than looking through a list of all 78 of my current aliases, I can see any aliases which use
cmus
by entering the command:
And that yields me the following list of aliases:
Code:
alias cmaddl='cmus-remote -l'
alias cmaddp='cmus-remote -P'
alias cmaddq='cmus-remote -q'
alias cmstats='cmus-remote -Q'
alias next='cmus-remote -n;nowplay'
alias pause='cmus-remote -u;'
alias play='cmus-remote -p;nowplay'
alias prev='cmus-remote -r;nowplay'
alias resume='cmus-remote -u;nowplay'
alias shf='cmus-remote -S; echo "Shuffle: $(cmus-remote -Q | \grep shuffle | awk '\''{ print $3 }'\'')"'
alias stop='cmus-remote -s'
In the above, I can see all of the aliases I've set up for
cmus
.
Again, if I potentially had a large number of aliases set up for a particular command - I might also consider piping the output to a pager.
So for example, if I have a couple hundred aliases set up which use ssh to connect to different servers, I might use something like
Where
w3m
is my preferred terminal based pager/browser. You could swap that out for
less
, or
more
, or
lynx
, or whatever.
So for me at least, managing my aliases is just down to using the
alias
command in conjunction with other standard tools to see the aliases that I'm interested in.
If I want to add a new temporary alias, for the life of the current terminal session - I'll just quickly enter a new alias. If I want to temporarily remove an alias, I'll use the
unalias
command.
If I want to add, a new permanent alias (or remove a permanent alias), I'll edit my .bashrc and then reload/re-source it in any open terminals to apply it. In fact, to do this I actually use an alias called
bashr
, which looks like this:
Bash:
alias bashr='vim ~/.bashrc; . ~/.bashrc'
Above will open my .bashrc in vim. I can edit my .bashrc to make any changes I want (including adding/removing aliases). After saving and exiting vim, the running terminal then uses the
.
builtin to re-load my .bashrc - allowing any changes to be picked up in the currently running shell.
Going back to the topic of making your aliases memorable:
For a while I called it
basher
(which in my head stands for .BASHrc Edit and Re-init - I find that using mnemonics and acronyms in your aliases can sometimes help you to remember them.).
Eventually, I dropped the e and renamed it to
bashr
, purely to save me from having to enter one extra character. What can I say? I'm supremely lazy! The less characters I have to type to get something done, the better!
As much as I like entering as few characters as possible, I wouldn't be so keen on numbering all of my aliases because it would be much more difficult to remember what each one did.
So to round up, the best way of managing aliases (in my opinion) is simply to use the
alias
command in conjunction with other standard tools. And to give your aliases short names that help to describe what they do.
I do have some really short, one character aliases:
Bash:
alias j='jobs -l'
alias l='ls -CF --color=auto'
alias v='vim'
Again - using single letter aliases might not very memorable. I remember these OK. But anything that is short and memorable will work.
My only golden rule with aliases is:
If the name of an alias contains more characters than the actual command/commands it aliases - don't bother aliasing it!