1

I am running MacOS Big Sur 11.2.3 (20D91) and using zsh (the default shell).

Whenever I added new executable files/scripts to $PATH, I can not auto-complete the script filename in the existing terminals. I have to open a new terminal to make the autocomplete work for the new files.

Is that possible to make the autocomplete work for new files in $PATH automatically for old existing terminal sessions?

Thanks a lot.

1 Answer 1

3

The issue is that the new command is not been entered into the hash table that zsh that maps commands to the full name.

The command hash table is built when you first attempt a command in a new shell as you have shown.

To make the command get added to the table without startinga new shell run the command rehash

4
  • Thank you very much for your answer. I have upvoted it. This solves a half of the question. Do you know how to make this happen automatically after adding new commands? In Linux, the new commands will be available automatically. I am wondering if I can achieve the same on MacOS. If not possible, I will have to live with it then. Thanks.
    – sgon00
    Apr 27, 2021 at 2:11
  • On Linux you probably used bash rather than zsh and that might act differently for adding executables - if you add one earlier in the path you will need to rehash (hash -r). Also is your $PATH the same in both cases if it is in a different order then adding excutables will behave differently (I don't know the details as my use predates bash and csh/tcsh always needed rehash so I always ran it when I had an issue)
    – mmmmmm
    Apr 27, 2021 at 7:51
  • On second thoughts - the original problem could be different paths on Linuix and macOS
    – mmmmmm
    Apr 27, 2021 at 7:53
  • Thanks a lot for the reply. Yeah, on Linux, it's bash shell by default. Bash will auto update the search hash somehow. I find out I could put zstyle ":completion:*:commands" rehash 1 to .zshrc and it will make rehash eveytime with a new autocomplete search. But there is a speed penalty for this, and it will not be really noticeable with fast file access drive though. Thanks a lot for letting me know the keyword rehash so that I can google more on this. I have marked your answer as the answer. Cheers.
    – sgon00
    Apr 27, 2021 at 9:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .