Another (silly) idea: autocorrect in shell (bash) tab completion

Here's another idea that just popped up in my head and can be considered silly, somewhat: autocorrect for tab completion.

bash already has cdspell shell option (enabled via shopt -s cdspell) which can correct directory names for you, so for example if you have a directory named "pictures" and you type "cd picture" or "cd pcitures" it will assume you meant "pictures" and cd to the corrected directory. To be honest I've never used this feature.

bash also has another option called dirspell which can correct spelling in directory name during completion. Try it out with shopt -s dirspell direxpand. So now if you type "ls /ect/pa" and press [TAB] bash will correct it into "ls /etc/pa". Pretty nifty. I'm only recently aware of this option and will turn the setting on for the next few weeks to find out if this is useful.

But the dirspell setting only works for directory/filename and it does not show suggested alternatives. I was thinking more like Google search autocomplete and typeahead suggestion.

So suppose we have a program that accepts a username. Available usernames are ["audrey", "audry", "andy", "adrian", "adrienne", "amanda"]. If the user types:

% suspend ad[TAB]

it will complete with ["adrian", "adrienne"]. But when the user makes a typo and types:

% suspend adu[TAB]

we can suggest a correction: ["audrey", "audry"]. Unfortunately, bash currently won't display the completion and will simply replace the buffer with suspend a.

The completion library can work around this by providing this completion instead: ["adu(audrey)", "adu(audry)"]. Thus after pressing [TAB] here's what she'll see:

% suspend adu\(audr
aud\(audrey\)   aud\(audry\)

After the completion finishes, the completion library can replace the argument with the corrected value and feed it to the program.

This idea is silly because it's just using what limited interface bash gives. The more proper solution would be for bash to allow mispelling during completion. Thus "adu[TAB]" will display ["audrey","audry"] and replaces the buffer with "aud".

I wonder if zsh can do this (or is it in fact already possible for bash to have this behavior?)


You should try fish which has many interactive features that you may like.

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays). My CPAN ID: SHARYANTO. I'm sedusedan on perlmonks. My twitter is stevenharyanto (but I don't tweet much). Follow me on github: sharyanto.