I'm using GNU bash version 4.3.46 .
One problem I have when typing commands is that I often forget a space between the command and its parameters. Examples:
cd..
gitlog
When the correct one should be cd ..
and git log
, respectively. The most common cases (commands I use most often) I solved by creating several alias
, for example:
alias cd..='cd ..'
alias gitlog='git log'
But since my mistake of forgetting the space is frequent, I would like a more general solution, instead of having to create dozens of alias
for each possibility - since the problem also happens with commands that I only use at times, and it's not worth creating a alias
just for this.
First I tried to make a script that, given an incomplete command, shows the options to complete it. In the example below I used git l
as input, just to test (first I wanted to test a command with space just to see how it works; a second step is to adapt it to check cases with no space):
__print_completions() {
printf '%s\n' "${COMPREPLY[@]}"
}
COMP_WORDS=(git l)
COMP_LINE='git l'
COMP_POINT=6
COMP_CWORD=1
_git
__print_completions
Output was log
, which is correct. There are still some more details to work on in the script, such as calling the command, if there is only one possibility, etc. But that's beside the point.
The focus of the question are the problems I can not solve:
- How to make this script receive as a parameter the command that I typed?
- how to break this command correctly?
- ex:
cd..
can be broken asc d..
,cd ..
andcd. .
- ex:
- How do I get the script to fire only when the command I typed is not found? (that is, if there is an alias or a valid command, I do not need this script, just run the command)
In other words, if I type cd..
, bash will recognize that this command is invalid and should call this script (which will correct for cd ..
and will run the corrected command).
How to do this? (if possible)
I'm also starting to think that maybe this script is not the best way, but I do not know if there's another way to solve it.