3
I’m using GNU bash version 4.3.46.
One problem I have when typing commands, is that I often end up forgetting a space between the command and its parameters. Examples:
cd..
gitlog
When the right should be, respectively, cd ..
and git log
. The most common cases (commands I use most often) I solved by creating several alias
, as an example:
alias cd..='cd ..'
alias gitlog='git log'
But since my mistake of forgetting space is frequent, I would like a more general solution, rather than having to create dozens of alias
for every possibility - since the problem also happens with commands that I only use sometimes, and it is not worth creating a alias
just for that.
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 for testing (first I wanted to test a command with space just to see how it works; a second step is to adapt it to check the cases without space):
__print_completions() {
printf '%s\n' "${COMPREPLY[@]}"
}
COMP_WORDS=(git l)
COMP_LINE='git l'
COMP_POINT=6
COMP_CWORD=1
_git
__print_completions
The exit was log
, what is right. There are still a few more details to work on the script, such as calling the command if there is only one possibility, etc. But this is not the case.
The focus of the question is the problems I’m failing to solve:
- how to make this script receive as parameter the command I typed?
- how to break this command correctly?
- ex:
cd..
can be broken asc d..
,cd ..
andcd. .
- ex:
- how to make the script be triggered only when the command I typed is not found? (i.e., if there is an alias or a valid command, I don’t need this script, just run the command)
That is to say, if I type cd..
, bash will recognize that this command is invalid and must call this script (which will fix to cd ..
and run the corrected command).
How to do this? (if possible)
I’m also starting to think that maybe this script isn’t the best way, but I don’t know if there’s any other way to solve this.
Very interesting!
– Lacobus