Autoexpanding aliases with readline; the story of suod
The problem is: execute
suod is typed.
A trivial solution would be to put in place a shell alias:
it makes sense, but command line completion will not be available.
This can be worked around by setting up the same completion function of the aliased command for the aliasing one:
source /usr/share/bash-completion/completions/sudo complete -F _sudo suod
this is nice, but not enough for me, it is still lacking “cross-alias” history completion in case the wrong version is typed only sometime; and history completion is the greatest thing since... you know.
For that purpose this variant of key bindings will be used:
Either the following can be put in
or the bash builtin command
bindcan be invoked from
bind '"suod": "\C-vsudo"'
Now every time the
suodkey sequence is typed in, it will be replaced automatically with
sudoon the command line.
The \C-v sequence calls the quoted-insert function which tells readline that the following character —namely s— has to be taken verbatim, this is needed to avoid that the s key is considered as the start of a macro expansion itself messing up the end result.
This solution is good enough for me, as I get history completion.
Maybe there could be downsides too:
- the expansion happens at any point in the line;
- there could be conflicts if many of these typo-fixing-hacks share letters one another; the possible issues could be alleviated by prefixing each character in the macro string with the sequence for quoted-insert.
Unrelated, but while googling around I've found out how to have aliases executed in a
sudo context by using this alias:
alias sudo='sudo '
Check out the Aliases section of the bash manual for the complete explanation.