Zsh-autosuggestions: History-based suggestions for cd are often invalid

Created on 17 Jun 2018  路  1Comment  路  Source: zsh-users/zsh-autosuggestions

Suggestions for cd generally suggest the most recent (or frequent?) cd ... command, which is often not valid in the current working directory. I think (but I'm not sure) that the Fish suggestions for cd are location-aware, which is handy.

screenshot from 2018-06-16 20-38-05

330 seems to make a fix for this more possible. According to #111 (comment), prioritizing the completion strategy over history in ZSH_AUTOSUGGEST_STRATEGY gives more relevant cd suggestions in most cases.

has-workaround wontfix

Most helpful comment

I think it's unlikely that this will be fixed for the history suggestion strategy, as it simply looks through your shell history for any entries that match the prefix. Zsh has no option to natively store the current working directory for each history entry, and I don't want to get this plugin into the business of trying to parse which words in history entries refer to directories/files and ensuring that those directories/files exist before offering the entry as a suggestion.

That being said, I have had pretty decent success using zsh-histdb to store current working directory alongside history entries and writing a custom suggestion strategy to suggest commands executed in the current directory before commands executed in other directories.

My .zshrc has this config:

ZSH_AUTOSUGGEST_USE_ASYNC=true
ZSH_AUTOSUGGEST_STRATEGY=(histdb history completion)

_zsh_autosuggest_strategy_histdb() {
    typeset -g suggestion
    suggestion=$(_histdb_query "
            SELECT commands.argv
            FROM history
                LEFT JOIN commands ON history.command_id = commands.rowid
                LEFT JOIN places ON history.place_id = places.rowid
            WHERE
                commands.argv LIKE '$(sql_escape $1)%' AND
                places.dir = '$(sql_escape $PWD)'
            GROUP BY commands.argv
            ORDER BY history.start_time desc
            LIMIT 1
    ")
}

The SQL query can be tweaked to get different behavior. There are more examples of SQL statements for suggestion strategies in that project's readme: https://github.com/larkery/zsh-histdb#integration-with-zsh-autosuggestions

There are currently some issues related to LIKE not being case-sensitive in SQLite, but it looks like I may be able to fix it with PRAGMA case_sensitive_like=ON;, though I haven't gotten the chance to look into it yet.

You may also be interested in https://github.com/ericfreese/zsh-prioritize-cwd-history and https://github.com/ericfreese/zsh-cwd-history, two plugins I wrote a while ago related to this issue. Though, I haven't maintained those in some time.

>All comments

I think it's unlikely that this will be fixed for the history suggestion strategy, as it simply looks through your shell history for any entries that match the prefix. Zsh has no option to natively store the current working directory for each history entry, and I don't want to get this plugin into the business of trying to parse which words in history entries refer to directories/files and ensuring that those directories/files exist before offering the entry as a suggestion.

That being said, I have had pretty decent success using zsh-histdb to store current working directory alongside history entries and writing a custom suggestion strategy to suggest commands executed in the current directory before commands executed in other directories.

My .zshrc has this config:

ZSH_AUTOSUGGEST_USE_ASYNC=true
ZSH_AUTOSUGGEST_STRATEGY=(histdb history completion)

_zsh_autosuggest_strategy_histdb() {
    typeset -g suggestion
    suggestion=$(_histdb_query "
            SELECT commands.argv
            FROM history
                LEFT JOIN commands ON history.command_id = commands.rowid
                LEFT JOIN places ON history.place_id = places.rowid
            WHERE
                commands.argv LIKE '$(sql_escape $1)%' AND
                places.dir = '$(sql_escape $PWD)'
            GROUP BY commands.argv
            ORDER BY history.start_time desc
            LIMIT 1
    ")
}

The SQL query can be tweaked to get different behavior. There are more examples of SQL statements for suggestion strategies in that project's readme: https://github.com/larkery/zsh-histdb#integration-with-zsh-autosuggestions

There are currently some issues related to LIKE not being case-sensitive in SQLite, but it looks like I may be able to fix it with PRAGMA case_sensitive_like=ON;, though I haven't gotten the chance to look into it yet.

You may also be interested in https://github.com/ericfreese/zsh-prioritize-cwd-history and https://github.com/ericfreese/zsh-cwd-history, two plugins I wrote a while ago related to this issue. Though, I haven't maintained those in some time.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dragonxlwang picture dragonxlwang  路  3Comments

OmeGak picture OmeGak  路  3Comments

clay-f picture clay-f  路  7Comments

guitaristtom picture guitaristtom  路  6Comments

mehedi-sharif picture mehedi-sharif  路  6Comments