Are you familiar with Huffman encoding? That’s where you pick shorter codes for more frequent letters. Morse code is the same way, in that the most-used letters are the shortest. [Matheus Richard] had the same idea for optimizing your workflow in the Linux shell. The idea is to measure what commands you use the most and make them shorter.
If you use zsh, it is easy to find out what commands you are using the most. If you use bash, [Matheus] helpfully offers a command to give you a similar result (the original post limits the list to the last entry which we are sure is a typo):
history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10
Once you know the commands you use the most, you can use your shell’s aliasing or scripts to shorten them up. For example, in [Matheus’] case, git was a very common command. This led to aliases:
alias gc="git commit --verbose" alias gp="git push" alias gprom="git pull --rebase origin main"
Not only does this save typing, but you lessen your chance for typos: “git comit”, for example. Another idea is to alias your common errors, for example setting an alias for “git” as “gti”.
These are small things, but they are definitely time savers. Be sure to read the rest of the post, as there are a number of other optimization ideas. [Matheus] definitely has a thing for zsh, but there are many other shells out there. Some of them are even evolving towards more modern programming languages.
For sure, I’ve been using long lists of aliases for over 25 years so I can do lots of nifty command-prompt things quickly and without mistakes. It’s to the point that I have to look at my .bash file to recall the actual commands, haha.
I have so many aliases that I have an agrep alias just to search through them…
That’s kind of the problem, isn’t it? Back in the 80s, I had all sorts or productivity enhancing short cuts. When I found myself on a vanilla machine, I was completely lost. I still use aliases, just not as numerous as before.
What specifically are you referring to? Because if you refer to
history 1
, this is needed with zsh, by defaulthistory
only returns the last 10 entries and you have to specify “how far back” you want to go.For bash this however not the case..
Bash history seems to only return the last 1000 commands.
This probably will get mangled by the comment processing markup whatever. But this should reliably get the top 20 commands for bash users:
$ cat ~/.bash_history | awk ‘{ print $1 }’ | sort | uniq -c | sort -hr | head -20
History of commands in zsh is configurable, both in count and storage consumed.
I keep the last 20,000 with no difficulty, including timestamps, and
checkpoint my histories periodically so that I have years of records.
You can remove the “in zsh” in your first line, since in any shell the history program is configurable. I think I set mine to 100K in bash… I do trim duplicates and ls commands and probably a few others like pwd, du, date, etc …
You don’t lessen your chance of significant typos by making every command a 2 letter command.
Careful what you allow to happen without user interaction.
(Ignore those that say they would never do anything stupid in an aliased command….)
No, Al is correct.
This was studied by IBM. They wanted to reduce the number of programmer errors and did a whole bunch of experiments and surveys.
They found that the number of typos is proportional to the number of characters typed, and it was unrelated to any other consideration such as the intent or meaning of the characters typed. Each programmer had a constant number of typos per 100 lines of code, that number varied by programmer, and didn’t change much over the course of the study.
C programmers had the same number of typos per line of code as did assembly programmers.
Their conclusion was that programming languages that were highly expressive – ones that packed a lot of meaning into a few lines – would have fewer typos than less expressive languages.
This then got taken to extremes for languages like APL and perl, but the theory is sound.
Reducing the typing you need to do for frequent commands will also reduce your chances of making a typo.
(I happen to like perl for its expressiveness. I don’t push the envelope beyond human understanding, but the ability to read a file into an array of strings or build a structure on the fly without having to go back and forth between ideating a new value, adding it to the definition, then going back to use it.)
But the condensed code will also make the impact of the typo worse in the same proportion, so what exactly was gained?
In more verbose languages, the effect of a typo is more likely to be a “syntax error” rather than “delete everything”.
I think the language that took it to the most extreme was MUMPS. I never used it, but was told there were 26 commands, each starting with a different letter.
I’m a big fan of aliases, but I tend to create them based on how long the command is, rather than how often I use the command.
For example, I haven’t aliased git’s push and pull operations (used many times per day), but I’ve got an “fmm” alias to Fetch the latest code from Main and Merge it into my current branch (used maybe once per day, maybe less).
As an avid MUDder I’m very quick to use aliases to shorten long commands. ‘get all from corpse’ vs ‘gac’, no contest =P
Note to self: “>” at the start of a line is “quote” in Hackaday.
Note to Hackday admins: Implement preview please.
That’ll come when we can edit our own comments.
But thank you for documenting this syntax.
If one uses zsh oh-my-zsh is a valuable resource for shell convenience. all the examples and more are already included.
powerlevel-10k is the perfect addition to omz
“Another idea is to alias your common errors”; evokes “DWIM”
http://www.catb.org/jargon/html/D/DWIM.html
I did that back in the day. I would inevitably type “dri” instead of “dir”, so I aliases it (not UNIX obviously).
Unix
This doesn’t seem like a good idea if you use multiple systems or have to use systems that aren’t yours. Then you either have to set up your aliases and remove them after or type the full command which you might not know as well as you should because you just use your alias. Most commands are short enough that they aren’t that much trouble to type and you can use autocomplete too.
Up vote.
While useful, knowing what the stock command is off the top of your head is more useful. Generally in devops one has to access hundreds of Linux vms and run commands on them. Aliasing obscures knowledge and history logs. Maintain a list of commands and Copy and paste if one is worried about typos
don’t need to analyze to know I need
alias g=git
also .gitconfig keeps its own aliases, and out of .bashrc
For the naysayers, this is about productivity.
Why type “mvn clean install -DskipTests” when one can type “mci”? This is but one trivial example. One can check of course if there is any such “mci” command before creating the alias. One can choose to use or not use such aliases on remote servers on which one has a personal home directory. Etc etc.
Instead of aliases, you should use shell functions. They can handle arguments unlike aliases
This is an incredible oxymoron as the kernel, Linux does not implement a shell.
The linked article appreciates this and is about the zsh shell and does not mention “Linux”.
Both GNU bash and zsh are rather GNU shells, considering that zsh is built on top of GNU ncurses and GNU roff has GNU database libraries as an optional dependency.
Why not have the auto complete feature default to most used? Then it will only be 2 keystrokes for the most used command for every letter of the alphabet. The first letter and then the spacebar (or tab, then space I Guess). Unless I am crazy. The two are not mutually exclusive.
I see, this is a command with switches and arguments. Interesting 🤔
Nice 😄
Sure… https://github.com/paulmars/hufshell – 12 years ago.
https://github.com/paulmars/huffshell
I’ve avoided aliases unless there is a business logic component to the command, or if it’s a set of commands that compose a workflow.
The reason being I’ve seen many people struggle on new or remote machines and offset whatever gains those aliases bought on theirs. I also like being able to collaborate with work, and seeing the actual commands allows myself or whoever I’m pairing with to knowledge transfer on unfamiliar commands or novel approaches to using them.
One of my favourite aliases is cheat for calling cheat.sh on a command to get quick summaries and examples on how to use a command. Much more immediately digestible than man, where man is nicer for a deep dive sometime after the immediate need is addressed.