zsh-users / zsh-autosuggestions

Fish-like autosuggestions for zsh
MIT License
30.3k stars 1.85k forks source link

Disable completions when line contains a comment #773

Open bretello opened 6 months ago

bretello commented 6 months ago

Problem

When using ZSH_AUTOSUGGEST_STRATEGY=(completion), starting a comment inline sometime results in extra completions being shown after the start of the comment (# )

In the following example, -e is proposed as autosuggestion after the comment (completion for cat)

image

Solution

A potential approach could be disabling completions whenever a comment is started. I've experimented with the following:

export ZSH_AUTOSUGGEST_COMPLETION_IGNORE='* \#*'

which works in basic scenarios, but I'm sure it would break down in some cases (e.g. a string containing <space>#, multi-line statements?)

pwnalone commented 4 months ago

I'm currently using @bretello's workaround to disable auto-suggestions within comments, but it's not ideal. My zshrc contains the following lines.

ZSH_AUTOSUGGEST_STRATEGY=(history completion)
ZSH_AUTOSUGGEST_COMPLETION_IGNORE='*\#*'
ZSH_AUTOSUGGEST_HISTORY_IGNORE='*\#*'

But now, if there is a # character in the command line that does not begin a comment, then I will no longer get suggestions.

image image

Maybe this could be solved for # characters contained within strings using a sufficiently fancy glob pattern, but there are other cases too (e.g. arr=(1 2 3); echo "${#arr[@]}" && ...), so I think another solution might be necessary.