Zsh – How to Rebuild Auto-Complete Index and Binaries in $PATH Cache

autocompletezsh

After installing new software, an already opened terminal with zsh won't know about the new commands and cannot generate auto-complete for those. Apparently opening a new terminal fix the problem, but can the index (or whatever you call it) be rebuilt so that auto-complete will work on the old terminal?

I tried with compinit but that didn't help. Also, is there a way that is not shell-dependent? It's nice to have a way to verify the answer as well (except for uninstalling something and reinstalling it).

What I mean is after typing a few characters of a command's name, I can press Tab, and zsh should do the rest to pull up the full name.

Best Answer

To rebuild the cache of executable commands, use rehash or hash -rf.

Make sure you haven't unset the hash_list_all option (it causes even fewer disk accesses but makes the cache update less often).

If you don't want to have to type a command, you can tell zsh not to trust its cache when completing by putting the following line in your ~/.zshrc¹:

zstyle ":completion:*:commands" rehash 1

There is a performance cost, but it is negligible on a typical desktop setting today. (It isn't if you have $PATH on NFS, or a RAM-starved system.)

The zstyle command itself is documented in the zshmodule man page. The styles values are documented in the zshcompsys and zshcompwid man pages, or you can read the source (here, of the _command_names function). If you wanted some readable documentation… if you find some, let me know!

¹ requires zsh≥4.3.3, thanks Chris Johnsen