filepath autosuggestions? #42
So far I think this plugin is the most advanced in this regard. It badly needs significant contributions!
The goal behind the completion server is to use the user completion functions/configuration(including filepath) to provide autosuggestions. I gave up on that because I've bumbed into zsh limitations the last time I tried to make it work nicely.
However, it may be possible to reactivate the completion server in the latest zsh version, so I'd give that I shot if you want filepath completions.
I played around a little with it but did not get very far. The completion server is working and I obtain a filepath completion result (by default the longest match is returned). However, I cannot seem to find any back channel to the client? Is this part missing or did I overlook something?
The bit of code responsible for fetching a suggestion is here.
You should be able to play around for now by overriding the _zsh_autosuggest_suggestion function somewhere in your .zshrc after sourcing the plugin.
Wow you did a thorough refactoring, looks pretty sleek :) My problem basically is that I'd have to call the existing completion functions with the current prefix (i.e. _main_complete?) but I don't really get how to do that? Is it even possible to call another completion function/widget outside of a completion function?
Last time I manually worked around this by only doing local file completion but what we'd actually want is all default completion displayed in an autocomplete fashion and not only history completion. It cannot be too difficult but I'm not really knowledgeable in zsh scripting and the docs aren't of much help for this specific case.
I'd have to call the existing completion functions with the current prefix (i.e. _main_complete?) but I don't really get how to do that
the docs aren't of much help
Yeah, I don't really get how to do that either ;) There's a pretty active irc channel that might be of some help.
Can you clarify exactly what behavior you're thinking of? A simple example could be really helpful.
In general I'd think of zsh-autosuggestions to just 'complement' the exiting completion. That means it should just pass the current prefix to the currently active completion widget and pretty-print the result somehow.
A simple example would be a directory containing file1.txt file2.txt file3.txt
I now do an
ls f.
What I would expect (and what is currently happening) is that the history is searched and the suggestion is printed, so for instance:
ls filefoo.txt.
As I continue typing the history search continues backwards until no further match is found. Then the local filename completion (or whatever is configured for zsh's completion) should take over and suggest:
ls file1.txt.
I could provide a somewhat buggy and working solution for this exact case but I feel the problem should be solved on completion level as others might have different needs.
Further improvements would be to combine all this with auto-fu (display suggestions below the cursor as you type as well as fuzzy matching if all completers fail). These are low-priority for me but if we had all these I suppose the completion/suggestion system would be pretty much perfect :)
I'll try to get assistance in the irc channel, thanks.
Answer from irc:
16:53:03 > For the time being, is there some workaround (albeit
slow etc.) to access the completion results, except
spawning another shell?
16:57:32 Mikachu> no
So it seems as long as the zsh infrastructure doesn't change, we are bound to either use tarruda's client-server model or reimplement existing completions (poorly? ;) ) I find both rather unappealing. Seems, fish is better off here.
Thanks for looking into this @modk. How does the client-server model solve this problem?
Something I've thought about (and I'm not sure how possible/reasonable this is) is to keep track of the directory that commands were issued in and how frequently they were issued and then use those to fetch the suggestion. So if I've issued git checkout some_branch a lot in ~/Code/some_project but I've just issued the command git checkout something_else and I start to type git check, it will show me git checkout some_branch instead because it's been used more frequently even though it's not the most recent history result that matches.
I'd like to see this plugin grow different "strategies" for fetching a suggestion, and you could choose one by setting a global config variable.
If I get it correctly (still unsure though) the server-zsh would actually execute the completion based on the prefix it got sent from the client (i.e. ls f). As soon as the completion is finished, the server sends the results (or only the first, depending on settings) back to the client which can do a colored suggestion then.
Your idea seems interesting and it should definitely be doable. There exists something alike for directory jumps, see https://github.com/rupa/z. The principle is to keep track of most used directories (in a dedicated file, weighting them dependent on how often you cd'ed into them) and providing a quick jump.
@modk Just found this plugin that stores a separate history for each directory. Plugged it in and at first glance it seems to work great with this autosuggest plugin to do exactly what I wanted.
EDIT: Actually, found some issues with that plugin, so I created my own. Check it out at https://github.com/ericfreese/zsh-cwd-history.
Sorry for reviving this again, but I feel we should keep this issue open if it is of interest or close it with a wontfix if it is not.
The per-dir-history is nice but not really what I (and others) am looking for. I would like to have the autosuggestions to work the same way they do in fish, namely suggesting everything that can be reached via TAB/the normal completion.
For example, it would be especially nice to complete command arguments (that have not been previously typed) possibly even with a description text that the normal completion would do. A first-order approximation to this behavior would probably involve emulating a TAB press after every keystroke and display the first line of the result colored in the autosuggestion color.
Is there any way to enable fish-alike file/directory autosuggestion in commands that receiving it, like cd, ls etc.? Any other plugin maybe? thx